Skip to content
This repository has been archived by the owner on Jul 23, 2024. It is now read-only.

RestAPIWorkFlowTask: Use more reliable URL for the example #251

Merged
merged 1 commit into from
Apr 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions workflow-examples/run_examples.sh
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,7 @@ run_simple_flow() {
"arguments": [
{
"key": "url",
"value": "https://httpbin.org/post"
},
{
"key": "payload",
"value": "'Hello!'"
"value": "http://localhost:8080/actuator/health"
}
]
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@
@Slf4j
public class RestAPIWorkFlowTask extends BaseInfrastructureWorkFlowTask {

private static final String PAYLOAD_KEY = "payload";

private static final String URL_KEY = "url";

/**
Expand All @@ -50,9 +48,8 @@ public class RestAPIWorkFlowTask extends BaseInfrastructureWorkFlowTask {
public WorkReport execute(WorkContext workContext) {
try {
String urlString = getRequiredParameterValue(workContext, URL_KEY);
String payload = getRequiredParameterValue(workContext, PAYLOAD_KEY);
log.info("Running Task REST API Call: urlString: {} payload: {} ", urlString, payload);
ResponseEntity<String> result = RestUtils.executePost(urlString, payload);
log.info("Running Task REST API Call: urlString: {}", urlString);
ResponseEntity<String> result = RestUtils.executeGet(urlString);
if (result.getStatusCode().is2xxSuccessful()) {
log.info("Rest call completed: {}", result.getBody());
return new DefaultWorkReport(WorkStatus.COMPLETED, workContext);
Expand All @@ -70,8 +67,6 @@ public List<WorkParameter> getWorkFlowTaskParameters() {
return List.of(
WorkParameter.builder().key(URL_KEY).description("The Url of the service (ie: https://httpbin.org/post")
.optional(false).type(WorkParameterType.URL).build(),
WorkParameter.builder().key(PAYLOAD_KEY).description("Json of what to provide for data. (ie: 'Hello!')")
.optional(false).type(WorkParameterType.TEXT).build(),
WorkParameter.builder().key("user-id").description("The user id").type(WorkParameterType.TEXT)
.optional(false).build());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,17 @@ public static <T, E> ResponseEntity<E> executePost(String urlString, T requestDt
return restTemplate.postForEntity(urlString, request, responseType);
}

/**
* Execute the GET HTTP method to the given URL
*
* @param urlString the URL
* @return HTTP response as string
*/
public static ResponseEntity<String> executeGet(String urlString) {
RestTemplate restTemplate = new RestTemplate();
return restTemplate.getForEntity(urlString, String.class);
}

/**
* Execute the GET HTTP method to the given URL, writing the given request entity to
* the request, and returns the response as ResponseEntity.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,7 @@ public void runSimpleWorkFlow() throws ApiException, InterruptedException {
// Define WorkRequests
WorkRequestDTO work1 = new WorkRequestDTO();
work1.setWorkName("restCallTask");
work1.setArguments(Arrays.asList(new ArgumentRequestDTO().key("url").value("https://httpbin.org/post"),
new ArgumentRequestDTO().key("payload").value("'Hello!'")));
work1.setArguments(Arrays.asList(new ArgumentRequestDTO().key("url").value("http://localhost:8080/actuator/health")));

WorkRequestDTO work2 = new WorkRequestDTO();
work2.setWorkName("loggingTask");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public void executeSuccess() {
// given
WorkContext workContext = Mockito.mock(WorkContext.class);
try (MockedStatic<RestUtils> restUtilsMockedStatic = Mockito.mockStatic(RestUtils.class)) {
restUtilsMockedStatic.when(() -> RestUtils.executePost(eq(valueUrl), eq(valuePayload)))
restUtilsMockedStatic.when(() -> RestUtils.executeGet(eq(valueUrl)))
.thenReturn(ResponseEntity.ok().build());
// when
WorkReport workReport = restAPIWorkflowTask.execute(workContext);
Expand Down Expand Up @@ -102,13 +102,10 @@ public void testGetWorkFlowTaskParameters() {

// then
assertNotNull(workParameters);
assertEquals(3, workParameters.size());
assertEquals(2, workParameters.size());
assertEquals(URL_KEY, workParameters.get(0).getKey());
assertEquals("The Url of the service (ie: https://httpbin.org/post", workParameters.get(0).getDescription());
assertEquals(WorkParameterType.URL, workParameters.get(0).getType());
assertEquals(PAYLOAD_KEY, workParameters.get(1).getKey());
assertEquals("Json of what to provide for data. (ie: 'Hello!')", workParameters.get(1).getDescription());
assertEquals(WorkParameterType.TEXT, workParameters.get(1).getType());
}

@Test
Expand Down