From e44f1a1c58efed9fbe2efce78864aaee3b577003 Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Mon, 17 Feb 2020 13:57:56 -0500 Subject: More changes to actor code Use Coder.convert() from policy-common. Passed response to setOutcome(). Changed class names from XxxOperator to XxxOperation. Modified SDNC junits to invoke start() instead of startOperationAsync(). Changed context obtain() to re-run if the future was canceled. Added junit support class, BasicBidirectionalTopicOperation. Modified HttpOperation to allow subsequent requests to be issued. Some actors, like SO, send an initial HTTP request and then follow it with HTTP "are you done?" requests. Issue-ID: POLICY-2363-prop Change-Id: I12b5c2d4f07254e0cb79fabfe1ccf844b70a0654 Signed-off-by: Jim Hahn --- .../actor/aai/AaiCustomQueryOperation.java | 5 +- .../controlloop/actor/aai/AaiGetOperation.java | 6 +- .../actor/aai/AaiCustomQueryOperationTest.java | 2 +- .../controlloop/actor/aai/AaiGetOperationTest.java | 137 +++++++++++++++++++++ .../controlloop/actor/aai/AaiGetOperatorTest.java | 137 --------------------- .../policy/controlloop/actor/aai/AaiUtilTest.java | 2 +- .../controlloop/actor/aai/BasicAaiOperation.java | 54 ++++++++ .../controlloop/actor/aai/BasicAaiOperator.java | 54 -------- 8 files changed, 201 insertions(+), 196 deletions(-) create mode 100644 models-interactions/model-actors/actor.aai/src/test/java/org/onap/policy/controlloop/actor/aai/AaiGetOperationTest.java delete mode 100644 models-interactions/model-actors/actor.aai/src/test/java/org/onap/policy/controlloop/actor/aai/AaiGetOperatorTest.java create mode 100644 models-interactions/model-actors/actor.aai/src/test/java/org/onap/policy/controlloop/actor/aai/BasicAaiOperation.java delete mode 100644 models-interactions/model-actors/actor.aai/src/test/java/org/onap/policy/controlloop/actor/aai/BasicAaiOperator.java (limited to 'models-interactions/model-actors/actor.aai/src') diff --git a/models-interactions/model-actors/actor.aai/src/main/java/org/onap/policy/controlloop/actor/aai/AaiCustomQueryOperation.java b/models-interactions/model-actors/actor.aai/src/main/java/org/onap/policy/controlloop/actor/aai/AaiCustomQueryOperation.java index bc2dde9d8..e32734b7d 100644 --- a/models-interactions/model-actors/actor.aai/src/main/java/org/onap/policy/controlloop/actor/aai/AaiCustomQueryOperation.java +++ b/models-interactions/model-actors/actor.aai/src/main/java/org/onap/policy/controlloop/actor/aai/AaiCustomQueryOperation.java @@ -121,9 +121,12 @@ public class AaiCustomQueryOperation extends HttpOperation { * Injects the response into the context. */ @Override - protected void postProcessResponse(OperationOutcome outcome, String url, Response rawResponse, String response) { + protected CompletableFuture postProcessResponse(OperationOutcome outcome, String url, + Response rawResponse, String response) { logger.info("{}: caching response for {}", getFullName(), params.getRequestId()); params.getContext().setProperty(AaiCqResponse.CONTEXT_KEY, new AaiCqResponse(response)); + + return super.postProcessResponse(outcome, url, rawResponse, response); } } diff --git a/models-interactions/model-actors/actor.aai/src/main/java/org/onap/policy/controlloop/actor/aai/AaiGetOperation.java b/models-interactions/model-actors/actor.aai/src/main/java/org/onap/policy/controlloop/actor/aai/AaiGetOperation.java index 60a28209b..ee1c4612d 100644 --- a/models-interactions/model-actors/actor.aai/src/main/java/org/onap/policy/controlloop/actor/aai/AaiGetOperation.java +++ b/models-interactions/model-actors/actor.aai/src/main/java/org/onap/policy/controlloop/actor/aai/AaiGetOperation.java @@ -116,13 +116,15 @@ public class AaiGetOperation extends HttpOperation { * Injects the response into the context. */ @Override - protected void postProcessResponse(OperationOutcome outcome, String url, Response rawResponse, - StandardCoderObject response) { + protected CompletableFuture postProcessResponse(OperationOutcome outcome, String url, + Response rawResponse, StandardCoderObject response) { String entity = params.getTargetEntity(); logger.info("{}: caching response of {} for {}", getFullName(), entity, params.getRequestId()); params.getContext().setProperty(propertyPrefix + entity, response); + + return super.postProcessResponse(outcome, url, rawResponse, response); } /** diff --git a/models-interactions/model-actors/actor.aai/src/test/java/org/onap/policy/controlloop/actor/aai/AaiCustomQueryOperationTest.java b/models-interactions/model-actors/actor.aai/src/test/java/org/onap/policy/controlloop/actor/aai/AaiCustomQueryOperationTest.java index c95425e7a..a93508757 100644 --- a/models-interactions/model-actors/actor.aai/src/test/java/org/onap/policy/controlloop/actor/aai/AaiCustomQueryOperationTest.java +++ b/models-interactions/model-actors/actor.aai/src/test/java/org/onap/policy/controlloop/actor/aai/AaiCustomQueryOperationTest.java @@ -50,7 +50,7 @@ import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpParams; import org.onap.policy.controlloop.actorserviceprovider.spi.Actor; import org.onap.policy.controlloop.policy.PolicyResult; -public class AaiCustomQueryOperationTest extends BasicAaiOperator> { +public class AaiCustomQueryOperationTest extends BasicAaiOperation> { private static final StandardCoder coder = new StandardCoder(); private static final String MY_LINK = "my-link"; diff --git a/models-interactions/model-actors/actor.aai/src/test/java/org/onap/policy/controlloop/actor/aai/AaiGetOperationTest.java b/models-interactions/model-actors/actor.aai/src/test/java/org/onap/policy/controlloop/actor/aai/AaiGetOperationTest.java new file mode 100644 index 000000000..654864246 --- /dev/null +++ b/models-interactions/model-actors/actor.aai/src/test/java/org/onap/policy/controlloop/actor/aai/AaiGetOperationTest.java @@ -0,0 +1,137 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.controlloop.actor.aai; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; + +import java.util.Map; +import java.util.concurrent.CompletableFuture; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.aai.AaiConstants; +import org.onap.policy.common.utils.coder.StandardCoder; +import org.onap.policy.common.utils.coder.StandardCoderObject; +import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome; +import org.onap.policy.controlloop.policy.PolicyResult; + +public class AaiGetOperationTest extends BasicAaiOperation { + + private static final String INPUT_FIELD = "input"; + private static final String TEXT = "my-text"; + + private AaiGetOperation oper; + + public AaiGetOperationTest() { + super(AaiConstants.ACTOR_NAME, AaiGetOperation.TENANT); + } + + /** + * Sets up. + */ + @Before + public void setUp() throws Exception { + super.setUp(); + oper = new AaiGetOperation(params, operator); + } + + @Test + public void testGetRetry() { + // use default if null retry + assertEquals(AaiGetOperation.DEFAULT_RETRY, oper.getRetry(null)); + + // otherwise, use specified value + assertEquals(0, oper.getRetry(0)); + assertEquals(10, oper.getRetry(10)); + } + + @Test + public void testStartOperationAsync_testStartQueryAsync_testPostProcessResponse() throws Exception { + + // return a map in the reply + Map reply = Map.of(INPUT_FIELD, TEXT); + when(rawResponse.readEntity(String.class)).thenReturn(new StandardCoder().encode(reply)); + + when(client.get(any(), any(), any())).thenAnswer(provideResponse(rawResponse)); + + CompletableFuture future2 = oper.startOperationAsync(1, outcome); + assertFalse(future2.isDone()); + + executor.runAll(100); + assertTrue(future2.isDone()); + + assertEquals(PolicyResult.SUCCESS, future2.get().getResult()); + + // data should have been cached within the context + StandardCoderObject data = context.getProperty(AaiGetOperation.getTenantKey(TARGET_ENTITY)); + assertNotNull(data); + assertEquals(TEXT, data.getString(INPUT_FIELD)); + } + + /** + * Tests startOperationAsync() when there's a failure. + */ + @Test + public void testStartOperationAsyncFailure() throws Exception { + + when(rawResponse.getStatus()).thenReturn(500); + when(rawResponse.readEntity(String.class)).thenReturn(""); + + when(client.get(any(), any(), any())).thenAnswer(provideResponse(rawResponse)); + + CompletableFuture future2 = oper.startOperationAsync(1, outcome); + assertFalse(future2.isDone()); + + executor.runAll(100); + assertTrue(future2.isDone()); + + assertEquals(PolicyResult.FAILURE, future2.get().getResult()); + + // data should NOT have been cached within the context + assertNull(context.getProperty(AaiGetOperation.getTenantKey(TARGET_ENTITY))); + } + + @Test + public void testMakeHeaders() { + verifyHeaders(oper.makeHeaders()); + } + + @Test + public void testMakePath() { + assertEquals(PATH + "/" + TARGET_ENTITY, oper.makePath()); + } + + @Test + public void testAaiGetOperator() { + assertEquals(AaiConstants.ACTOR_NAME, oper.getActorName()); + assertEquals(AaiGetOperation.TENANT, oper.getName()); + } + + @Test + public void testGetTenantKey() { + assertEquals("AAI.Tenant." + TARGET_ENTITY, AaiGetOperation.getTenantKey(TARGET_ENTITY)); + } +} diff --git a/models-interactions/model-actors/actor.aai/src/test/java/org/onap/policy/controlloop/actor/aai/AaiGetOperatorTest.java b/models-interactions/model-actors/actor.aai/src/test/java/org/onap/policy/controlloop/actor/aai/AaiGetOperatorTest.java deleted file mode 100644 index ebe953570..000000000 --- a/models-interactions/model-actors/actor.aai/src/test/java/org/onap/policy/controlloop/actor/aai/AaiGetOperatorTest.java +++ /dev/null @@ -1,137 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP - * ================================================================================ - * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.controlloop.actor.aai; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.when; - -import java.util.Map; -import java.util.concurrent.CompletableFuture; -import org.junit.Before; -import org.junit.Test; -import org.onap.policy.aai.AaiConstants; -import org.onap.policy.common.utils.coder.StandardCoder; -import org.onap.policy.common.utils.coder.StandardCoderObject; -import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome; -import org.onap.policy.controlloop.policy.PolicyResult; - -public class AaiGetOperatorTest extends BasicAaiOperator { - - private static final String INPUT_FIELD = "input"; - private static final String TEXT = "my-text"; - - private AaiGetOperation oper; - - public AaiGetOperatorTest() { - super(AaiConstants.ACTOR_NAME, AaiGetOperation.TENANT); - } - - /** - * Sets up. - */ - @Before - public void setUp() throws Exception { - super.setUp(); - oper = new AaiGetOperation(params, operator); - } - - @Test - public void testGetRetry() { - // use default if null retry - assertEquals(AaiGetOperation.DEFAULT_RETRY, oper.getRetry(null)); - - // otherwise, use specified value - assertEquals(0, oper.getRetry(0)); - assertEquals(10, oper.getRetry(10)); - } - - @Test - public void testStartOperationAsync_testStartQueryAsync_testPostProcessResponse() throws Exception { - - // return a map in the reply - Map reply = Map.of(INPUT_FIELD, TEXT); - when(rawResponse.readEntity(String.class)).thenReturn(new StandardCoder().encode(reply)); - - when(client.get(any(), any(), any())).thenAnswer(provideResponse(rawResponse)); - - CompletableFuture future2 = oper.startOperationAsync(1, outcome); - assertFalse(future2.isDone()); - - executor.runAll(100); - assertTrue(future2.isDone()); - - assertEquals(PolicyResult.SUCCESS, future2.get().getResult()); - - // data should have been cached within the context - StandardCoderObject data = context.getProperty(AaiGetOperation.getTenantKey(TARGET_ENTITY)); - assertNotNull(data); - assertEquals(TEXT, data.getString(INPUT_FIELD)); - } - - /** - * Tests startOperationAsync() when there's a failure. - */ - @Test - public void testStartOperationAsyncFailure() throws Exception { - - when(rawResponse.getStatus()).thenReturn(500); - when(rawResponse.readEntity(String.class)).thenReturn(""); - - when(client.get(any(), any(), any())).thenAnswer(provideResponse(rawResponse)); - - CompletableFuture future2 = oper.startOperationAsync(1, outcome); - assertFalse(future2.isDone()); - - executor.runAll(100); - assertTrue(future2.isDone()); - - assertEquals(PolicyResult.FAILURE, future2.get().getResult()); - - // data should NOT have been cached within the context - assertNull(context.getProperty(AaiGetOperation.getTenantKey(TARGET_ENTITY))); - } - - @Test - public void testMakeHeaders() { - verifyHeaders(oper.makeHeaders()); - } - - @Test - public void testMakePath() { - assertEquals(PATH + "/" + TARGET_ENTITY, oper.makePath()); - } - - @Test - public void testAaiGetOperator() { - assertEquals(AaiConstants.ACTOR_NAME, oper.getActorName()); - assertEquals(AaiGetOperation.TENANT, oper.getName()); - } - - @Test - public void testGetTenantKey() { - assertEquals("AAI.Tenant." + TARGET_ENTITY, AaiGetOperation.getTenantKey(TARGET_ENTITY)); - } -} diff --git a/models-interactions/model-actors/actor.aai/src/test/java/org/onap/policy/controlloop/actor/aai/AaiUtilTest.java b/models-interactions/model-actors/actor.aai/src/test/java/org/onap/policy/controlloop/actor/aai/AaiUtilTest.java index 39ed6fe88..ae38cca35 100644 --- a/models-interactions/model-actors/actor.aai/src/test/java/org/onap/policy/controlloop/actor/aai/AaiUtilTest.java +++ b/models-interactions/model-actors/actor.aai/src/test/java/org/onap/policy/controlloop/actor/aai/AaiUtilTest.java @@ -23,7 +23,7 @@ package org.onap.policy.controlloop.actor.aai; import java.util.Map; import org.junit.Test; -public class AaiUtilTest extends BasicAaiOperator { +public class AaiUtilTest extends BasicAaiOperation { @Test public void testMakeHeaders() { diff --git a/models-interactions/model-actors/actor.aai/src/test/java/org/onap/policy/controlloop/actor/aai/BasicAaiOperation.java b/models-interactions/model-actors/actor.aai/src/test/java/org/onap/policy/controlloop/actor/aai/BasicAaiOperation.java new file mode 100644 index 000000000..00485c935 --- /dev/null +++ b/models-interactions/model-actors/actor.aai/src/test/java/org/onap/policy/controlloop/actor/aai/BasicAaiOperation.java @@ -0,0 +1,54 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.controlloop.actor.aai; + +import static org.junit.Assert.assertEquals; + +import java.util.Map; +import org.onap.policy.controlloop.actor.test.BasicHttpOperation; + +/** + * Superclass for various operator tests. + */ +public abstract class BasicAaiOperation extends BasicHttpOperation { + + /** + * Constructs the object using a default actor and operation name. + */ + public BasicAaiOperation() { + super(); + } + + /** + * Constructs the object. + * + * @param actor actor name + * @param operation operation name + */ + public BasicAaiOperation(String actor, String operation) { + super(actor, operation); + } + + protected void verifyHeaders(Map headers) { + assertEquals("POLICY", headers.get("X-FromAppId").toString()); + assertEquals(params.getRequestId().toString(), headers.get("X-TransactionId")); + } +} diff --git a/models-interactions/model-actors/actor.aai/src/test/java/org/onap/policy/controlloop/actor/aai/BasicAaiOperator.java b/models-interactions/model-actors/actor.aai/src/test/java/org/onap/policy/controlloop/actor/aai/BasicAaiOperator.java deleted file mode 100644 index 50b562afb..000000000 --- a/models-interactions/model-actors/actor.aai/src/test/java/org/onap/policy/controlloop/actor/aai/BasicAaiOperator.java +++ /dev/null @@ -1,54 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP - * ================================================================================ - * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.controlloop.actor.aai; - -import static org.junit.Assert.assertEquals; - -import java.util.Map; -import org.onap.policy.controlloop.actor.test.BasicHttpOperation; - -/** - * Superclass for various operator tests. - */ -public abstract class BasicAaiOperator extends BasicHttpOperation { - - /** - * Constructs the object using a default actor and operation name. - */ - public BasicAaiOperator() { - super(); - } - - /** - * Constructs the object. - * - * @param actor actor name - * @param operation operation name - */ - public BasicAaiOperator(String actor, String operation) { - super(actor, operation); - } - - protected void verifyHeaders(Map headers) { - assertEquals("POLICY", headers.get("X-FromAppId").toString()); - assertEquals(params.getRequestId().toString(), headers.get("X-TransactionId")); - } -} -- cgit 1.2.3-korg