From 45e3d3ae1cc5a4c29526da4f4cb6096e1b6d7d4f Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Fri, 29 May 2020 16:32:57 -0400 Subject: Use "coder" to serialize Actor requests Modified the Actors to use the "coder" to serialize requests instead of defaulting to the HttpClient serialization provider. Decided to just pretty-print the requests since that can be used for both logging and transmission, which avoids serializing the request twice. Issue-ID: POLICY-2601 Change-Id: I190ed19dd852a1aa66156b358cbc97c3b121af1f Signed-off-by: Jim Hahn --- .../controlloop/actor/so/VfModuleCreate.java | 6 ++-- .../controlloop/actor/so/VfModuleDelete.java | 33 +++---------------- .../controlloop/actor/so/VfModuleDeleteTest.java | 37 ++++------------------ 3 files changed, 15 insertions(+), 61 deletions(-) (limited to 'models-interactions/model-actors/actor.so/src') diff --git a/models-interactions/model-actors/actor.so/src/main/java/org/onap/policy/controlloop/actor/so/VfModuleCreate.java b/models-interactions/model-actors/actor.so/src/main/java/org/onap/policy/controlloop/actor/so/VfModuleCreate.java index 077c8578b..6a6c79279 100644 --- a/models-interactions/model-actors/actor.so/src/main/java/org/onap/policy/controlloop/actor/so/VfModuleCreate.java +++ b/models-interactions/model-actors/actor.so/src/main/java/org/onap/policy/controlloop/actor/so/VfModuleCreate.java @@ -107,10 +107,12 @@ public class VfModuleCreate extends SoOperation { String path = getPath() + pair.getLeft(); SoRequest request = pair.getRight(); - Entity entity = Entity.entity(request, MediaType.APPLICATION_JSON); String url = getClient().getBaseUrl() + path; - logMessage(EventType.OUT, CommInfrastructure.REST, url, request); + String strRequest = prettyPrint(request); + logMessage(EventType.OUT, CommInfrastructure.REST, url, strRequest); + + Entity entity = Entity.entity(strRequest, MediaType.APPLICATION_JSON); Map headers = createSimpleHeaders(); diff --git a/models-interactions/model-actors/actor.so/src/main/java/org/onap/policy/controlloop/actor/so/VfModuleDelete.java b/models-interactions/model-actors/actor.so/src/main/java/org/onap/policy/controlloop/actor/so/VfModuleDelete.java index 5134d58da..04f0287b7 100644 --- a/models-interactions/model-actors/actor.so/src/main/java/org/onap/policy/controlloop/actor/so/VfModuleDelete.java +++ b/models-interactions/model-actors/actor.so/src/main/java/org/onap/policy/controlloop/actor/so/VfModuleDelete.java @@ -45,7 +45,6 @@ import org.onap.policy.aai.AaiCqResponse; import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure; import org.onap.policy.common.endpoints.http.client.HttpClient; import org.onap.policy.common.endpoints.utils.NetLoggerUtil.EventType; -import org.onap.policy.common.utils.coder.CoderException; import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome; import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams; import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig; @@ -118,13 +117,14 @@ public class VfModuleDelete extends SoOperation { SoRequest request = pair.getRight(); String url = getPath() + pair.getLeft(); - logMessage(EventType.OUT, CommInfrastructure.REST, url, request); + String strRequest = prettyPrint(request); + logMessage(EventType.OUT, CommInfrastructure.REST, url, strRequest); Map headers = createSimpleHeaders(); // @formatter:off return handleResponse(outcome, url, - callback -> delete(url, headers, MediaType.APPLICATION_JSON, request, callback)); + callback -> delete(url, headers, MediaType.APPLICATION_JSON, strRequest, callback)); // @formatter:on } @@ -143,12 +143,9 @@ public class VfModuleDelete extends SoOperation { * future will actually cancel the underlying HTTP request */ protected CompletableFuture delete(String uri, Map headers, String contentType, - Q request, InvocationCallback callback) { + String request, InvocationCallback callback) { // TODO move to HttpOperation - // make sure we can encode it before going any further - final String body = encodeRequest(request); - final String url = getClient().getBaseUrl() + uri; Builder builder = HttpRequest.newBuilder(URI.create(url)); @@ -161,7 +158,7 @@ public class VfModuleDelete extends SoOperation { PipelineControllerFuture controller = new PipelineControllerFuture<>(); - HttpRequest req = builder.method("DELETE", BodyPublishers.ofString(body)).build(); + HttpRequest req = builder.method("DELETE", BodyPublishers.ofString(request)).build(); CompletableFuture> future = makeHttpClient().sendAsync(req, BodyHandlers.ofString()); @@ -182,26 +179,6 @@ public class VfModuleDelete extends SoOperation { return controller; } - /** - * Encodes a request. - * - * @param request type - * @param request request to be encoded - * @return the encoded request - */ - protected String encodeRequest(Q request) { - // TODO move to HttpOperation - try { - if (request instanceof String) { - return request.toString(); - } else { - return makeCoder().encode(request); - } - } catch (CoderException e) { - throw new IllegalArgumentException("cannot encode request", e); - } - } - /** * Adds the authorization header to the HTTP request, if configured. * diff --git a/models-interactions/model-actors/actor.so/src/test/java/org/onap/policy/controlloop/actor/so/VfModuleDeleteTest.java b/models-interactions/model-actors/actor.so/src/test/java/org/onap/policy/controlloop/actor/so/VfModuleDeleteTest.java index 86242f042..f5d05a0e8 100644 --- a/models-interactions/model-actors/actor.so/src/test/java/org/onap/policy/controlloop/actor/so/VfModuleDeleteTest.java +++ b/models-interactions/model-actors/actor.so/src/test/java/org/onap/policy/controlloop/actor/so/VfModuleDeleteTest.java @@ -61,9 +61,7 @@ import org.onap.aai.domain.yang.ServiceInstance; import org.onap.aai.domain.yang.Tenant; import org.onap.policy.aai.AaiCqResponse; import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance; -import org.onap.policy.common.utils.coder.Coder; import org.onap.policy.common.utils.coder.CoderException; -import org.onap.policy.common.utils.coder.StandardCoder; import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome; import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams; import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig; @@ -271,8 +269,10 @@ public class VfModuleDeleteTest extends BasicSoOperation { Map headers = Map.of("key-A", "value-A"); + String reqText = oper.prettyPrint(req); + final CompletableFuture delFuture = - oper.delete("my-uri", headers, MediaType.APPLICATION_JSON, req, callback); + oper.delete("my-uri", headers, MediaType.APPLICATION_JSON, reqText, callback); ArgumentCaptor reqCaptor = ArgumentCaptor.forClass(HttpRequest.class); verify(javaClient).sendAsync(reqCaptor.capture(), any()); @@ -311,8 +311,10 @@ public class VfModuleDeleteTest extends BasicSoOperation { SoRequest req = new SoRequest(); req.setRequestId(REQ_ID); + String reqText = oper.prettyPrint(req); + CompletableFuture delFuture = - oper.delete("/my-uri", Map.of(), MediaType.APPLICATION_JSON, req, callback); + oper.delete("/my-uri", Map.of(), MediaType.APPLICATION_JSON, reqText, callback); assertTrue(delFuture.isCompletedExceptionally()); @@ -321,33 +323,6 @@ public class VfModuleDeleteTest extends BasicSoOperation { assertSame(thrown, thrownCaptor.getValue().getCause()); } - @Test - public void testEncodeBody() { - // try when request is already a string - assertEquals("hello", oper.encodeRequest("hello")); - - // try with a real request - SoRequest req = new SoRequest(); - req.setRequestId(REQ_ID); - assertEquals("{\"requestId\":\"" + REQ_ID.toString() + "\"}", oper.encodeRequest(req)); - - // coder throws an exception - oper = new MyOperation(params, config) { - @Override - protected Coder makeCoder() { - return new StandardCoder() { - @Override - public String encode(Object object) throws CoderException { - throw new CoderException(EXPECTED_EXCEPTION); - } - }; - } - }; - - assertThatIllegalArgumentException().isThrownBy(() -> oper.encodeRequest(req)) - .withMessage("cannot encode request"); - } - /** * Tests addAuthHeader() when there is a username, but no password. */ -- cgit 1.2.3-korg