From e9af3a2b3a430626c740b18ccf8592706db1dfb1 Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Thu, 11 Jun 2020 19:00:41 -0400 Subject: 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 --- .../onap/policy/controlloop/actor/vfc/Restart.java | 2 +- .../actor/vfc/VfcActorServiceProvider.java | 10 +- .../policy/controlloop/actor/vfc/VfcConfig.java | 63 ----------- .../policy/controlloop/actor/vfc/VfcOperation.java | 115 ++++++--------------- .../policy/controlloop/actor/vfc/VfcOperator.java | 49 --------- .../policy/controlloop/actor/vfc/VfcParams.java | 55 ---------- .../controlloop/actor/vfc/BasicVfcOperation.java | 15 +-- .../policy/controlloop/actor/vfc/RestartTest.java | 10 +- .../controlloop/actor/vfc/VfcConfigTest.java | 85 --------------- .../controlloop/actor/vfc/VfcOperationTest.java | 50 +-------- .../controlloop/actor/vfc/VfcOperatorTest.java | 102 ------------------ .../controlloop/actor/vfc/VfcParamsTest.java | 93 ----------------- .../actor.vfc/src/test/resources/service.yaml | 8 +- 13 files changed, 62 insertions(+), 595 deletions(-) delete mode 100644 models-interactions/model-actors/actor.vfc/src/main/java/org/onap/policy/controlloop/actor/vfc/VfcConfig.java delete mode 100644 models-interactions/model-actors/actor.vfc/src/main/java/org/onap/policy/controlloop/actor/vfc/VfcOperator.java delete mode 100644 models-interactions/model-actors/actor.vfc/src/main/java/org/onap/policy/controlloop/actor/vfc/VfcParams.java delete mode 100644 models-interactions/model-actors/actor.vfc/src/test/java/org/onap/policy/controlloop/actor/vfc/VfcConfigTest.java delete mode 100644 models-interactions/model-actors/actor.vfc/src/test/java/org/onap/policy/controlloop/actor/vfc/VfcOperatorTest.java delete mode 100644 models-interactions/model-actors/actor.vfc/src/test/java/org/onap/policy/controlloop/actor/vfc/VfcParamsTest.java (limited to 'models-interactions/model-actors/actor.vfc') diff --git a/models-interactions/model-actors/actor.vfc/src/main/java/org/onap/policy/controlloop/actor/vfc/Restart.java b/models-interactions/model-actors/actor.vfc/src/main/java/org/onap/policy/controlloop/actor/vfc/Restart.java index b6b82d14f..bc9642888 100644 --- a/models-interactions/model-actors/actor.vfc/src/main/java/org/onap/policy/controlloop/actor/vfc/Restart.java +++ b/models-interactions/model-actors/actor.vfc/src/main/java/org/onap/policy/controlloop/actor/vfc/Restart.java @@ -41,7 +41,7 @@ public class Restart extends VfcOperation { protected CompletableFuture startOperationAsync(int attempt, OperationOutcome outcome) { // starting a whole new attempt - reset the count - resetGetCount(); + resetPollCount(); Pair pair = makeRequest(); Entity entity = Entity.entity(pair.getRight(), MediaType.APPLICATION_JSON); diff --git a/models-interactions/model-actors/actor.vfc/src/main/java/org/onap/policy/controlloop/actor/vfc/VfcActorServiceProvider.java b/models-interactions/model-actors/actor.vfc/src/main/java/org/onap/policy/controlloop/actor/vfc/VfcActorServiceProvider.java index 5bd6b59ad..768ad4c11 100644 --- a/models-interactions/model-actors/actor.vfc/src/main/java/org/onap/policy/controlloop/actor/vfc/VfcActorServiceProvider.java +++ b/models-interactions/model-actors/actor.vfc/src/main/java/org/onap/policy/controlloop/actor/vfc/VfcActorServiceProvider.java @@ -27,14 +27,16 @@ import java.util.List; import org.onap.policy.aai.AaiCqResponse; import org.onap.policy.controlloop.ControlLoopOperation; import org.onap.policy.controlloop.VirtualControlLoopEvent; -import org.onap.policy.controlloop.actorserviceprovider.impl.ActorImpl; +import org.onap.policy.controlloop.actorserviceprovider.impl.HttpActor; +import org.onap.policy.controlloop.actorserviceprovider.impl.HttpPollingOperator; +import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpPollingActorParams; import org.onap.policy.controlloop.policy.Policy; import org.onap.policy.vfc.VfcHealActionVmInfo; import org.onap.policy.vfc.VfcHealAdditionalParams; import org.onap.policy.vfc.VfcHealRequest; import org.onap.policy.vfc.VfcRequest; -public class VfcActorServiceProvider extends ActorImpl { +public class VfcActorServiceProvider extends HttpActor { private static final String GENERIC_VNF_ID = "generic-vnf.vnf-id"; // TODO old code: remove lines down to **HERE** @@ -60,9 +62,9 @@ public class VfcActorServiceProvider extends ActorImpl { * Constructor. */ public VfcActorServiceProvider() { - super(NAME); + super(NAME, HttpPollingActorParams.class); - addOperator(new VfcOperator(NAME, Restart.NAME, Restart::new)); + addOperator(new HttpPollingOperator(NAME, Restart.NAME, Restart::new)); } // TODO old code: remove lines down to **HERE** diff --git a/models-interactions/model-actors/actor.vfc/src/main/java/org/onap/policy/controlloop/actor/vfc/VfcConfig.java b/models-interactions/model-actors/actor.vfc/src/main/java/org/onap/policy/controlloop/actor/vfc/VfcConfig.java deleted file mode 100644 index 4b5088a3a..000000000 --- a/models-interactions/model-actors/actor.vfc/src/main/java/org/onap/policy/controlloop/actor/vfc/VfcConfig.java +++ /dev/null @@ -1,63 +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 java.util.concurrent.Executor; -import lombok.Getter; -import org.onap.policy.common.endpoints.http.client.HttpClient; -import org.onap.policy.common.endpoints.http.client.HttpClientFactory; -import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig; - -@Getter -public class VfcConfig extends HttpConfig { - - /** - * Path to use for the "get" request. A trailing "/" is added, if it is missing. - */ - private String pathGet; - - /** - * Maximum number of "get" requests permitted, after the initial request, to retrieve - * the response. - */ - private int maxGets; - - /** - * Time, in seconds, to wait between issuing "get" requests. - */ - private int waitSecGet; - - /** - * Constructor. - * - * @param blockingExecutor executor to be used for tasks that may perform blocking I/O - * @param params operator parameters - * @param clientFactory factory from which to obtain the {@link HttpClient} - */ - public VfcConfig(Executor blockingExecutor, VfcParams params, HttpClientFactory clientFactory) { - super(blockingExecutor, params, clientFactory); - - this.pathGet = params.getPathGet() + (params.getPathGet().endsWith("/") ? "" : "/"); - this.maxGets = params.getMaxGets(); - this.waitSecGet = params.getWaitSecGet(); - } - -} diff --git a/models-interactions/model-actors/actor.vfc/src/main/java/org/onap/policy/controlloop/actor/vfc/VfcOperation.java b/models-interactions/model-actors/actor.vfc/src/main/java/org/onap/policy/controlloop/actor/vfc/VfcOperation.java index 573ee2698..d5ee44a5d 100644 --- a/models-interactions/model-actors/actor.vfc/src/main/java/org/onap/policy/controlloop/actor/vfc/VfcOperation.java +++ b/models-interactions/model-actors/actor.vfc/src/main/java/org/onap/policy/controlloop/actor/vfc/VfcOperation.java @@ -21,13 +21,8 @@ package org.onap.policy.controlloop.actor.vfc; import java.util.concurrent.CompletableFuture; -import java.util.concurrent.TimeUnit; -import java.util.function.Function; import javax.ws.rs.core.Response; -import lombok.Getter; import org.apache.commons.lang3.StringUtils; -import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure; -import org.onap.policy.common.endpoints.utils.NetLoggerUtil.EventType; import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome; import org.onap.policy.controlloop.actorserviceprovider.controlloop.ControlLoopEventContext; import org.onap.policy.controlloop.actorserviceprovider.impl.HttpOperation; @@ -39,12 +34,8 @@ import org.onap.policy.vfc.VfcHealAdditionalParams; import org.onap.policy.vfc.VfcHealRequest; import org.onap.policy.vfc.VfcRequest; import org.onap.policy.vfc.VfcResponse; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; public abstract class VfcOperation extends HttpOperation { - private static final Logger logger = LoggerFactory.getLogger(VfcOperation.class); - public static final String FAILED = "FAILED"; public static final String COMPLETE = "COMPLETE"; public static final int VFC_RESPONSE_CODE = 999; @@ -53,13 +44,11 @@ public abstract class VfcOperation extends HttpOperation { public static final String REQ_PARAM_NM = "requestParameters"; public static final String CONFIG_PARAM_NM = "configurationParameters"; - private final VfcConfig config; - /** - * Number of "get" requests issued so far, on the current operation attempt. + * Job ID extracted from the first response. */ - @Getter - private int getCount; + private String jobId; + /** * Constructs the object. @@ -69,14 +58,19 @@ public abstract class VfcOperation extends HttpOperation { */ public VfcOperation(ControlLoopOperationParams params, HttpConfig config) { super(params, config, VfcResponse.class); - this.config = (VfcConfig) config; + + setUsePolling(); } - /** - * Subclasses should invoke this before issuing their first HTTP request. - */ - protected void resetGetCount() { - getCount = 0; + @Override + protected void resetPollCount() { + super.resetPollCount(); + jobId = null; + } + + @Override + protected String getPollingPath() { + return super.getPollingPath() + jobId; } /** @@ -87,64 +81,38 @@ public abstract class VfcOperation extends HttpOperation { return startGuardAsync(); } - /** - * If the response does not indicate that the request has been completed, then sleep a - * bit and issue a "get". - */ @Override - protected CompletableFuture postProcessResponse(OperationOutcome outcome, String url, - Response rawResponse, VfcResponse response) { - // Determine if the request has "completed" and determine if it was successful + protected Status detmStatus(Response rawResponse, VfcResponse response) { if (rawResponse.getStatus() == 200) { String requestState = getRequestState(response); if ("finished".equalsIgnoreCase(requestState)) { - return CompletableFuture - .completedFuture(setOutcome(outcome, PolicyResult.SUCCESS, rawResponse, response)); + extractJobId(response); + return Status.SUCCESS; } if ("error".equalsIgnoreCase(requestState)) { - return CompletableFuture - .completedFuture(setOutcome(outcome, PolicyResult.FAILURE, rawResponse, response)); + extractJobId(response); + return Status.FAILURE; } } // still incomplete - // need a request ID with which to query - if (response == null || response.getJobId() == null) { + // need a job ID with which to query + if (jobId == null && !extractJobId(response)) { throw new IllegalArgumentException("missing job ID in response"); } - // see if the limit for the number of "gets" has been reached - if (getCount++ >= getMaxGets()) { - logger.warn("{}: execeeded 'get' limit {} for {}", getFullName(), getMaxGets(), params.getRequestId()); - setOutcome(outcome, PolicyResult.FAILURE_TIMEOUT); - outcome.setResponse(response); - outcome.setMessage(VFC_RESPONSE_CODE + " " + outcome.getMessage()); - return CompletableFuture.completedFuture(outcome); - } - - // sleep and then perform a "get" operation - Function> doGet = unused -> issueGet(outcome, response); - return sleep(getWaitMsGet(), TimeUnit.MILLISECONDS).thenComposeAsync(doGet); + return Status.STILL_WAITING; } - /** - * Issues a "get" request to see if the original request is complete yet. - * - * @param outcome outcome to be populated with the response - * @param response previous response - * @return a future that can be used to cancel the "get" request or await its response - */ - private CompletableFuture issueGet(OperationOutcome outcome, VfcResponse response) { - String path = getPathGet() + response.getJobId(); - String url = getClient().getBaseUrl() + path; - - logger.debug("{}: 'get' count {} for {}", getFullName(), getCount, params.getRequestId()); - - logMessage(EventType.OUT, CommInfrastructure.REST, url, null); + private boolean extractJobId(VfcResponse response) { + if (response == null || response.getJobId() == null) { + return false; + } - return handleResponse(outcome, url, callback -> getClient().get(callback, path, null)); + jobId = response.getJobId(); + return true; } /** @@ -180,8 +148,10 @@ public abstract class VfcOperation extends HttpOperation { // set default result and message setOutcome(outcome, result); + int code = (result == PolicyResult.FAILURE_TIMEOUT ? VFC_RESPONSE_CODE : rawResponse.getStatus()); + outcome.setResponse(response); - outcome.setMessage(rawResponse.getStatus() + " " + outcome.getMessage()); + outcome.setMessage(code + " " + outcome.getMessage()); return outcome; } @@ -221,27 +191,4 @@ public abstract class VfcOperation extends HttpOperation { return request; } - - // These may be overridden by jUnit tests - - /** - * Gets the wait time, in milliseconds, between "get" requests. - * - * @return the wait time, in milliseconds, between "get" requests - */ - public long getWaitMsGet() { - return TimeUnit.MILLISECONDS.convert(getWaitSecGet(), TimeUnit.SECONDS); - } - - public int getMaxGets() { - return config.getMaxGets(); - } - - public String getPathGet() { - return config.getPathGet(); - } - - public int getWaitSecGet() { - return config.getWaitSecGet(); - } } diff --git a/models-interactions/model-actors/actor.vfc/src/main/java/org/onap/policy/controlloop/actor/vfc/VfcOperator.java b/models-interactions/model-actors/actor.vfc/src/main/java/org/onap/policy/controlloop/actor/vfc/VfcOperator.java deleted file mode 100644 index 4e70d6550..000000000 --- a/models-interactions/model-actors/actor.vfc/src/main/java/org/onap/policy/controlloop/actor/vfc/VfcOperator.java +++ /dev/null @@ -1,49 +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 java.util.Map; -import org.onap.policy.common.parameters.ValidationResult; -import org.onap.policy.controlloop.actorserviceprovider.Util; -import org.onap.policy.controlloop.actorserviceprovider.impl.HttpOperation; -import org.onap.policy.controlloop.actorserviceprovider.impl.HttpOperator; -import org.onap.policy.controlloop.actorserviceprovider.impl.OperationMaker; -import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig; -import org.onap.policy.controlloop.actorserviceprovider.parameters.ParameterValidationRuntimeException; - -public class VfcOperator extends HttpOperator { - - public VfcOperator(String actorName, String name, - @SuppressWarnings("rawtypes") OperationMaker operationMaker) { - super(actorName, name, operationMaker); - } - - @Override - protected HttpConfig makeConfiguration(Map parameters) { - VfcParams params = Util.translate(getFullName(), parameters, VfcParams.class); - ValidationResult result = params.validate(getFullName()); - if (!result.isValid()) { - throw new ParameterValidationRuntimeException("invalid parameters", result); - } - - return new VfcConfig(getBlockingExecutor(), params, getClientFactory()); - } -} diff --git a/models-interactions/model-actors/actor.vfc/src/main/java/org/onap/policy/controlloop/actor/vfc/VfcParams.java b/models-interactions/model-actors/actor.vfc/src/main/java/org/onap/policy/controlloop/actor/vfc/VfcParams.java deleted file mode 100644 index 31c2a9441..000000000 --- a/models-interactions/model-actors/actor.vfc/src/main/java/org/onap/policy/controlloop/actor/vfc/VfcParams.java +++ /dev/null @@ -1,55 +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 lombok.Data; -import lombok.EqualsAndHashCode; -import lombok.experimental.SuperBuilder; -import org.onap.policy.common.parameters.annotations.Min; -import org.onap.policy.common.parameters.annotations.NotBlank; -import org.onap.policy.common.parameters.annotations.NotNull; -import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpParams; - -@NotNull -@NotBlank -@Data -@EqualsAndHashCode(callSuper = true) -@SuperBuilder(toBuilder = true) -public class VfcParams extends HttpParams { - - /** - * Path to use for the "get" request. - */ - private String pathGet; - - /** - * Maximum number of "get" requests permitted, after the initial request, to retrieve - * the response. - */ - @Min(0) - private int maxGets; - - /** - * Time, in seconds, to wait between issuing "get" requests. - */ - @Min(1) - private int waitSecGet; -} 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 { - 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 { 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 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 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 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 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 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 -- cgit 1.2.3-korg