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/actorServiceProvider/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/actorServiceProvider/src/test')
6 files changed, 665 insertions, 4 deletions
diff --git a/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/impl/BidirectionalTopicOperatorTest.java b/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/impl/BidirectionalTopicOperatorTest.java index b5e3de32c..29f507e60 100644 --- a/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/impl/BidirectionalTopicOperatorTest.java +++ b/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/impl/BidirectionalTopicOperatorTest.java @@ -34,7 +34,6 @@ import org.junit.Before; import org.junit.Test; import org.mockito.Mock; import org.mockito.MockitoAnnotations; -import org.onap.policy.controlloop.actorserviceprovider.Operation; import org.onap.policy.controlloop.actorserviceprovider.Util; import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicConfig; import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicParams; @@ -108,8 +107,7 @@ public class BidirectionalTopicOperatorTest { AtomicReference<BidirectionalTopicConfig> configRef = new AtomicReference<>(); // @formatter:off - @SuppressWarnings("rawtypes") - OperationMaker<BidirectionalTopicConfig, BidirectionalTopicOperation> maker = + OperationMaker<BidirectionalTopicConfig, BidirectionalTopicOperation<?,?>> maker = (params, config) -> { paramsRef.set(params); configRef.set(config); @@ -152,7 +150,7 @@ public class BidirectionalTopicOperatorTest { } @Override - public Operation buildOperation(ControlLoopOperationParams params) { + public BidirectionalTopicOperation<?,?> buildOperation(ControlLoopOperationParams params) { return null; } } diff --git a/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/impl/HttpPollingOperationTest.java b/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/impl/HttpPollingOperationTest.java new file mode 100644 index 000000000..ed5ba9b56 --- /dev/null +++ b/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/impl/HttpPollingOperationTest.java @@ -0,0 +1,255 @@ +/*- + * ============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.actorserviceprovider.impl; + +import static org.assertj.core.api.Assertions.assertThatIllegalStateException; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.Assert.assertEquals; +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 static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ForkJoinPool; +import java.util.concurrent.TimeUnit; +import javax.ws.rs.client.InvocationCallback; +import javax.ws.rs.core.Response; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.mockito.stubbing.Answer; +import org.onap.policy.common.endpoints.http.client.HttpClient; +import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome; +import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams; +import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig; +import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpPollingConfig; +import org.onap.policy.controlloop.policy.PolicyResult; + +/** + * Tests HttpOperation when polling is enabled. + */ +public class HttpPollingOperationTest { + private static final String BASE_URI = "http://my-host:6969/base-uri/"; + private static final String MY_PATH = "my-path"; + private static final String FULL_PATH = BASE_URI + MY_PATH; + private static final int MAX_POLLS = 3; + private static final int POLL_WAIT_SEC = 20; + private static final String POLL_PATH = "my-poll-path"; + private static final String MY_ACTOR = "my-actor"; + private static final String MY_OPERATION = "my-operation"; + private static final String MY_RESPONSE = "my-response"; + private static final int RESPONSE_ACCEPT = 100; + private static final int RESPONSE_SUCCESS = 200; + private static final int RESPONSE_FAILURE = 500; + + @Mock + private HttpPollingConfig config; + @Mock + private HttpClient client; + @Mock + private Response rawResponse; + + protected ControlLoopOperationParams params; + private String response; + private OperationOutcome outcome; + + private HttpOperation<String> oper; + + /** + * Sets up. + */ + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + + when(client.getBaseUrl()).thenReturn(BASE_URI); + + when(config.getClient()).thenReturn(client); + when(config.getPath()).thenReturn(MY_PATH); + when(config.getMaxPolls()).thenReturn(MAX_POLLS); + when(config.getPollPath()).thenReturn(POLL_PATH); + when(config.getPollWaitSec()).thenReturn(POLL_WAIT_SEC); + + response = MY_RESPONSE; + + when(rawResponse.getStatus()).thenReturn(RESPONSE_SUCCESS); + when(rawResponse.readEntity(String.class)).thenReturn(response); + + params = ControlLoopOperationParams.builder().actor(MY_ACTOR).operation(MY_OPERATION).build(); + outcome = params.makeOutcome(); + + oper = new MyOper(params, config); + } + + @Test + public void testConstructor_testGetWaitMsGet() { + assertEquals(MY_ACTOR, oper.getActorName()); + assertEquals(MY_OPERATION, oper.getName()); + assertSame(config, oper.getConfig()); + assertEquals(1000 * POLL_WAIT_SEC, oper.getPollWaitMs()); + } + + @Test + public void testSetUsePollExceptions() { + // should be no exception + oper.setUsePolling(); + + // should throw an exception if we pass a plain HttpConfig + HttpConfig config2 = mock(HttpConfig.class); + when(config2.getClient()).thenReturn(client); + when(config2.getPath()).thenReturn(MY_PATH); + + assertThatIllegalStateException().isThrownBy(() -> new MyOper(params, config2).setUsePolling()); + } + + @Test + public void testPostProcess() throws Exception { + // completed + oper.generateSubRequestId(2); + CompletableFuture<OperationOutcome> future2 = + oper.postProcessResponse(outcome, FULL_PATH, rawResponse, response); + assertTrue(future2.isDone()); + assertSame(outcome, future2.get()); + assertEquals(PolicyResult.SUCCESS, outcome.getResult()); + assertNotNull(oper.getSubRequestId()); + assertSame(response, outcome.getResponse()); + + // failed + oper.generateSubRequestId(2); + when(rawResponse.getStatus()).thenReturn(RESPONSE_FAILURE); + future2 = oper.postProcessResponse(outcome, FULL_PATH, rawResponse, response); + assertTrue(future2.isDone()); + assertSame(outcome, future2.get()); + assertEquals(PolicyResult.FAILURE, outcome.getResult()); + assertNotNull(oper.getSubRequestId()); + assertSame(response, outcome.getResponse()); + } + + /** + * Tests postProcess() when the poll is repeated a couple of times. + */ + @Test + public void testPostProcessRepeated_testResetGetCount() throws Exception { + /* + * Two accepts and then a success - should result in two polls. + */ + when(rawResponse.getStatus()).thenReturn(RESPONSE_ACCEPT, RESPONSE_ACCEPT, RESPONSE_SUCCESS, RESPONSE_SUCCESS); + + when(client.get(any(), any(), any())).thenAnswer(provideResponse(rawResponse)); + + // use a real executor + params = params.toBuilder().executor(ForkJoinPool.commonPool()).build(); + + oper = new MyOper(params, config) { + @Override + public long getPollWaitMs() { + return 1; + } + }; + + CompletableFuture<OperationOutcome> future2 = + oper.postProcessResponse(outcome, FULL_PATH, rawResponse, response); + + assertSame(outcome, future2.get(5, TimeUnit.SECONDS)); + assertEquals(PolicyResult.SUCCESS, outcome.getResult()); + assertEquals(2, oper.getPollCount()); + + /* + * repeat - this time, the "poll" count will be exhausted, so it should fail + */ + when(rawResponse.getStatus()).thenReturn(RESPONSE_ACCEPT); + + future2 = oper.postProcessResponse(outcome, FULL_PATH, rawResponse, response); + + assertSame(outcome, future2.get(5, TimeUnit.SECONDS)); + assertEquals(PolicyResult.FAILURE_TIMEOUT, outcome.getResult()); + assertEquals(MAX_POLLS + 1, oper.getPollCount()); + + oper.resetPollCount(); + assertEquals(0, oper.getPollCount()); + assertNull(oper.getSubRequestId()); + } + + @Test + public void testDetmStatus() { + // make an operation that does NOT override detmStatus() + oper = new HttpOperation<String>(params, config, String.class) {}; + + assertThatThrownBy(() -> oper.detmStatus(rawResponse, response)) + .isInstanceOf(UnsupportedOperationException.class); + } + + /** + * Provides a response to an asynchronous HttpClient call. + * + * @param response response to be provided to the call + * @return a function that provides the response to the call + */ + protected Answer<CompletableFuture<Response>> provideResponse(Response response) { + return provideResponse(response, 0); + } + + /** + * Provides a response to an asynchronous HttpClient call. + * + * @param response response to be provided to the call + * @param index index of the callback within the arguments + * @return a function that provides the response to the call + */ + protected Answer<CompletableFuture<Response>> provideResponse(Response response, int index) { + return args -> { + InvocationCallback<Response> cb = args.getArgument(index); + cb.completed(response); + return CompletableFuture.completedFuture(response); + }; + } + + private static class MyOper extends HttpOperation<String> { + + public MyOper(ControlLoopOperationParams params, HttpConfig config) { + super(params, config, String.class); + + setUsePolling(); + } + + @Override + protected Status detmStatus(Response rawResponse, String response) { + switch (rawResponse.getStatus()) { + case RESPONSE_ACCEPT: + return Status.STILL_WAITING; + case RESPONSE_SUCCESS: + return Status.SUCCESS; + default: + return Status.FAILURE; + } + } + + @Override + protected boolean isSuccess(Response rawResponse, String response) { + return true; + } + } +} diff --git a/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/impl/HttpPollingOperatorTest.java b/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/impl/HttpPollingOperatorTest.java new file mode 100644 index 000000000..2a22b4b86 --- /dev/null +++ b/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/impl/HttpPollingOperatorTest.java @@ -0,0 +1,117 @@ +/*- + * ============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.actorserviceprovider.impl; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +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.ControlLoopOperationParams; +import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig; +import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpPollingConfig; +import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpPollingParams; +import org.onap.policy.controlloop.actorserviceprovider.parameters.ParameterValidationRuntimeException; + +public class HttpPollingOperatorTest { + 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 POLL_PATH = "my-path-get/"; + private static final int MAX_POLLS = 3; + private static final int POLL_WAIT_SEC = 20; + private static final int TIMEOUT = 100; + + @Mock + private HttpClient client; + @Mock + private HttpClientFactory factory; + + private HttpPollingOperator oper; + + /** + * Initializes fields, including {@link #oper}, and resets the static fields used by + * the REST server. + */ + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + + when(factory.get(CLIENT)).thenReturn(client); + + oper = new MyOperator(); + + HttpPollingParams params = HttpPollingParams.builder().pollPath(POLL_PATH).maxPolls(MAX_POLLS) + .pollWaitSec(POLL_WAIT_SEC).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 HttpPollingConfig); + + // test invalid parameters + Map<String, Object> paramMap2 = Util.translateToMap(OPERATION, HttpPollingParams.builder().build()); + assertThatThrownBy(() -> oper.configure(paramMap2)).isInstanceOf(ParameterValidationRuntimeException.class); + } + + @Test + public void testGetClientFactory() { + HttpPollingOperator oper2 = new HttpPollingOperator(ACTOR, OPERATION); + assertNotNull(oper2.getClientFactory()); + } + + + private class MyOperator extends HttpPollingOperator { + public MyOperator() { + super(ACTOR, OPERATION, MyOperation::new); + } + + @Override + protected HttpClientFactory getClientFactory() { + return factory; + } + } + + private static class MyOperation extends HttpOperation<String> { + public MyOperation(ControlLoopOperationParams params, HttpConfig config) { + super(params, config, String.class); + } + } +} diff --git a/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/parameters/HttpPollingActorParamsTest.java b/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/parameters/HttpPollingActorParamsTest.java new file mode 100644 index 000000000..78240beb6 --- /dev/null +++ b/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/parameters/HttpPollingActorParamsTest.java @@ -0,0 +1,118 @@ +/*- + * ============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.actorserviceprovider.parameters; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.util.Map; +import java.util.TreeMap; +import java.util.function.Consumer; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.common.parameters.ValidationResult; +import org.onap.policy.controlloop.actorserviceprovider.Util; +import org.onap.policy.controlloop.actorserviceprovider.parameters.ActorParams; + + +public class HttpPollingActorParamsTest { + private static final String CONTAINER = "my-container"; + private static final String CLIENT = "my-client"; + private static final String POLL_PATH = "my-poll-path"; + private static final int MAX_POLLS = 3; + private static final int POLL_WAIT_SEC = 20; + private static final int TIMEOUT = 10; + + private static final String PATH1 = "path #1"; + private static final String PATH2 = "path #2"; + private static final String URI1 = "uri #1"; + private static final String URI2 = "uri #2"; + + private Map<String, Map<String, Object>> operations; + private HttpPollingActorParams params; + + /** + * Initializes {@link #operations} with two items and {@link params} with a fully + * populated object. + */ + @Before + public void setUp() { + operations = new TreeMap<>(); + operations.put(PATH1, Map.of("path", URI1)); + operations.put(PATH2, Map.of("path", URI2)); + + params = makeHttpPollingActorParams(); + } + + @Test + public void testValidate() { + assertTrue(params.validate(CONTAINER).isValid()); + + // only a few fields are required + HttpPollingActorParams sparse = Util.translate(CONTAINER, Map.of(ActorParams.OPERATIONS_FIELD, operations), + HttpPollingActorParams.class); + assertTrue(sparse.validate(CONTAINER).isValid()); + + testValidateField("maxPolls", "minimum", params2 -> params2.setMaxPolls(-1)); + testValidateField("pollWaitSec", "minimum", params2 -> params2.setPollWaitSec(0)); + + // check fields from superclass + testValidateField(ActorParams.OPERATIONS_FIELD, "null", params2 -> params2.setOperations(null)); + testValidateField("timeoutSec", "minimum", params2 -> params2.setTimeoutSec(-1)); + + // check edge cases + params.setMaxPolls(0); + assertTrue(params.validate(CONTAINER).isValid()); + params.setMaxPolls(MAX_POLLS); + + params.setPollWaitSec(1); + assertTrue(params.validate(CONTAINER).isValid()); + params.setPollWaitSec(POLL_WAIT_SEC); + } + + private void testValidateField(String fieldName, String expected, Consumer<HttpPollingActorParams> makeInvalid) { + + // original params should be valid + ValidationResult result = params.validate(CONTAINER); + assertTrue(fieldName, result.isValid()); + + // make invalid params + HttpPollingActorParams params2 = makeHttpPollingActorParams(); + makeInvalid.accept(params2); + result = params2.validate(CONTAINER); + assertFalse(fieldName, result.isValid()); + assertThat(result.getResult()).contains(CONTAINER).contains(fieldName).contains(expected); + } + + private HttpPollingActorParams makeHttpPollingActorParams() { + HttpPollingActorParams params2 = new HttpPollingActorParams(); + params2.setClientName(CLIENT); + params2.setTimeoutSec(TIMEOUT); + params2.setOperations(operations); + + params2.setPollWaitSec(POLL_WAIT_SEC); + params2.setMaxPolls(MAX_POLLS); + params2.setPollPath(POLL_PATH); + + return params2; + } +} diff --git a/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/parameters/HttpPollingConfigTest.java b/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/parameters/HttpPollingConfigTest.java new file mode 100644 index 000000000..53be6c711 --- /dev/null +++ b/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/parameters/HttpPollingConfigTest.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.actorserviceprovider.parameters; + +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 HttpPollingConfigTest { + private static final String MY_CLIENT = "my-client"; + private static final String MY_PATH = "my-path"; + private static final String POLL_PATH = "poll-path"; + private static final int TIMEOUT_SEC = 10; + private static final int MAX_POLLS = 20; + private static final int WAIT_SEC = 30; + + @Mock + private HttpClient client; + @Mock + private HttpClientFactory factory; + @Mock + private Executor executor; + + private HttpPollingParams params; + private HttpPollingConfig config; + + /** + * Sets up. + */ + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + + when(factory.get(MY_CLIENT)).thenReturn(client); + + params = HttpPollingParams.builder().maxPolls(MAX_POLLS).pollPath(POLL_PATH).pollWaitSec(WAIT_SEC) + .clientName(MY_CLIENT).path(MY_PATH).timeoutSec(TIMEOUT_SEC).build(); + config = new HttpPollingConfig(executor, params, factory); + } + + @Test + public void test() { + assertEquals(POLL_PATH + "/", config.getPollPath()); + assertEquals(MAX_POLLS, config.getMaxPolls()); + assertEquals(WAIT_SEC, config.getPollWaitSec()); + + // check value from superclass + assertSame(executor, config.getBlockingExecutor()); + assertSame(client, config.getClient()); + + // path with trailing "/" + params = params.toBuilder().pollPath(POLL_PATH + "/").build(); + config = new HttpPollingConfig(executor, params, factory); + assertEquals(POLL_PATH + "/", config.getPollPath()); + } +} diff --git a/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/parameters/HttpPollingParamsTest.java b/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/parameters/HttpPollingParamsTest.java new file mode 100644 index 000000000..e3fef83c5 --- /dev/null +++ b/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/parameters/HttpPollingParamsTest.java @@ -0,0 +1,91 @@ +/*- + * ============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.actorserviceprovider.parameters; + +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.actorserviceprovider.parameters.HttpPollingParams.HttpPollingParamsBuilder; + +public class HttpPollingParamsTest { + 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 POLL_PATH = "my-poll-path"; + private static final int MAX_POLLS = 3; + private static final int POLL_WAIT_SEC = 20; + private static final int TIMEOUT = 10; + + private HttpPollingParams params; + + @Before + public void setUp() { + params = HttpPollingParams.builder().pollPath(POLL_PATH).maxPolls(MAX_POLLS).pollWaitSec(POLL_WAIT_SEC) + .clientName(CLIENT).path(PATH).timeoutSec(TIMEOUT).build(); + } + + @Test + public void testValidate() { + assertTrue(params.validate(CONTAINER).isValid()); + + testValidateField("pollPath", "null", bldr -> bldr.pollPath(null)); + testValidateField("maxPolls", "minimum", bldr -> bldr.maxPolls(-1)); + testValidateField("pollWaitSec", "minimum", bldr -> bldr.pollWaitSec(-1)); + + // validate one of the superclass fields + testValidateField("clientName", "null", bldr -> bldr.clientName(null)); + + // check edge cases + assertTrue(params.toBuilder().maxPolls(0).build().validate(CONTAINER).isValid()); + assertFalse(params.toBuilder().pollWaitSec(0).build().validate(CONTAINER).isValid()); + assertTrue(params.toBuilder().pollWaitSec(1).build().validate(CONTAINER).isValid()); + } + + @Test + public void testBuilder_testToBuilder() { + assertEquals(CLIENT, params.getClientName()); + + assertEquals(POLL_PATH, params.getPollPath()); + assertEquals(MAX_POLLS, params.getMaxPolls()); + assertEquals(POLL_WAIT_SEC, params.getPollWaitSec()); + + assertEquals(params, params.toBuilder().build()); + } + + private void testValidateField(String fieldName, String expected, + Function<HttpPollingParamsBuilder<?,?>, HttpPollingParamsBuilder<?,?>> 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); + } +} |