diff options
Diffstat (limited to 'models-interactions/model-actors/actor.vfc/src/test')
6 files changed, 550 insertions, 0 deletions
diff --git a/models-interactions/model-actors/actor.vfc/src/test/java/org/onap/policy/controlloop/actor/vfc/BasicVfcOperation.java b/models-interactions/model-actors/actor.vfc/src/test/java/org/onap/policy/controlloop/actor/vfc/BasicVfcOperation.java new file mode 100644 index 000000000..be62bbb3f --- /dev/null +++ b/models-interactions/model-actors/actor.vfc/src/test/java/org/onap/policy/controlloop/actor/vfc/BasicVfcOperation.java @@ -0,0 +1,82 @@ +/*- + * ============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.vfc; + +import static org.mockito.Mockito.when; + +import org.mockito.Mock; +import org.onap.policy.controlloop.actor.test.BasicHttpOperation; +import org.onap.policy.vfc.VfcRequest; +import org.onap.policy.vfc.VfcResponse; + +public abstract class BasicVfcOperation extends BasicHttpOperation<VfcRequest> { + public static final String PATH_GET = "my-path-get/"; + public static final int MAX_GETS = 3; + public static final int WAIT_SEC_GETS = 20; + + @Mock + protected VfcConfig config; + + protected VfcResponse response; + + /** + * Constructs the object using a default actor and operation name. + */ + public BasicVfcOperation() { + super(); + } + + /** + * Constructs the object. + * + * @param actor actor name + * @param operation operation name + */ + public BasicVfcOperation(String actor, String operation) { + super(actor, operation); + } + + /** + * Initializes mocks and sets up. + */ + public void setUp() throws Exception { + super.setUpBasic(); + + response = new VfcResponse(); + + // PLD + + when(rawResponse.getStatus()).thenReturn(200); + when(rawResponse.readEntity(String.class)).thenReturn(coder.encode(response)); + + initConfig(); + } + + @Override + protected void initConfig() { + super.initConfig(); + when(config.getClient()).thenReturn(client); + when(config.getMaxGets()).thenReturn(MAX_GETS); + when(config.getPathGet()).thenReturn(PATH_GET); + when(config.getWaitSecGet()).thenReturn(WAIT_SEC_GETS); + } + +} diff --git a/models-interactions/model-actors/actor.vfc/src/test/java/org/onap/policy/controlloop/actor/vfc/RestartTest.java b/models-interactions/model-actors/actor.vfc/src/test/java/org/onap/policy/controlloop/actor/vfc/RestartTest.java new file mode 100644 index 000000000..dd6c4cf69 --- /dev/null +++ b/models-interactions/model-actors/actor.vfc/src/test/java/org/onap/policy/controlloop/actor/vfc/RestartTest.java @@ -0,0 +1,61 @@ +/*- + * ============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.vfc; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.util.concurrent.CompletableFuture; +import org.apache.commons.lang3.tuple.Pair; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome; +import org.onap.policy.vfc.VfcRequest; + +public class RestartTest extends BasicVfcOperation { + private Restart restartOper; + + /** + * setup restart operation. + */ + @Before + public void setup() throws Exception { + super.setUp(); + params.getContext().getEnrichment().put("service-instance.service-instance-id", "test-service-instance-id"); + params.getContext().getEnrichment().put("vserver.vserver-id", "test-vserver-id"); + params.getContext().getEnrichment().put("vserver.vserver-name", "test-vserver-name"); + restartOper = new Restart(params, config); + } + + @Test + public void testStartOperationAsync() { + CompletableFuture<OperationOutcome> futureRes = restartOper.startOperationAsync(1, outcome); + assertNotNull(futureRes); + assertEquals(0, restartOper.getGetCount()); + } + + @Test + public void testMakeRequest() { + Pair<String, VfcRequest> resultPair = restartOper.makeRequest(); + assertNotNull(resultPair.getLeft()); + assertNotNull(resultPair.getRight()); + } +} diff --git a/models-interactions/model-actors/actor.vfc/src/test/java/org/onap/policy/controlloop/actor/vfc/VfcConfigTest.java b/models-interactions/model-actors/actor.vfc/src/test/java/org/onap/policy/controlloop/actor/vfc/VfcConfigTest.java new file mode 100644 index 000000000..f03e32393 --- /dev/null +++ b/models-interactions/model-actors/actor.vfc/src/test/java/org/onap/policy/controlloop/actor/vfc/VfcConfigTest.java @@ -0,0 +1,85 @@ +/*- + * ============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.vfc; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertSame; +import static org.mockito.Mockito.when; + +import java.util.concurrent.Executor; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.onap.policy.common.endpoints.http.client.HttpClient; +import org.onap.policy.common.endpoints.http.client.HttpClientFactory; + +public class VfcConfigTest { + private static final String MY_CLIENT = "my-client"; + private static final String MY_PATH = "my-path"; + private static final String GET_PATH = "get-path"; + private static final int TIMEOUT_SEC = 10; + private static final int MAX_GETS = 20; + private static final int WAIT_SEC = 30; + + @Mock + private HttpClient client; + @Mock + private HttpClientFactory factory; + @Mock + private Executor executor; + + private VfcParams params; + private VfcConfig config; + + /** + * Setup. + * + * @throws Exception Exception + */ + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + + when(factory.get(MY_CLIENT)).thenReturn(client); + + params = VfcParams.builder().maxGets(MAX_GETS).pathGet(GET_PATH).waitSecGet(WAIT_SEC).clientName(MY_CLIENT) + .path(MY_PATH).timeoutSec(TIMEOUT_SEC).build(); + config = new VfcConfig(executor, params, factory); + } + + @Test + public void test() { + assertEquals(GET_PATH + "/", config.getPathGet()); + assertEquals(MAX_GETS, config.getMaxGets()); + assertEquals(WAIT_SEC, config.getWaitSecGet()); + + // check value from superclass + assertSame(executor, config.getBlockingExecutor()); + assertSame(client, config.getClient()); + + // path with trailing "/" + params = params.toBuilder().pathGet(GET_PATH + "/").build(); + config = new VfcConfig(executor, params, factory); + assertEquals(GET_PATH + "/", config.getPathGet()); + } + +} diff --git a/models-interactions/model-actors/actor.vfc/src/test/java/org/onap/policy/controlloop/actor/vfc/VfcOperationTest.java b/models-interactions/model-actors/actor.vfc/src/test/java/org/onap/policy/controlloop/actor/vfc/VfcOperationTest.java new file mode 100644 index 000000000..4fe0cd4a6 --- /dev/null +++ b/models-interactions/model-actors/actor.vfc/src/test/java/org/onap/policy/controlloop/actor/vfc/VfcOperationTest.java @@ -0,0 +1,127 @@ +/*- + * ============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.vfc; + +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; +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.assertSame; +import static org.junit.Assert.assertTrue; + +import java.util.concurrent.CompletableFuture; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; +import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome; +import org.onap.policy.controlloop.policy.PolicyResult; +import org.onap.policy.vfc.VfcResponse; +import org.onap.policy.vfc.VfcResponseDescriptor; + +public class VfcOperationTest extends BasicVfcOperation { + + private VfcOperation oper; + + /** + * setUp. + */ + @Override + @Before + public void setUp() throws Exception { + super.setUp(); + + initConfig(); + + oper = new VfcOperation(params, config) {}; + } + + @Test + public void testConstructor_testGetWaitMsGet() { + assertEquals(DEFAULT_ACTOR, oper.getActorName()); + assertEquals(DEFAULT_OPERATION, oper.getName()); + assertSame(config, oper.getConfig()); + assertEquals(1000 * WAIT_SEC_GETS, oper.getWaitMsGet()); + } + + @Test + public void testStartPreprocessorAsync() { + assertNotNull(oper.startPreprocessorAsync()); + } + + @Test + public void testResetGetCount() { + oper.resetGetCount(); + assertEquals(0, oper.getGetCount()); + } + + @Test + public void testPostProcess() throws Exception { + + assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> { + oper.postProcessResponse(outcome, PATH, rawResponse, response); + }); + + response.setResponseDescriptor(new VfcResponseDescriptor()); + response.setJobId("sampleJobId"); + CompletableFuture<OperationOutcome> future2 = oper.postProcessResponse(outcome, PATH, rawResponse, response); + assertFalse(future2.isDone()); + //assertSame(outcome, future2.get()); TODO Hanging + assertEquals(PolicyResult.SUCCESS, outcome.getResult()); + + response.getResponseDescriptor().setStatus("FinisHeD"); + future2 = oper.postProcessResponse(outcome, PATH, rawResponse, response); + assertTrue(future2.isDone()); + assertSame(outcome, future2.get()); + assertEquals(PolicyResult.SUCCESS, outcome.getResult()); + + response.getResponseDescriptor().setStatus("eRRor"); + future2 = oper.postProcessResponse(outcome, PATH, rawResponse, response); + assertTrue(future2.isDone()); + assertSame(outcome, future2.get()); + assertEquals(PolicyResult.FAILURE, outcome.getResult()); + + // failed + /*response.getResponseDescriptor().setStatus("anything but finished"); + future2 = oper.postProcessResponse(outcome, PATH, rawResponse, response); + assertTrue(future2.isDone()); + assertSame(outcome, future2.get()); + assertEquals(PolicyResult.FAILURE, outcome.getResult());*/ + } + + @Test + public void testGetRequestState() { + VfcResponse mockResponse = Mockito.mock(VfcResponse.class); + Mockito.when(mockResponse.getResponseDescriptor()).thenReturn(null); + assertNull(oper.getRequestState(mockResponse)); + + VfcResponseDescriptor mockDescriptor = Mockito.mock(VfcResponseDescriptor.class); + Mockito.when(mockResponse.getResponseDescriptor()).thenReturn(mockDescriptor); + Mockito.when(mockDescriptor.getStatus()).thenReturn("COMPLETE"); // TODO use actual request state value + assertNotNull(oper.getRequestState(mockResponse)); + } + + @Test + public void testIsSuccess() { + assertTrue(oper.isSuccess(rawResponse, response)); + } + +} diff --git a/models-interactions/model-actors/actor.vfc/src/test/java/org/onap/policy/controlloop/actor/vfc/VfcOperatorTest.java b/models-interactions/model-actors/actor.vfc/src/test/java/org/onap/policy/controlloop/actor/vfc/VfcOperatorTest.java new file mode 100644 index 000000000..d469d5a74 --- /dev/null +++ b/models-interactions/model-actors/actor.vfc/src/test/java/org/onap/policy/controlloop/actor/vfc/VfcOperatorTest.java @@ -0,0 +1,102 @@ +/*- + * ============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.vfc; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.when; + +import java.util.Map; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.onap.policy.common.endpoints.http.client.HttpClient; +import org.onap.policy.common.endpoints.http.client.HttpClientFactory; +import org.onap.policy.controlloop.actorserviceprovider.Util; +import org.onap.policy.controlloop.actorserviceprovider.parameters.ParameterValidationRuntimeException; + +public class VfcOperatorTest { + private static final String ACTOR = "my-actor"; + private static final String OPERATION = "my-name"; + private static final String CLIENT = "my-client"; + private static final String PATH = "/my-path"; + private static final String PATH_GET = "my-path-get/"; + private static final int MAX_GETS = 3; + private static final int WAIT_SEC_GETS = 20; + private static final int TIMEOUT = 100; + + private VfcOperator oper; + + @Mock + private HttpClient client; + + @Mock + private HttpClientFactory factory; + + /** + * setUp. + * + * @throws Exception exception + */ + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + + when(factory.get(CLIENT)).thenReturn(client); + + oper = new MyOperator(); + + VfcParams params = VfcParams.builder().pathGet(PATH_GET).maxGets(MAX_GETS).waitSecGet(WAIT_SEC_GETS) + .clientName(CLIENT).path(PATH).timeoutSec(TIMEOUT).build(); + Map<String, Object> paramMap = Util.translateToMap(OPERATION, params); + oper.configure(paramMap); + } + + @Test + public void testConstructor() { + assertEquals(ACTOR, oper.getActorName()); + assertEquals(OPERATION, oper.getName()); + assertEquals(ACTOR + "." + OPERATION, oper.getFullName()); + } + + @Test + public void testDoConfigure_testGetters() { + assertTrue(oper.getCurrentConfig() instanceof VfcConfig); + + // test invalid parameters + Map<String, Object> paramMap2 = Util.translateToMap(OPERATION, VfcParams.builder().build()); + assertThatThrownBy(() -> oper.configure(paramMap2)).isInstanceOf(ParameterValidationRuntimeException.class); + } + + private class MyOperator extends VfcOperator { + + public MyOperator() { + super(ACTOR, OPERATION, null); + } + + @Override + protected HttpClientFactory getClientFactory() { + return factory; + } + } +} diff --git a/models-interactions/model-actors/actor.vfc/src/test/java/org/onap/policy/controlloop/actor/vfc/VfcParamsTest.java b/models-interactions/model-actors/actor.vfc/src/test/java/org/onap/policy/controlloop/actor/vfc/VfcParamsTest.java new file mode 100644 index 000000000..5e324463f --- /dev/null +++ b/models-interactions/model-actors/actor.vfc/src/test/java/org/onap/policy/controlloop/actor/vfc/VfcParamsTest.java @@ -0,0 +1,93 @@ +/*- + * ============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.vfc; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.util.function.Function; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.common.parameters.ValidationResult; +import org.onap.policy.controlloop.actor.vfc.VfcParams.VfcParamsBuilder; +import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpParams.HttpParamsBuilder; + +public class VfcParamsTest { + private static final String CONTAINER = "my-container"; + private static final String CLIENT = "my-client"; + private static final String PATH = "my-path"; + private static final String PATH_GET = "my-path-get"; + private static final int MAX_GETS = 3; + private static final int WAIT_SEC_GETS = 20; + private static final int TIMEOUT = 10; + + private VfcParams params; + + @Before + public void setUp() throws Exception { + params = VfcParams.builder().pathGet(PATH_GET).maxGets(MAX_GETS).waitSecGet(WAIT_SEC_GETS).clientName(CLIENT) + .path(PATH).timeoutSec(TIMEOUT).build(); + } + + @Test + public void testValidate() { + assertTrue(params.validate(CONTAINER).isValid()); + + testValidateField("pathGet", "null", bldr -> bldr.pathGet(null)); + testValidateField("maxGets", "minimum", bldr -> bldr.maxGets(-1)); + testValidateField("waitSecGet", "minimum", bldr -> bldr.waitSecGet(-1)); + + // validate one of the superclass fields + testValidateField("clientName", "null", bldr -> bldr.clientName(null)); + + // check edge cases + assertTrue(params.toBuilder().maxGets(0).build().validate(CONTAINER).isValid()); + assertFalse(params.toBuilder().waitSecGet(0).build().validate(CONTAINER).isValid()); + assertTrue(params.toBuilder().waitSecGet(1).build().validate(CONTAINER).isValid()); + } + + @Test + public void testBuilder_testToBuilder() { + assertEquals(CLIENT, params.getClientName()); + + assertEquals(PATH_GET, params.getPathGet()); + assertEquals(MAX_GETS, params.getMaxGets()); + assertEquals(WAIT_SEC_GETS, params.getWaitSecGet()); + + assertEquals(params, params.toBuilder().build()); + } + + private void testValidateField(String fieldName, String expected, + @SuppressWarnings("rawtypes") Function<VfcParamsBuilder, HttpParamsBuilder> makeInvalid) { + + // original params should be valid + ValidationResult result = params.validate(CONTAINER); + assertTrue(fieldName, result.isValid()); + + // make invalid params + result = makeInvalid.apply(params.toBuilder()).build().validate(CONTAINER); + assertFalse(fieldName, result.isValid()); + assertThat(result.getResult()).contains(fieldName).contains(expected); + } + +} |