Skip to content

Commit

Permalink
update flow yaml in business
Browse files Browse the repository at this point in the history
  • Loading branch information
gy2006 committed Dec 20, 2024
1 parent fd6ceaf commit e6c5c54
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package com.flowci.flow.business;

public interface FetchFlowYamlContent {
/**
* Fetch yaml content
* @param id flow id
* @return YAML with Base64 format
*/
String invoke(Long id);
}
10 changes: 10 additions & 0 deletions src/main/java/com/flowci/flow/business/UpdateFlowYamlContent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.flowci.flow.business;

public interface UpdateFlowYamlContent {
/**
* Update flow YAML
* @param id flow id
* @param b64Yaml YAML with Base64 format
*/
void invoke(Long id, String b64Yaml);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Base64;

import static java.lang.String.format;

@Slf4j
Expand All @@ -22,6 +24,7 @@ public String invoke(Long id) {
if (optional.isEmpty()) {
throw new NotAvailableException(format("flow %s not found", id));
}
return optional.get().getYaml();
var yaml = optional.get().getYaml();
return Base64.getEncoder().encodeToString(yaml.getBytes());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.flowci.flow.business.impl;

import com.flowci.common.RequestContextHolder;
import com.flowci.common.exception.NotAvailableException;
import com.flowci.flow.business.UpdateFlowYamlContent;
import com.flowci.flow.repo.FlowYamlRepo;
import com.flowci.yaml.business.ParseYamlV2;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Base64;

import static java.lang.String.format;

@Slf4j
@Component
@AllArgsConstructor
public class UpdateFlowYamlContentImpl implements UpdateFlowYamlContent {

private final ParseYamlV2 parseYamlV2;

private final FlowYamlRepo flowYamlRepo;

private final RequestContextHolder requestContextHolder;

@Override
public void invoke(Long id, String b64Yaml) {
var yaml = new String(Base64.getDecoder().decode(b64Yaml));
var ignore = parseYamlV2.invoke(yaml);

var optional = flowYamlRepo.findById(id);
if (optional.isEmpty()) {
throw new NotAvailableException(format("flow %s not found", id));
}

var flowYaml = optional.get();
flowYaml.setYaml(yaml);
flowYaml.setUpdatedBy(requestContextHolder.getUserId());
flowYamlRepo.save(flowYaml);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.flowci.flow.business;

import com.flowci.SpringTest;
import com.flowci.TestUtils;
import com.flowci.flow.model.FlowYaml;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Base64;
import java.util.Optional;

import static org.instancio.Select.field;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;

class FetchFlowYamlContentTest extends SpringTest {

@Autowired
private MockRepositoriesConfig repositoriesConfig;

@Autowired
private FetchFlowYamlContent fetchFlowYamlContent;

@Test
void whenFetching_thenReturnYamlEncodedWithBase64() {
var expectedYaml = "some yaml";
var mockFlowYaml = TestUtils.newDummyInstance(FlowYaml.class)
.set(field(FlowYaml::getYaml), expectedYaml)
.create();

var mockFlowYamlRepo = repositoriesConfig.getFlowYamlRepo();
when(mockFlowYamlRepo.findById(any())).thenReturn(Optional.of(mockFlowYaml));

var base64Yaml = fetchFlowYamlContent.invoke(1L);
assertEquals(Base64.getEncoder().encodeToString(expectedYaml.getBytes()), base64Yaml);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.flowci.flow.business;

import com.flowci.SpringTest;
import com.flowci.common.RequestContextHolder;
import com.flowci.flow.model.FlowYaml;
import com.flowci.yaml.business.ParseYamlV2;
import com.flowci.yaml.model.FlowV2;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;

import java.util.Base64;
import java.util.Optional;

import static com.flowci.TestUtils.newDummyInstance;
import static org.instancio.Select.field;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

class UpdateFlowYamlContentTest extends SpringTest {

@Autowired
private UpdateFlowYamlContent updateFlowYamlContent;

@Autowired
private MockRepositoriesConfig mockRepositoriesConfig;

@MockBean
private ParseYamlV2 mockParseYamlV2;

@MockBean
private RequestContextHolder mockRequestContextHolder;

@Test
void givenYaml_whenUpdating_thenUpdated() {
var mockUserId = 10L;
var mockFlowId = 20L;

var mockFlowYaml = newDummyInstance(FlowYaml.class)
.set(field(FlowYaml::getId), mockFlowId)
.set(field(FlowYaml::getYaml), "yaml")
.set(field(FlowYaml::getUpdatedBy), 1L)
.create();

when(mockRequestContextHolder.getUserId()).thenReturn(mockUserId);
when(mockParseYamlV2.invoke(any())).thenReturn(mock(FlowV2.class));

var flowYamlCaptor = ArgumentCaptor.forClass(FlowYaml.class);
var mockFlowYamlRepo = mockRepositoriesConfig.getFlowYamlRepo();
when(mockFlowYamlRepo.findById(eq(mockFlowId))).thenReturn(Optional.of(mockFlowYaml));
when(mockFlowYamlRepo.save(flowYamlCaptor.capture())).thenReturn(mockFlowYaml);

var base64Yaml = Base64.getEncoder().encodeToString("updated yaml".getBytes());
assertDoesNotThrow(() -> updateFlowYamlContent.invoke(mockFlowId, base64Yaml));
assertEquals("updated yaml", flowYamlCaptor.getValue().getYaml());
assertEquals(mockUserId, flowYamlCaptor.getValue().getUpdatedBy());
}
}

0 comments on commit e6c5c54

Please sign in to comment.