diff options
Diffstat (limited to 'models-interactions/model-actors/actor.sdnr/src/test')
3 files changed, 374 insertions, 0 deletions
diff --git a/models-interactions/model-actors/actor.sdnr/src/test/java/org/onap/policy/controlloop/actor/sdnr/BasicSdnrOperation.java b/models-interactions/model-actors/actor.sdnr/src/test/java/org/onap/policy/controlloop/actor/sdnr/BasicSdnrOperation.java new file mode 100644 index 000000000..0aea35f37 --- /dev/null +++ b/models-interactions/model-actors/actor.sdnr/src/test/java/org/onap/policy/controlloop/actor/sdnr/BasicSdnrOperation.java @@ -0,0 +1,141 @@ +/*- + * ============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.sdnr; + +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.verify; + +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeoutException; +import java.util.function.BiConsumer; +import org.onap.policy.common.utils.coder.StandardCoderObject; +import org.onap.policy.controlloop.actor.test.BasicBidirectionalTopicOperation; +import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome; +import org.onap.policy.controlloop.actorserviceprovider.Util; +import org.onap.policy.controlloop.actorserviceprovider.impl.OperationMaker; +import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicConfig; +import org.onap.policy.controlloop.policy.PolicyResult; +import org.onap.policy.sdnr.PciResponse; +import org.onap.policy.sdnr.PciResponseWrapper; +import org.onap.policy.sdnr.Status; +import org.onap.policy.sdnr.util.StatusCodeEnum; +import org.powermock.reflect.Whitebox; + +public abstract class BasicSdnrOperation extends BasicBidirectionalTopicOperation { + + protected PciResponseWrapper response; + + /** + * Constructs the object using a default actor and operation name. + */ + public BasicSdnrOperation() { + super(); + } + + /** + * Constructs the object. + * + * @param actor actor name + * @param operation operation name + */ + public BasicSdnrOperation(String actor, String operation) { + super(actor, operation); + } + + /** + * Initializes mocks and sets up. + */ + public void setUp() throws Exception { + super.setUpBasic(); + + response = new PciResponseWrapper(); + + PciResponse body = new PciResponse(); + Status status = new Status(); + status.setCode(100); + status.setValue(StatusCodeEnum.SUCCESS.toString()); + body.setStatus(status); + response.setBody(body); + } + + /** + * Runs the operation and verifies that the response is successful. + * + * @param operation operation to run + */ + protected void verifyOperation(SdnrOperation operation) + throws InterruptedException, ExecutionException, TimeoutException { + + CompletableFuture<OperationOutcome> future2 = operation.start(); + executor.runAll(100); + assertFalse(future2.isDone()); + + verify(forwarder).register(any(), listenerCaptor.capture()); + provideResponse(listenerCaptor.getValue(), StatusCodeEnum.SUCCESS.toString()); + + executor.runAll(100); + assertTrue(future2.isDone()); + + outcome = future2.get(); + assertEquals(PolicyResult.SUCCESS, outcome.getResult()); + } + + /** + * Verifies that an exception is thrown if a field is missing from the enrichment + * data. + * + * @param fieldName name of the field to be removed from the enrichment data + * @param expectedText text expected in the exception message + */ + protected void verifyMissing(String fieldName, String expectedText, + OperationMaker<BidirectionalTopicConfig, SdnrOperation> maker) { + + makeContext(); + enrichment.remove(fieldName); + + SdnrOperation oper = maker.apply(params, config); + + assertThatIllegalArgumentException().isThrownBy(() -> Whitebox.invokeMethod(oper, "makeRequest", 1)) + .withMessageContaining("missing").withMessageContaining(expectedText); + } + + /** + * Provides a response to the listener. + * + * @param listener listener to which to provide the response + * @param code response code + * @param description response description + */ + protected void provideResponse(BiConsumer<String, StandardCoderObject> listener, int code, String description) { + PciResponse response = new PciResponse(); + + Status status = new Status(); + response.setStatus(status); + status.setCode(code); + + provideResponse(listener, Util.translate("", response, String.class)); + } +} diff --git a/models-interactions/model-actors/actor.sdnr/src/test/java/org/onap/policy/controlloop/actor/sdnr/ModifyConfigOperationTest.java b/models-interactions/model-actors/actor.sdnr/src/test/java/org/onap/policy/controlloop/actor/sdnr/ModifyConfigOperationTest.java new file mode 100644 index 000000000..d1e192277 --- /dev/null +++ b/models-interactions/model-actors/actor.sdnr/src/test/java/org/onap/policy/controlloop/actor/sdnr/ModifyConfigOperationTest.java @@ -0,0 +1,94 @@ +/*-- + * ============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.sdnr; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.atomic.AtomicBoolean; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.common.utils.coder.CoderException; +import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome; +import org.onap.policy.controlloop.actorserviceprovider.controlloop.ControlLoopEventContext; +import org.onap.policy.controlloop.policy.PolicyResult; +import org.onap.policy.sdnr.PciRequestWrapper; + +public class ModifyConfigOperationTest extends BasicSdnrOperation { + + private ModifyConfigOperation oper; + + public ModifyConfigOperationTest() { + super(DEFAULT_ACTOR, ModifyConfigOperation.NAME); + } + + @Before + public void setUp() throws Exception { + super.setUp(); + oper = new ModifyConfigOperation(params, config); + } + + + @Test + public void testModifyConfigOperation() { + assertEquals(DEFAULT_ACTOR, oper.getActorName()); + assertEquals(ModifyConfigOperation.NAME, oper.getName()); + } + + @Test + public void testStartPreprocessorAsync() throws Exception { + final CompletableFuture<OperationOutcome> future2 = new CompletableFuture<>(); + context = mock(ControlLoopEventContext.class); + when(context.getEvent()).thenReturn(event); + params = params.toBuilder().context(context).build(); + + AtomicBoolean guardStarted = new AtomicBoolean(); + + oper = new ModifyConfigOperation(params, config) { + @Override + protected CompletableFuture<OperationOutcome> startGuardAsync() { + guardStarted.set(true); + return super.startGuardAsync(); + } + }; + CompletableFuture<OperationOutcome> future3 = oper.startPreprocessorAsync(); + + assertNotNull(future3); + assertFalse(future.isDone()); + assertTrue(guardStarted.get()); + + future2.complete(params.makeOutcome()); + assertTrue(executor.runAll(100)); + assertTrue(future3.isDone()); + assertEquals(PolicyResult.SUCCESS, future3.get().getResult()); + } + + @Test + public void testMakeRequest() throws CoderException { + PciRequestWrapper request = oper.makeRequest(1); + assertNotNull(request); + } +} diff --git a/models-interactions/model-actors/actor.sdnr/src/test/java/org/onap/policy/controlloop/actor/sdnr/SdnrOperationTest.java b/models-interactions/model-actors/actor.sdnr/src/test/java/org/onap/policy/controlloop/actor/sdnr/SdnrOperationTest.java new file mode 100644 index 000000000..fc7e86a94 --- /dev/null +++ b/models-interactions/model-actors/actor.sdnr/src/test/java/org/onap/policy/controlloop/actor/sdnr/SdnrOperationTest.java @@ -0,0 +1,139 @@ +/*- + * ============LICENSE_START======================================================= + * SdnrOperation + * ================================================================================ + * 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.sdnr; + +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertSame; + +import java.util.Arrays; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.controlloop.actorserviceprovider.impl.BidirectionalTopicOperation.Status; +import org.onap.policy.controlloop.policy.PolicyResult; +import org.onap.policy.sdnr.PciCommonHeader; +import org.onap.policy.sdnr.PciRequestWrapper; +import org.onap.policy.sdnr.util.StatusCodeEnum; + +public class SdnrOperationTest extends BasicSdnrOperation { + + private SdnrOperation operation; + + /** + * Setup. + */ + @Before + public void setUp() throws Exception { + super.setUp(); + + operation = new SdnrOperation(params, config) { + @Override + protected PciRequestWrapper makeRequest(int attempt) { + return super.makeRequest(attempt); + } + }; + } + + @Test + public void testSdnrOperation() { + assertEquals(DEFAULT_ACTOR, operation.getActorName()); + assertEquals(DEFAULT_OPERATION, operation.getName()); + } + + @Test + public void testMakeRequest() { + PciRequestWrapper request = operation.makeRequest(1); + + assertNotNull(request.getBody()); + assertEquals("1.0", request.getVersion()); + assertEquals("request", request.getType()); + + PciCommonHeader header = request.getBody().getCommonHeader(); + assertNotNull(header); + assertEquals(params.getRequestId(), header.getRequestId()); + } + + @Test + public void testGetExpectedKeyValues() { + PciRequestWrapper request = operation.makeRequest(1); + assertEquals(Arrays.asList(request.getBody().getCommonHeader().getSubRequestId()), + operation.getExpectedKeyValues(50, request)); + + } + + @Test + public void testDetmStatusStringResponse() { + final org.onap.policy.sdnr.Status status = response.getBody().getStatus(); + + // null status + response.getBody().setStatus(null); + assertThatIllegalArgumentException().isThrownBy(() -> operation.detmStatus("", response)) + .withMessage("SDNR response is missing the response status"); + response.getBody().setStatus(status); + + // invalid code + status.setCode(-45); + assertThatIllegalArgumentException().isThrownBy(() -> operation.detmStatus("", response)) + .withMessage("unknown SDNR response status code: -45"); + + + status.setValue(StatusCodeEnum.ACCEPTED.toString()); + status.setCode(StatusCodeEnum.toValue(StatusCodeEnum.ACCEPTED)); + assertEquals(Status.STILL_WAITING, operation.detmStatus("", response)); + + status.setValue(StatusCodeEnum.SUCCESS.toString()); + status.setCode(StatusCodeEnum.toValue(StatusCodeEnum.SUCCESS)); + assertEquals(Status.SUCCESS, operation.detmStatus("", response)); + + status.setValue(StatusCodeEnum.REJECT.toString()); + status.setCode(StatusCodeEnum.toValue(StatusCodeEnum.REJECT)); + assertThatIllegalArgumentException().isThrownBy(() -> operation.detmStatus("", response)) + .withMessage("SDNR request was not accepted, code=" + StatusCodeEnum.REJECT.toString()); + + status.setValue(StatusCodeEnum.REJECT.toString()); + status.setCode(313); + assertThatIllegalArgumentException().isThrownBy(() -> operation.detmStatus("", response)) + .withMessage("SDNR request was not accepted, code=" + StatusCodeEnum.REJECT.toString()); + + status.setValue(StatusCodeEnum.ERROR.toString()); + status.setCode(StatusCodeEnum.toValue(StatusCodeEnum.ERROR)); + assertThatIllegalArgumentException().isThrownBy(() -> operation.detmStatus("", response)) + .withMessage("SDNR request was not accepted, code=" + StatusCodeEnum.ERROR.toString()); + + status.setValue(StatusCodeEnum.FAILURE.toString()); + status.setCode(450); + assertEquals(Status.FAILURE, operation.detmStatus("", response)); + } + + @Test + public void testSetOutcome() { + final org.onap.policy.sdnr.Status status = response.getBody().getStatus(); + + // null status + response.getBody().setStatus(null); + assertSame(outcome, operation.setOutcome(outcome, PolicyResult.SUCCESS, response)); + assertEquals(PolicyResult.SUCCESS, outcome.getResult()); + assertNotNull(outcome.getMessage()); + response.getBody().setStatus(status); + + } +} |