-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorkflowControllerTest.java
More file actions
86 lines (63 loc) · 3.27 KB
/
WorkflowControllerTest.java
File metadata and controls
86 lines (63 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package com.apiflows.controller;
import com.apiflows.exception.InvalidContentException;
import com.apiflows.exception.UnexpectedErrorException;
import com.apiflows.model.WorkflowsSpecificationView;
import com.apiflows.service.WorkflowService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.*;
@SpringBootTest
class WorkflowControllerTest {
@Autowired
WorkflowController workflowController;
@Test
public void testGetFromUrl() throws Exception {
final String URL = "https://raw.githubusercontent.com/API-Flows/api-flows-studio/main/src/test/resources/pet-coupons.arazzo.yaml";
ResponseEntity<WorkflowsSpecificationView> responseEntity = workflowController.getFromUrl(URL);
assertEquals(HttpStatus.ACCEPTED, responseEntity.getStatusCode());
assertTrue(responseEntity.getBody().getOpenAPIWorkflowParserResult().isYaml());
}
@Test
public void getFromContent() throws Exception {
ResponseEntity<WorkflowsSpecificationView> responseEntity = workflowController.getFromContent(getContentFromFile());
assertEquals(HttpStatus.ACCEPTED, responseEntity.getStatusCode());
assertTrue(responseEntity.getBody().getOpenAPIWorkflowParserResult().isYaml());
}
@Test
public void getFromContentWithComponents() throws Exception {
ResponseEntity<WorkflowsSpecificationView> responseEntity = workflowController.getFromContent(getContentFromFile());
assertEquals(HttpStatus.ACCEPTED, responseEntity.getStatusCode());
assertNotNull(responseEntity.getBody().getComponentsAsString());
}
@Test
public void getFromUrlThrowsInvalidContentException() throws Exception {
WorkflowService mockService = mock(WorkflowService.class);
when(mockService.getFromUrl(anyString())).thenThrow(new InvalidContentException("invalid"));
WorkflowController controller = new WorkflowController();
controller.setWorkflowService(mockService);
assertThrows(InvalidContentException.class, () -> controller.getFromUrl("https://example.com/bad.yaml"));
}
@Test
public void getFromContentThrowsUnexpectedErrorException() {
WorkflowService mockService = mock(WorkflowService.class);
when(mockService.getFromContent(anyString())).thenThrow(new UnexpectedErrorException());
WorkflowController controller = new WorkflowController();
controller.setWorkflowService(mockService);
assertThrows(UnexpectedErrorException.class, () -> controller.getFromContent("invalid content"));
}
String getContentFromFile() throws Exception {
String filePath = "src/test/resources/pet-coupons.arazzo.yaml";
Path fullPath = Paths.get(filePath).toAbsolutePath();
byte[] encodedBytes = Files.readAllBytes(fullPath);
return new String(encodedBytes, StandardCharsets.UTF_8);
}
}