|
| 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