Skip to content

Commit ad993f6

Browse files
Improve test coverage for service and controller layers
Add unit tests for OpenApiOperationService, WorkflowService and additional WorkflowController scenarios, increasing total tests from 18 to 38. Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com>
1 parent f0c923f commit ad993f6

3 files changed

Lines changed: 221 additions & 0 deletions

File tree

src/test/java/com/apiflows/controller/WorkflowControllerTest.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package com.apiflows.controller;
22

3+
import com.apiflows.exception.InvalidContentException;
4+
import com.apiflows.exception.UnexpectedErrorException;
35
import com.apiflows.model.WorkflowsSpecificationView;
6+
import com.apiflows.service.WorkflowService;
47
import org.junit.jupiter.api.Test;
58
import org.springframework.beans.factory.annotation.Autowired;
69
import org.springframework.boot.test.context.SpringBootTest;
@@ -13,6 +16,8 @@
1316
import java.nio.file.Paths;
1417

1518
import static org.junit.jupiter.api.Assertions.*;
19+
import static org.mockito.ArgumentMatchers.anyString;
20+
import static org.mockito.Mockito.*;
1621

1722
@SpringBootTest
1823
class WorkflowControllerTest {
@@ -40,6 +45,36 @@ public void getFromContent() throws Exception {
4045
assertTrue(responseEntity.getBody().getOpenAPIWorkflowParserResult().isYaml());
4146
}
4247

48+
@Test
49+
public void getFromContentWithComponents() throws Exception {
50+
ResponseEntity<WorkflowsSpecificationView> responseEntity = workflowController.getFromContent(getContentFromFile());
51+
52+
assertEquals(HttpStatus.ACCEPTED, responseEntity.getStatusCode());
53+
assertNotNull(responseEntity.getBody().getComponentsAsString());
54+
}
55+
56+
@Test
57+
public void getFromUrlThrowsInvalidContentException() throws Exception {
58+
WorkflowService mockService = mock(WorkflowService.class);
59+
when(mockService.getFromUrl(anyString())).thenThrow(new InvalidContentException("invalid"));
60+
61+
WorkflowController controller = new WorkflowController();
62+
controller.setWorkflowService(mockService);
63+
64+
assertThrows(InvalidContentException.class, () -> controller.getFromUrl("https://example.com/bad.yaml"));
65+
}
66+
67+
@Test
68+
public void getFromContentThrowsUnexpectedErrorException() {
69+
WorkflowService mockService = mock(WorkflowService.class);
70+
when(mockService.getFromContent(anyString())).thenThrow(new UnexpectedErrorException());
71+
72+
WorkflowController controller = new WorkflowController();
73+
controller.setWorkflowService(mockService);
74+
75+
assertThrows(UnexpectedErrorException.class, () -> controller.getFromContent("invalid content"));
76+
}
77+
4378
String getContentFromFile() throws Exception {
4479
String filePath = "src/test/resources/pet-coupons.arazzo.yaml";
4580

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.apiflows.service;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.*;
6+
7+
class OpenApiOperationServiceTest {
8+
9+
private OpenApiOperationService service = new OpenApiOperationService();
10+
11+
@Test
12+
void isUrlWithHttps() {
13+
assertTrue(service.isUrl("https://example.com/spec.yaml"));
14+
}
15+
16+
@Test
17+
void isUrlWithHttp() {
18+
assertTrue(service.isUrl("http://example.com/spec.yaml"));
19+
}
20+
21+
@Test
22+
void isUrlWithRelativePath() {
23+
assertFalse(service.isUrl("./pet-coupons.openapi.yaml"));
24+
}
25+
26+
@Test
27+
void isUrlNull() {
28+
assertFalse(service.isUrl(null));
29+
}
30+
31+
@Test
32+
void extractExampleByName() {
33+
assertEquals("MyExample", service.extractExampleByName("#/components/examples/MyExample"));
34+
}
35+
36+
@Test
37+
void extractExampleByNameSimplePath() {
38+
assertEquals("petExample", service.extractExampleByName("/components/examples/petExample"));
39+
}
40+
41+
@Test
42+
void getRootFolderNull() {
43+
assertEquals(".", service.getRootFolder(null));
44+
}
45+
46+
@Test
47+
void getRootFolderFromUrl() {
48+
String root = service.getRootFolder("https://example.com/api/spec.yaml");
49+
assertEquals("https://example.com/api/", root);
50+
}
51+
52+
@Test
53+
void getRootFolderFromAbsolutePath() {
54+
String root = service.getRootFolder("/Users/beppe/project/spec.yaml");
55+
assertEquals("/Users/beppe/project", root);
56+
}
57+
58+
@Test
59+
void getRootFolderFromRelativePath() {
60+
String root = service.getRootFolder("src/test/resources/pet-coupons.arazzo.yaml");
61+
assertEquals("src/test/resources", root);
62+
}
63+
64+
@Test
65+
void getRootFolderNoParent() {
66+
String root = service.getRootFolder("spec.yaml");
67+
assertNull(root);
68+
}
69+
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package com.apiflows.service;
2+
3+
import com.apiflows.exception.InvalidContentException;
4+
import com.apiflows.exception.UnexpectedErrorException;
5+
import com.apiflows.model.WorkflowsSpecificationView;
6+
import org.junit.jupiter.api.BeforeEach;
7+
import org.junit.jupiter.api.Test;
8+
9+
import java.nio.charset.StandardCharsets;
10+
import java.nio.file.Files;
11+
import java.nio.file.Path;
12+
import java.nio.file.Paths;
13+
14+
import static org.junit.jupiter.api.Assertions.*;
15+
import static org.mockito.Mockito.*;
16+
17+
class WorkflowServiceTest {
18+
19+
private WorkflowService service;
20+
private FileService fileService;
21+
22+
@BeforeEach
23+
void setUp() {
24+
service = new WorkflowService();
25+
fileService = mock(FileService.class);
26+
service.setFileService(fileService);
27+
}
28+
29+
@Test
30+
void getFromContentWithValidYaml() throws Exception {
31+
String content = getContentFromFile("src/test/resources/pet-coupons.arazzo.yaml");
32+
33+
WorkflowsSpecificationView view = service.getFromContent(content);
34+
35+
assertNotNull(view);
36+
assertNotNull(view.getOpenAPIWorkflowParserResult());
37+
assertNotNull(view.getOpenAPIWorkflowParserResult().getOpenAPIWorkflow());
38+
assertEquals("Petstore - Apply Coupons", view.getOpenAPIWorkflowParserResult().getOpenAPIWorkflow().getInfo().getTitle());
39+
}
40+
41+
@Test
42+
void getFromContentWithComponentsAsString() throws Exception {
43+
String content = getContentFromFile("src/test/resources/pet-coupons.arazzo.yaml");
44+
45+
WorkflowsSpecificationView view = service.getFromContent(content);
46+
47+
assertNotNull(view.getComponentsAsString());
48+
}
49+
50+
@Test
51+
void getFromUrlWithRealFile() throws Exception {
52+
String filePath = Paths.get("src/test/resources/pet-coupons.arazzo.yaml").toAbsolutePath().toString();
53+
String content = getContentFromFile("src/test/resources/pet-coupons.arazzo.yaml");
54+
55+
when(fileService.call(filePath)).thenReturn(content);
56+
when(fileService.isValidJson(content)).thenReturn(false);
57+
when(fileService.isValidYaml(content)).thenReturn(true);
58+
59+
WorkflowsSpecificationView view = service.getFromUrl(filePath);
60+
61+
assertNotNull(view);
62+
assertNotNull(view.getOpenAPIWorkflowParserResult().getOpenAPIWorkflow());
63+
assertEquals("Petstore - Apply Coupons", view.getOpenAPIWorkflowParserResult().getOpenAPIWorkflow().getInfo().getTitle());
64+
}
65+
66+
@Test
67+
void getFromUrlThrowsInvalidContentException() {
68+
String url = "https://example.com/spec.yaml";
69+
String invalidContent = "this is not valid yaml or json content for arazzo";
70+
71+
when(fileService.call(url)).thenReturn(invalidContent);
72+
when(fileService.isValidJson(invalidContent)).thenReturn(false);
73+
when(fileService.isValidYaml(invalidContent)).thenReturn(false);
74+
75+
assertThrows(InvalidContentException.class, () -> service.getFromUrl(url));
76+
}
77+
78+
@Test
79+
void getFromUrlThrowsUnexpectedErrorWhenContentIsNull() {
80+
String url = "https://example.com/spec.yaml";
81+
String emptyContent = "";
82+
83+
when(fileService.call(url)).thenReturn(emptyContent);
84+
when(fileService.isValidJson(emptyContent)).thenReturn(false);
85+
when(fileService.isValidYaml(emptyContent)).thenReturn(false);
86+
87+
assertThrows(InvalidContentException.class, () -> service.getFromUrl(url));
88+
}
89+
90+
@Test
91+
void getFromContentNoComponentsWhenAbsent() throws Exception {
92+
String content = "arazzo: 1.0.0\n" +
93+
"info:\n" +
94+
" title: minimal\n" +
95+
" version: v1\n" +
96+
"sourceDescriptions:\n" +
97+
" - name: petstore\n" +
98+
" url: https://petstore3.swagger.io/api/v3/openapi.json\n" +
99+
" type: openapi\n" +
100+
"workflows:\n" +
101+
" - workflowId: test-workflow\n" +
102+
" steps:\n" +
103+
" - stepId: step-one\n" +
104+
" operationId: listPets\n";
105+
106+
WorkflowsSpecificationView view = service.getFromContent(content);
107+
108+
assertNotNull(view);
109+
assertNull(view.getComponentsAsString());
110+
}
111+
112+
private String getContentFromFile(String filePath) throws Exception {
113+
Path fullPath = Paths.get(filePath).toAbsolutePath();
114+
byte[] encodedBytes = Files.readAllBytes(fullPath);
115+
return new String(encodedBytes, StandardCharsets.UTF_8);
116+
}
117+
}

0 commit comments

Comments
 (0)