This repository has been archived by the owner on Jul 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FLPATH-144 https://issues.redhat.com/browse/FLPATH-214 Signed-off-by: Yaron Dayagi <ydayagi@redhat.com>
- Loading branch information
1 parent
743d180
commit db8e234
Showing
6 changed files
with
404 additions
and
0 deletions.
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/rest/RestService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.redhat.parodos.tasks.rest; | ||
|
||
import org.springframework.http.HttpEntity; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.client.RestClientException; | ||
|
||
interface RestService { | ||
|
||
ResponseEntity<String> exchange(String url, HttpMethod method, HttpEntity<String> requestEntity) | ||
throws RestClientException; | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/rest/RestServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.redhat.parodos.tasks.rest; | ||
|
||
import org.springframework.http.HttpEntity; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.client.RestClientException; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
class RestServiceImpl implements RestService { | ||
|
||
@Override | ||
public ResponseEntity<String> exchange(String url, HttpMethod method, HttpEntity<String> requestEntity) | ||
throws RestClientException { | ||
return new RestTemplate().exchange(url, method, requestEntity, String.class); | ||
} | ||
|
||
} |
127 changes: 127 additions & 0 deletions
127
prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/rest/RestWorkFlowTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
package com.redhat.parodos.tasks.rest; | ||
|
||
import java.util.LinkedList; | ||
import java.util.List; | ||
|
||
import com.redhat.parodos.workflow.exception.MissingParameterException; | ||
import com.redhat.parodos.workflow.parameter.WorkParameter; | ||
import com.redhat.parodos.workflow.parameter.WorkParameterType; | ||
import com.redhat.parodos.workflow.task.BaseWorkFlowTask; | ||
import com.redhat.parodos.workflow.task.enums.WorkFlowTaskOutput; | ||
import com.redhat.parodos.workflows.work.DefaultWorkReport; | ||
import com.redhat.parodos.workflows.work.WorkContext; | ||
import com.redhat.parodos.workflows.work.WorkReport; | ||
import com.redhat.parodos.workflows.work.WorkStatus; | ||
import lombok.NonNull; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import org.springframework.http.HttpEntity; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.client.RestClientException; | ||
|
||
@Slf4j | ||
public class RestWorkFlowTask extends BaseWorkFlowTask { | ||
|
||
private RestService restService; | ||
|
||
public RestWorkFlowTask() { | ||
restService = new RestServiceImpl(); | ||
} | ||
|
||
RestWorkFlowTask(String beanName, RestService restService) { | ||
this.restService = restService; | ||
setBeanName(beanName); | ||
} | ||
|
||
@Override | ||
public @NonNull List<WorkParameter> getWorkFlowTaskParameters() { | ||
LinkedList<WorkParameter> params = new LinkedList<>(); | ||
params.add(WorkParameter.builder().key("url").type(WorkParameterType.TEXT).optional(false) | ||
.description("URL to send request to").build()); | ||
params.add(WorkParameter.builder().key("method").type(WorkParameterType.TEXT).optional(false) | ||
.description("The HTTP method").build()); | ||
params.add(WorkParameter.builder().key("content").type(WorkParameterType.TEXT).optional(true) | ||
.description("The content of the HTTP request").build()); | ||
params.add(WorkParameter.builder().key("username").type(WorkParameterType.TEXT).optional(true) | ||
.description("Username for basic HTTP authentication").build()); | ||
params.add(WorkParameter.builder().key("password").type(WorkParameterType.TEXT).optional(true) | ||
.description("Password for basic HTTP authentication").build()); | ||
params.add(WorkParameter.builder().key("response-key").type(WorkParameterType.TEXT).optional(true) | ||
.description("The content of the response will be stored in this key").build()); | ||
return params; | ||
} | ||
|
||
@Override | ||
public @NonNull List<WorkFlowTaskOutput> getWorkFlowTaskOutputs() { | ||
return List.of(WorkFlowTaskOutput.OTHER); | ||
} | ||
|
||
@Override | ||
public WorkReport execute(WorkContext workContext) { | ||
String url = ""; | ||
try { | ||
url = getRequiredParameterValue(workContext, "url"); | ||
String method = getRequiredParameterValue(workContext, "method"); | ||
|
||
HttpMethod httpMethod = HttpMethod.valueOf(method.toUpperCase()); | ||
|
||
ResponseEntity<String> responseEntity = restService.exchange(url, httpMethod, | ||
buildRequestEntity(workContext)); | ||
|
||
if (!responseEntity.getStatusCode().is2xxSuccessful()) { | ||
throw new RestClientException( | ||
"Request failed with HTTP status code " + responseEntity.getStatusCodeValue()); | ||
} | ||
|
||
processResponseEntity(workContext, responseEntity); | ||
|
||
} | ||
catch (MissingParameterException | IllegalArgumentException e) { | ||
log.error("Rest task failed for url " + url, e); | ||
return new DefaultWorkReport(WorkStatus.FAILED, workContext, e); | ||
} | ||
|
||
return new DefaultWorkReport(WorkStatus.COMPLETED, workContext); | ||
} | ||
|
||
protected void processResponseEntity(WorkContext workContext, ResponseEntity<String> responseEntity) | ||
throws RestClientException { | ||
String responseKey = getOptionalParameterValue(workContext, "response-key", ""); | ||
|
||
if (responseKey.isEmpty()) { | ||
return; | ||
} | ||
|
||
workContext.put(responseKey, responseEntity.getBody()); | ||
} | ||
|
||
protected HttpEntity<String> buildRequestEntity(WorkContext workContext) throws RestClientException { | ||
String content = getOptionalParameterValue(workContext, "content", ""); | ||
return new HttpEntity<>(content, buildHttpHeaders(workContext)); | ||
} | ||
|
||
protected HttpHeaders buildHttpHeaders(WorkContext workContext) throws RestClientException { | ||
HttpHeaders httpHeaders = new HttpHeaders(); | ||
|
||
httpHeaders.setContentType(MediaType.APPLICATION_JSON); | ||
httpHeaders.setAccept(List.of(MediaType.APPLICATION_JSON)); | ||
|
||
String username = getOptionalParameterValue(workContext, "username", ""); | ||
String password; | ||
|
||
if (!username.isEmpty()) { | ||
try { | ||
password = getRequiredParameterValue(workContext, "password"); | ||
} | ||
catch (MissingParameterException e) { | ||
throw new RestClientException("Missing password", e); | ||
} | ||
httpHeaders.setBasicAuth(username, password); | ||
} | ||
return httpHeaders; | ||
} | ||
|
||
} |
145 changes: 145 additions & 0 deletions
145
prebuilt-tasks/src/test/java/com/redhat/parodos/tasks/rest/RestWorkFlowTaskTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
package com.redhat.parodos.tasks.rest; | ||
|
||
import java.util.HashMap; | ||
|
||
import com.redhat.parodos.workflow.context.WorkContextDelegate; | ||
import com.redhat.parodos.workflow.exception.MissingParameterException; | ||
import com.redhat.parodos.workflows.work.WorkContext; | ||
import com.redhat.parodos.workflows.work.WorkReport; | ||
import com.redhat.parodos.workflows.work.WorkStatus; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.mockito.Mockito; | ||
import org.mockito.invocation.InvocationOnMock; | ||
import org.mockito.stubbing.Answer; | ||
|
||
import org.springframework.http.HttpEntity; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.client.RestClientException; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.assertThrows; | ||
import static org.junit.Assert.assertTrue; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.doAnswer; | ||
import static org.mockito.Mockito.doReturn; | ||
|
||
public class RestWorkFlowTaskTest { | ||
|
||
private RestService restService = Mockito.mock(RestService.class); | ||
|
||
private RestWorkFlowTask task = new RestWorkFlowTask("Test", restService); | ||
|
||
HashMap<String, String> map; | ||
|
||
@Before | ||
public void setUp() { | ||
map = new HashMap<>(); | ||
} | ||
|
||
@Test | ||
public void missingUrl() { | ||
map.put("method", "get"); | ||
|
||
WorkReport result = task.execute(createWorkContext(map)); | ||
|
||
assertEquals(WorkStatus.FAILED, result.getStatus()); | ||
assertEquals(MissingParameterException.class, result.getError().getClass()); | ||
} | ||
|
||
@Test | ||
public void missingMethod() { | ||
map.put("url", "http://localhost"); | ||
|
||
WorkReport result = task.execute(createWorkContext(map)); | ||
|
||
assertEquals(WorkStatus.FAILED, result.getStatus()); | ||
assertEquals(MissingParameterException.class, result.getError().getClass()); | ||
} | ||
|
||
@Test | ||
public void invalidMethod() { | ||
map.put("url", "http://localhost"); | ||
map.put("method", "drop"); | ||
|
||
WorkReport result = task.execute(createWorkContext(map)); | ||
|
||
assertEquals(WorkStatus.FAILED, result.getStatus()); | ||
assertEquals(IllegalArgumentException.class, result.getError().getClass()); | ||
} | ||
|
||
@Test | ||
public void get() { | ||
map.put("url", "http://localhost"); | ||
map.put("method", "get"); | ||
map.put("response-key", "http-body"); | ||
|
||
ResponseEntity<String> obj = new ResponseEntity<>("body", HttpStatus.OK); | ||
|
||
doReturn(obj).when(restService).exchange(any(), any(), any()); | ||
|
||
WorkContext ctx = createWorkContext(map); | ||
|
||
WorkReport result = task.execute(ctx); | ||
|
||
assertEquals(WorkStatus.COMPLETED, result.getStatus()); | ||
assertEquals(obj.getBody(), ctx.get("http-body")); | ||
} | ||
|
||
@Test | ||
public void post() { | ||
map.put("url", "http://localhost"); | ||
map.put("method", "post"); | ||
map.put("content", "{\"root\": \"value\"}"); | ||
map.put("username", "test"); | ||
map.put("password", "test"); | ||
|
||
ResponseEntity<String> obj = new ResponseEntity<>(HttpStatus.OK); | ||
|
||
doAnswer(new Answer() { | ||
@Override | ||
public Object answer(InvocationOnMock invocationOnMock) throws Throwable { | ||
HttpEntity<String> request = invocationOnMock.getArgument(2); | ||
HttpHeaders headers = request.getHeaders(); | ||
|
||
assertEquals(map.get("content"), request.getBody()); | ||
assertEquals(1, headers.getAccept().size()); | ||
assertNotNull(headers.getContentType()); | ||
assertTrue(headers.containsKey("Authorization")); | ||
|
||
return obj; | ||
} | ||
}).when(restService).exchange(any(), any(), any()); | ||
|
||
WorkContext ctx = createWorkContext(map); | ||
|
||
WorkReport result = task.execute(ctx); | ||
|
||
assertEquals(WorkStatus.COMPLETED, result.getStatus()); | ||
} | ||
|
||
@Test | ||
public void requestNot2xx() { | ||
map.put("url", "http://localhost"); | ||
map.put("method", "get"); | ||
|
||
ResponseEntity<String> obj = new ResponseEntity<>(HttpStatus.MOVED_PERMANENTLY); | ||
|
||
doReturn(obj).when(restService).exchange(any(), any(), any()); | ||
|
||
WorkContext ctx = createWorkContext(map); | ||
|
||
assertThrows(RestClientException.class, () -> task.execute(ctx)); | ||
} | ||
|
||
private WorkContext createWorkContext(HashMap<String, String> map) { | ||
WorkContext ctx = new WorkContext(); | ||
WorkContextDelegate.write(ctx, WorkContextDelegate.ProcessType.WORKFLOW_TASK_EXECUTION, task.getName(), | ||
WorkContextDelegate.Resource.ARGUMENTS, map); | ||
return ctx; | ||
} | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
workflow-examples/src/main/java/com/redhat/parodos/examples/RestWorkFlowConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.redhat.parodos.examples; | ||
|
||
import com.redhat.parodos.tasks.rest.RestWorkFlowTask; | ||
import com.redhat.parodos.workflow.annotation.Infrastructure; | ||
import com.redhat.parodos.workflows.workflow.SequentialFlow; | ||
import com.redhat.parodos.workflows.workflow.WorkFlow; | ||
|
||
import org.springframework.beans.factory.annotation.Qualifier; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.Profile; | ||
|
||
@Configuration | ||
@Profile("rest") | ||
public class RestWorkFlowConfiguration { | ||
|
||
@Bean | ||
RestWorkFlowTask restTask() { | ||
return new RestWorkFlowTask(); | ||
} | ||
|
||
@Bean | ||
@Infrastructure | ||
WorkFlow restWorkFlow(@Qualifier("restTask") RestWorkFlowTask restTask) { | ||
return SequentialFlow.Builder.aNewSequentialFlow().named("restWorkFlow").execute(restTask).build(); | ||
} | ||
|
||
} |
Oops, something went wrong.