From 3b4884e5688a71116b3935e3a4ad243ab6287c80 Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Wed, 12 Feb 2020 13:01:57 -0500 Subject: Add A&AI actor and some operators Added A&AI Actor, as well as operators for the "custom query" and the "tenant" query (on which the custom query depends). Issue-ID: POLICY-1625 Signed-off-by: Jim Hahn Change-Id: Ic9dadc07a6057cb1abb9698da86eb9431fc9ed3e --- .../actor/aai/AaiActorServiceProviderTest.java | 48 ++++++ .../actor/aai/AaiCustomQueryOperationTest.java | 189 +++++++++++++++++++++ .../controlloop/actor/aai/AaiGetOperatorTest.java | 131 ++++++++++++++ .../policy/controlloop/actor/aai/AaiUtilTest.java | 36 ++++ .../controlloop/actor/aai/BasicAaiOperator.java | 54 ++++++ 5 files changed, 458 insertions(+) create mode 100644 models-interactions/model-actors/actor.aai/src/test/java/org/onap/policy/controlloop/actor/aai/AaiActorServiceProviderTest.java create mode 100644 models-interactions/model-actors/actor.aai/src/test/java/org/onap/policy/controlloop/actor/aai/AaiCustomQueryOperationTest.java create 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/AaiUtilTest.java create 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/test') diff --git a/models-interactions/model-actors/actor.aai/src/test/java/org/onap/policy/controlloop/actor/aai/AaiActorServiceProviderTest.java b/models-interactions/model-actors/actor.aai/src/test/java/org/onap/policy/controlloop/actor/aai/AaiActorServiceProviderTest.java new file mode 100644 index 000000000..513f339fb --- /dev/null +++ b/models-interactions/model-actors/actor.aai/src/test/java/org/onap/policy/controlloop/actor/aai/AaiActorServiceProviderTest.java @@ -0,0 +1,48 @@ +/*- + * ============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.Collections; +import java.util.LinkedList; +import java.util.List; +import java.util.stream.Collectors; +import org.junit.Test; + +public class AaiActorServiceProviderTest { + + @Test + public void testAaiActorServiceProvider() { + final AaiActorServiceProvider prov = new AaiActorServiceProvider(); + + // verify that it has the operators we expect + List expected = new LinkedList<>(); + expected.add(AaiCustomQueryOperation.NAME); + expected.addAll(AaiGetOperation.OPERATIONS); + + Collections.sort(expected); + + var actual = prov.getOperationNames().stream().sorted().collect(Collectors.toList()); + + assertEquals(expected.toString(), actual.toString()); + } +} 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 new file mode 100644 index 000000000..eca5062f1 --- /dev/null +++ b/models-interactions/model-actors/actor.aai/src/test/java/org/onap/policy/controlloop/actor/aai/AaiCustomQueryOperationTest.java @@ -0,0 +1,189 @@ +/*- + * ============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.assertNotNull; +import static org.junit.Assert.assertSame; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; + +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.TimeUnit; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.onap.policy.aai.AaiConstants; +import org.onap.policy.aai.AaiCqResponse; +import org.onap.policy.common.endpoints.http.client.HttpClientFactory; +import org.onap.policy.common.utils.coder.StandardCoder; +import org.onap.policy.common.utils.coder.StandardCoderObject; +import org.onap.policy.controlloop.actorserviceprovider.Operation; +import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome; +import org.onap.policy.controlloop.actorserviceprovider.Util; +import org.onap.policy.controlloop.actorserviceprovider.impl.HttpOperator; +import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams; +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> { + private static final StandardCoder coder = new StandardCoder(); + + private static final String MY_LINK = "my-link"; + + @Mock + private Actor tenantActor; + + private AaiCustomQueryOperation oper; + + public AaiCustomQueryOperationTest() { + super(AaiConstants.ACTOR_NAME, AaiCustomQueryOperation.NAME); + } + + /** + * Sets up. + */ + @Before + public void setUp() throws Exception { + super.setUp(); + + MyTenantOperator tenantOperator = new MyTenantOperator(); + + when(service.getActor(AaiConstants.ACTOR_NAME)).thenReturn(tenantActor); + when(tenantActor.getOperator(AaiGetOperation.TENANT)).thenReturn(tenantOperator); + + oper = new AaiCustomQueryOperation(params, operator); + } + + @Test + public void testAaiCustomQueryOperation() { + assertEquals(AaiConstants.ACTOR_NAME, oper.getActorName()); + assertEquals(AaiCustomQueryOperation.NAME, oper.getName()); + } + + @Test + public void testStartOperationAsync_testStartPreprocessorAsync_testMakeRequest_testPostProcess() throws Exception { + // need two responses + when(rawResponse.readEntity(String.class)).thenReturn(makeTenantReply()).thenReturn(makeCqReply()); + when(client.get(any(), any(), any())).thenAnswer(provideResponse(rawResponse)); + when(client.put(any(), any(), any(), any())).thenAnswer(provideResponse(rawResponse)); + + CompletableFuture future2 = oper.start(); + + assertEquals(PolicyResult.SUCCESS, future2.get(5, TimeUnit.SECONDS).getResult()); + + // tenant response should have been cached within the context + assertNotNull(context.getProperty(AaiGetOperation.getTenantKey(TARGET_ENTITY))); + + // custom query response should have been cached within the context + AaiCqResponse cqData = context.getProperty(AaiCqResponse.CONTEXT_KEY); + assertNotNull(cqData); + } + + /** + * Tests when preprocessor step is not needed. + */ + @Test + public void testStartOperationAsync_testStartPreprocessorAsyncNotNeeded() throws Exception { + // pre-load the tenant data + final StandardCoderObject data = preloadTenantData(); + + // only need one response + when(rawResponse.readEntity(String.class)).thenReturn(makeCqReply()); + when(client.put(any(), any(), any(), any())).thenAnswer(provideResponse(rawResponse)); + + CompletableFuture future2 = oper.start(); + + assertEquals(PolicyResult.SUCCESS, future2.get(5, TimeUnit.SECONDS).getResult()); + + // should not have replaced tenant response + assertSame(data, context.getProperty(AaiGetOperation.getTenantKey(TARGET_ENTITY))); + + // custom query response should have been cached within the context + AaiCqResponse cqData = context.getProperty(AaiCqResponse.CONTEXT_KEY); + assertNotNull(cqData); + } + + @Test + public void testMakeHeaders() { + verifyHeaders(oper.makeHeaders()); + } + + @Test + public void testMakeRequestNoResourceLink() throws Exception { + // pre-load EMPTY tenant data + preloadTenantData(new StandardCoderObject()); + + when(rawResponse.readEntity(String.class)).thenReturn(makeCqReply()); + when(client.put(any(), any(), any(), any())).thenAnswer(provideResponse(rawResponse)); + + CompletableFuture future2 = oper.start(); + + assertEquals(PolicyResult.FAILURE_EXCEPTION, future2.get(5, TimeUnit.SECONDS).getResult()); + } + + private String makeTenantReply() throws Exception { + Map links = Map.of(AaiCustomQueryOperation.RESOURCE_LINK, MY_LINK); + List> data = Arrays.asList(links); + + Map reply = Map.of(AaiCustomQueryOperation.RESULT_DATA, data); + return coder.encode(reply); + } + + private String makeCqReply() { + return "{}"; + } + + private StandardCoderObject preloadTenantData() throws Exception { + StandardCoderObject data = coder.decode(makeTenantReply(), StandardCoderObject.class); + preloadTenantData(data); + return data; + } + + private void preloadTenantData(StandardCoderObject data) { + context.setProperty(AaiGetOperation.getTenantKey(TARGET_ENTITY), data); + } + + protected class MyTenantOperator extends HttpOperator { + public MyTenantOperator() { + super(AaiConstants.ACTOR_NAME, AaiGetOperation.TENANT); + + HttpParams http = HttpParams.builder().clientName(MY_CLIENT).path(PATH).build(); + + configure(Util.translateToMap(AaiGetOperation.TENANT, http)); + start(); + } + + @Override + public Operation buildOperation(ControlLoopOperationParams params) { + return new AaiGetOperation(params, this); + } + + @Override + protected HttpClientFactory getClientFactory() { + return factory; + } + } +} 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 new file mode 100644 index 000000000..ca90ce476 --- /dev/null +++ b/models-interactions/model-actors/actor.aai/src/test/java/org/onap/policy/controlloop/actor/aai/AaiGetOperatorTest.java @@ -0,0 +1,131 @@ +/*- + * ============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.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; + +import java.util.Map; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.TimeUnit; +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()); + + assertEquals(PolicyResult.SUCCESS, future2.get(5, TimeUnit.SECONDS).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()); + + assertEquals(PolicyResult.FAILURE, future2.get(5, TimeUnit.SECONDS).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 new file mode 100644 index 000000000..39ed6fe88 --- /dev/null +++ b/models-interactions/model-actors/actor.aai/src/test/java/org/onap/policy/controlloop/actor/aai/AaiUtilTest.java @@ -0,0 +1,36 @@ +/*- + * ============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 java.util.Map; +import org.junit.Test; + +public class AaiUtilTest extends BasicAaiOperator { + + @Test + public void testMakeHeaders() { + makeContext(); + + Map headers = AaiUtil.makeHeaders(params); + + verifyHeaders(headers); + } +} 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 new file mode 100644 index 000000000..50b562afb --- /dev/null +++ b/models-interactions/model-actors/actor.aai/src/test/java/org/onap/policy/controlloop/actor/aai/BasicAaiOperator.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 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