diff options
author | Jim Hahn <jrh3@att.com> | 2020-06-11 19:00:41 -0400 |
---|---|---|
committer | Jim Hahn <jrh3@att.com> | 2020-06-12 18:40:29 -0400 |
commit | e9af3a2b3a430626c740b18ccf8592706db1dfb1 (patch) | |
tree | ff0d351b54f4862de8512666d3a7c1371ef73118 /models-interactions/model-actors/actor.vfc/src/test | |
parent | c34fdc19686d70af12e8873b0b01b96dd54c1aa3 (diff) |
Moving common polling code into HttpOperation
SO and VFC have duplicate code for polling. Moved it into the
common superclass.
Issue-ID: POLICY-2632
Change-Id: I27128bfb2d54ef522b6b44ff569819a8463f3454
Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'models-interactions/model-actors/actor.vfc/src/test')
7 files changed, 24 insertions, 339 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 index 47371d2b3..d41da1b87 100644 --- 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 @@ -27,17 +27,18 @@ import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams; import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance; import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance; import org.onap.policy.controlloop.actor.test.BasicHttpOperation; +import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpPollingConfig; import org.onap.policy.simulators.Util; 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; + public static final String POLL_PATH = "my-path-get/"; + public static final int MAX_POLLS = 3; + public static final int POLL_WAIT_SEC = 20; @Mock - protected VfcConfig config; + protected HttpPollingConfig config; protected VfcResponse response; @@ -94,9 +95,9 @@ public abstract class BasicVfcOperation extends BasicHttpOperation<VfcRequest> { 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); + when(config.getMaxPolls()).thenReturn(MAX_POLLS); + when(config.getPollPath()).thenReturn(POLL_PATH); + when(config.getPollWaitSec()).thenReturn(POLL_WAIT_SEC); } } 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 index d8ba7e4e8..ed072f761 100644 --- 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 @@ -32,6 +32,8 @@ import org.junit.BeforeClass; import org.junit.Test; import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance; import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome; +import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpPollingConfig; +import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpPollingParams; import org.onap.policy.controlloop.policy.PolicyResult; import org.onap.policy.vfc.VfcRequest; import org.onap.policy.vfc.VfcResponse; @@ -66,10 +68,12 @@ public class RestartTest extends BasicVfcOperation { */ @Test public void testSuccess() throws Exception { - VfcParams opParams = VfcParams.builder().clientName(MY_CLIENT).path("ns").pathGet("jobs").maxGets(1).build(); - config = new VfcConfig(blockingExecutor, opParams, HttpClientFactoryInstance.getClientFactory()); + HttpPollingParams opParams = HttpPollingParams.builder().clientName(MY_CLIENT).path("ns").pollPath("jobs") + .maxPolls(1).build(); + config = new HttpPollingConfig(blockingExecutor, opParams, HttpClientFactoryInstance.getClientFactory()); params = params.toBuilder().retry(0).timeoutSec(5).executor(blockingExecutor).build(); + restartOper = new Restart(params, config); outcome = restartOper.start().get(); @@ -81,7 +85,7 @@ public class RestartTest extends BasicVfcOperation { public void testConstructor() { CompletableFuture<OperationOutcome> futureRes = restartOper.startOperationAsync(1, outcome); assertNotNull(futureRes); - assertEquals(0, restartOper.getGetCount()); + assertEquals(0, restartOper.getPollCount()); } @Test 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 deleted file mode 100644 index f03e32393..000000000 --- a/models-interactions/model-actors/actor.vfc/src/test/java/org/onap/policy/controlloop/actor/vfc/VfcConfigTest.java +++ /dev/null @@ -1,85 +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.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 index 0b58fd4a3..425ff91b1 100644 --- 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 @@ -20,20 +20,15 @@ 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; @@ -55,11 +50,11 @@ public class VfcOperationTest extends BasicVfcOperation { } @Test - public void testConstructor_testGetWaitMsGet() { + public void testConstructor() { assertEquals(DEFAULT_ACTOR, oper.getActorName()); assertEquals(DEFAULT_OPERATION, oper.getName()); assertSame(config, oper.getConfig()); - assertEquals(1000 * WAIT_SEC_GETS, oper.getWaitMsGet()); + assertTrue(oper.isUsePolling()); } @Test @@ -68,44 +63,9 @@ public class VfcOperationTest extends BasicVfcOperation { } @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"); - - // null status - CompletableFuture<OperationOutcome> future2 = oper.postProcessResponse(outcome, PATH, rawResponse, response); - assertFalse(future2.isDone()); - - response.getResponseDescriptor().setStatus("FinisHeD"); - future2 = oper.postProcessResponse(outcome, PATH, rawResponse, response); - assertTrue(future2.isDone()); - assertSame(outcome, future2.get()); - assertEquals(PolicyResult.SUCCESS, outcome.getResult()); - assertSame(response, outcome.getResponse()); - - // failed - response.getResponseDescriptor().setStatus("eRRor"); - future2 = oper.postProcessResponse(outcome, PATH, rawResponse, response); - assertTrue(future2.isDone()); - assertSame(outcome, future2.get()); - assertEquals(PolicyResult.FAILURE, outcome.getResult()); - assertSame(response, outcome.getResponse()); - - // unfinished - response.getResponseDescriptor().setStatus("anything but finished"); - future2 = oper.postProcessResponse(outcome, PATH, rawResponse, response); - assertFalse(future2.isDone()); + public void testResetPollCount() { + oper.resetPollCount(); + assertEquals(0, oper.getPollCount()); } @Test 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 deleted file mode 100644 index d469d5a74..000000000 --- a/models-interactions/model-actors/actor.vfc/src/test/java/org/onap/policy/controlloop/actor/vfc/VfcOperatorTest.java +++ /dev/null @@ -1,102 +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.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 deleted file mode 100644 index 5e324463f..000000000 --- a/models-interactions/model-actors/actor.vfc/src/test/java/org/onap/policy/controlloop/actor/vfc/VfcParamsTest.java +++ /dev/null @@ -1,93 +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.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); - } - -} diff --git a/models-interactions/model-actors/actor.vfc/src/test/resources/service.yaml b/models-interactions/model-actors/actor.vfc/src/test/resources/service.yaml index f6988517b..04d9e4639 100644 --- a/models-interactions/model-actors/actor.vfc/src/test/resources/service.yaml +++ b/models-interactions/model-actors/actor.vfc/src/test/resources/service.yaml @@ -25,11 +25,11 @@ httpClients: managed: true actors: VFC: + clientName: my-client + pollPath: jobs + pollWaitSec: 20 + maxPolls: 20 operations: Restart: - clientName: my-client - pathGet: jobs - maxGets: 20 - waitSecGet: 20 path: ns timeoutSec: 60 |