aboutsummaryrefslogtreecommitdiffstats
path: root/participant/participant-impl/participant-impl-http/src/test/java/org/onap/policy/clamp/controlloop
diff options
context:
space:
mode:
Diffstat (limited to 'participant/participant-impl/participant-impl-http/src/test/java/org/onap/policy/clamp/controlloop')
-rw-r--r--participant/participant-impl/participant-impl-http/src/test/java/org/onap/policy/clamp/controlloop/participant/http/handler/ClElementHandlerTest.java108
-rw-r--r--participant/participant-impl/participant-impl-http/src/test/java/org/onap/policy/clamp/controlloop/participant/http/rest/ActuatorControllerTest.java92
-rw-r--r--participant/participant-impl/participant-impl-http/src/test/java/org/onap/policy/clamp/controlloop/participant/http/utils/CommonActuatorController.java114
-rw-r--r--participant/participant-impl/participant-impl-http/src/test/java/org/onap/policy/clamp/controlloop/participant/http/utils/CommonTestData.java152
-rw-r--r--participant/participant-impl/participant-impl-http/src/test/java/org/onap/policy/clamp/controlloop/participant/http/utils/ToscaUtils.java51
-rw-r--r--participant/participant-impl/participant-impl-http/src/test/java/org/onap/policy/clamp/controlloop/participant/http/webclient/ClHttpClientTest.java129
6 files changed, 0 insertions, 646 deletions
diff --git a/participant/participant-impl/participant-impl-http/src/test/java/org/onap/policy/clamp/controlloop/participant/http/handler/ClElementHandlerTest.java b/participant/participant-impl/participant-impl-http/src/test/java/org/onap/policy/clamp/controlloop/participant/http/handler/ClElementHandlerTest.java
deleted file mode 100644
index 08f008ef3..000000000
--- a/participant/participant-impl/participant-impl-http/src/test/java/org/onap/policy/clamp/controlloop/participant/http/handler/ClElementHandlerTest.java
+++ /dev/null
@@ -1,108 +0,0 @@
-/*-
- * ============LICENSE_START=======================================================
- * Copyright (C) 2021-2022 Nordix Foundation.
- * ================================================================================
- * 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.
- *
- * SPDX-License-Identifier: Apache-2.0
- * ============LICENSE_END=========================================================
- */
-
-package org.onap.policy.clamp.controlloop.participant.http.handler;
-
-import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
-import static org.mockito.ArgumentMatchers.any;
-import static org.mockito.Mockito.doNothing;
-
-import java.io.IOException;
-import java.util.Map;
-import java.util.concurrent.ExecutionException;
-import org.junit.jupiter.api.BeforeAll;
-import org.junit.jupiter.api.Test;
-import org.junit.jupiter.api.extension.ExtendWith;
-import org.mockito.InjectMocks;
-import org.mockito.Mock;
-import org.mockito.Mockito;
-import org.mockito.Spy;
-import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
-import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState;
-import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState;
-import org.onap.policy.clamp.controlloop.participant.http.main.handler.ControlLoopElementHandler;
-import org.onap.policy.clamp.controlloop.participant.http.main.models.ConfigRequest;
-import org.onap.policy.clamp.controlloop.participant.http.utils.CommonTestData;
-import org.onap.policy.clamp.controlloop.participant.http.utils.ToscaUtils;
-import org.onap.policy.clamp.controlloop.participant.intermediary.api.ParticipantIntermediaryApi;
-import org.onap.policy.common.utils.coder.CoderException;
-import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
-import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
-import org.springframework.test.context.junit.jupiter.SpringExtension;
-
-
-@ExtendWith(SpringExtension.class)
-class ClElementHandlerTest {
-
- @InjectMocks
- @Spy
- private ControlLoopElementHandler controlLoopElementHandler = new ControlLoopElementHandler();
-
- @Mock
- private ParticipantIntermediaryApi participantIntermediaryApi;
-
- private CommonTestData commonTestData = new CommonTestData();
-
- private static ToscaServiceTemplate serviceTemplate;
- private static final String HTTP_CONTROL_LOOP_ELEMENT =
- "org.onap.domain.database.Http_PMSHMicroserviceControlLoopElement";
-
- @BeforeAll
- static void init() throws CoderException {
- serviceTemplate = ToscaUtils.readControlLoopFromTosca();
- }
-
- @Test
- void test_controlLoopElementeStateChange() throws IOException {
- var controlLoopId = commonTestData.getControlLoopId();
- var element = commonTestData.getControlLoopElement();
- var controlLoopElementId = element.getId();
-
- var config = Mockito.mock(ConfigRequest.class);
- assertDoesNotThrow(() -> controlLoopElementHandler.invokeHttpClient(config));
-
- assertDoesNotThrow(() -> controlLoopElementHandler
- .controlLoopElementStateChange(controlLoopId,
- controlLoopElementId, ControlLoopState.PASSIVE, ControlLoopOrderedState.PASSIVE));
-
- assertDoesNotThrow(() -> controlLoopElementHandler
- .controlLoopElementStateChange(controlLoopId,
- controlLoopElementId, ControlLoopState.PASSIVE, ControlLoopOrderedState.UNINITIALISED));
-
- assertDoesNotThrow(() -> controlLoopElementHandler
- .controlLoopElementStateChange(controlLoopId,
- controlLoopElementId, ControlLoopState.PASSIVE, ControlLoopOrderedState.RUNNING));
-
- controlLoopElementHandler.close();
- }
-
- @Test
- void test_ControlLoopElementUpdate() throws ExecutionException, InterruptedException {
- doNothing().when(controlLoopElementHandler).invokeHttpClient(any());
- ControlLoopElement element = commonTestData.getControlLoopElement();
-
- Map<String, ToscaNodeTemplate> nodeTemplatesMap =
- serviceTemplate.getToscaTopologyTemplate().getNodeTemplates();
-
- assertDoesNotThrow(() -> controlLoopElementHandler
- .controlLoopElementUpdate(commonTestData.getControlLoopId(), element,
- nodeTemplatesMap.get(HTTP_CONTROL_LOOP_ELEMENT)));
- }
-}
diff --git a/participant/participant-impl/participant-impl-http/src/test/java/org/onap/policy/clamp/controlloop/participant/http/rest/ActuatorControllerTest.java b/participant/participant-impl/participant-impl-http/src/test/java/org/onap/policy/clamp/controlloop/participant/http/rest/ActuatorControllerTest.java
deleted file mode 100644
index 8efff8f6a..000000000
--- a/participant/participant-impl/participant-impl-http/src/test/java/org/onap/policy/clamp/controlloop/participant/http/rest/ActuatorControllerTest.java
+++ /dev/null
@@ -1,92 +0,0 @@
-/*-
- * ============LICENSE_START=======================================================
- * Copyright (C) 2021 Nordix Foundation.
- * ================================================================================
- * 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.
- *
- * SPDX-License-Identifier: Apache-2.0
- * ============LICENSE_END=========================================================
- */
-
-package org.onap.policy.clamp.controlloop.participant.http.rest;
-
-import static org.junit.jupiter.api.Assertions.assertEquals;
-
-import javax.ws.rs.client.Invocation;
-import javax.ws.rs.core.Response;
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Test;
-import org.junit.jupiter.api.extension.ExtendWith;
-import org.onap.policy.clamp.controlloop.participant.http.utils.CommonActuatorController;
-import org.springframework.boot.test.autoconfigure.actuate.metrics.AutoConfigureMetrics;
-import org.springframework.boot.test.context.SpringBootTest;
-import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
-import org.springframework.boot.web.server.LocalServerPort;
-import org.springframework.test.context.TestPropertySource;
-import org.springframework.test.context.junit.jupiter.SpringExtension;
-
-@AutoConfigureMetrics
-@ExtendWith(SpringExtension.class)
-@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
-@TestPropertySource(locations = {"classpath:application_test.properties"})
-class ActuatorControllerTest extends CommonActuatorController {
-
- private static final String HEALTH_ENDPOINT = "health";
- private static final String METRICS_ENDPOINT = "metrics";
- private static final String PROMETHEUS_ENDPOINT = "prometheus";
-
- @LocalServerPort
- private int randomServerPort;
-
- @BeforeEach
- public void setUpPort() {
- super.setHttpPrefix(randomServerPort);
- }
-
- @Test
- void testGetHealth_Unauthorized() throws Exception {
- assertUnauthorizedActGet(HEALTH_ENDPOINT);
- }
-
- @Test
- void testGetMetrics_Unauthorized() throws Exception {
- assertUnauthorizedActGet(METRICS_ENDPOINT);
- }
-
- @Test
- void testGetPrometheus_Unauthorized() throws Exception {
- assertUnauthorizedActGet(PROMETHEUS_ENDPOINT);
- }
-
- @Test
- void testGetHealth() throws Exception {
- Invocation.Builder invocationBuilder = super.sendActRequest(HEALTH_ENDPOINT);
- Response rawresp = invocationBuilder.buildGet().invoke();
- assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
- }
-
- @Test
- void testGetMetrics() throws Exception {
- Invocation.Builder invocationBuilder = super.sendActRequest(METRICS_ENDPOINT);
- Response rawresp = invocationBuilder.buildGet().invoke();
- assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
- }
-
- @Test
- void testGePrometheus() throws Exception {
- Invocation.Builder invocationBuilder = super.sendActRequest(PROMETHEUS_ENDPOINT);
- Response rawresp = invocationBuilder.buildGet().invoke();
- assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
- }
-
-}
diff --git a/participant/participant-impl/participant-impl-http/src/test/java/org/onap/policy/clamp/controlloop/participant/http/utils/CommonActuatorController.java b/participant/participant-impl/participant-impl-http/src/test/java/org/onap/policy/clamp/controlloop/participant/http/utils/CommonActuatorController.java
deleted file mode 100644
index 5ebce8126..000000000
--- a/participant/participant-impl/participant-impl-http/src/test/java/org/onap/policy/clamp/controlloop/participant/http/utils/CommonActuatorController.java
+++ /dev/null
@@ -1,114 +0,0 @@
-/*-
- * ============LICENSE_START=======================================================
- * Copyright (C) 2021 Nordix Foundation.
- * ================================================================================
- * 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.
- *
- * SPDX-License-Identifier: Apache-2.0
- * ============LICENSE_END=========================================================
- */
-
-package org.onap.policy.clamp.controlloop.participant.http.utils;
-
-import static org.junit.Assert.assertEquals;
-
-import javax.ws.rs.client.Client;
-import javax.ws.rs.client.ClientBuilder;
-import javax.ws.rs.client.Invocation;
-import javax.ws.rs.client.WebTarget;
-import javax.ws.rs.core.MediaType;
-import javax.ws.rs.core.Response;
-import org.glassfish.jersey.client.ClientProperties;
-import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
-import org.onap.policy.common.gson.GsonMessageBodyHandler;
-import org.onap.policy.common.utils.network.NetworkUtil;
-
-/**
- * Class to perform Rest unit tests.
- *
- */
-public class CommonActuatorController {
-
- public static final String SELF = NetworkUtil.getHostname();
- public static final String CONTEXT_PATH = "onap/httpparticipant";
- public static final String ACTUATOR_ENDPOINT = CONTEXT_PATH + "/actuator/";
-
- private static String httpPrefix;
-
- /**
- * Sends a request to an actuator endpoint.
- *
- * @param endpoint the target endpoint
- * @return a request builder
- * @throws Exception if an error occurs
- */
- protected Invocation.Builder sendActRequest(final String endpoint) throws Exception {
- return sendFqeRequest(httpPrefix + ACTUATOR_ENDPOINT + endpoint, true);
- }
-
- /**
- * Sends a request to an actuator endpoint, without any authorization header.
- *
- * @param endpoint the target endpoint
- * @return a request builder
- * @throws Exception if an error occurs
- */
- protected Invocation.Builder sendNoAuthActRequest(final String endpoint) throws Exception {
- return sendFqeRequest(httpPrefix + ACTUATOR_ENDPOINT + endpoint, false);
- }
-
- /**
- * Sends a request to a fully qualified endpoint.
- *
- * @param fullyQualifiedEndpoint the fully qualified target endpoint
- * @param includeAuth if authorization header should be included
- * @return a request builder
- * @throws Exception if an error occurs
- */
- protected Invocation.Builder sendFqeRequest(final String fullyQualifiedEndpoint, boolean includeAuth)
- throws Exception {
- final Client client = ClientBuilder.newBuilder().build();
-
- client.property(ClientProperties.METAINF_SERVICES_LOOKUP_DISABLE, "true");
- client.register(GsonMessageBodyHandler.class);
-
- if (includeAuth) {
- client.register(HttpAuthenticationFeature.basic("participantUser", "zb!XztG34"));
- }
-
- final WebTarget webTarget = client.target(fullyQualifiedEndpoint);
-
- return webTarget.request(MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN);
- }
-
- /**
- * Assert that GET call to actuator endpoint is Unauthorized.
- *
- * @param endPoint the endpoint
- * @throws Exception if an error occurs
- */
- protected void assertUnauthorizedActGet(final String endPoint) throws Exception {
- Response rawresp = sendNoAuthActRequest(endPoint).buildGet().invoke();
- assertEquals(Response.Status.UNAUTHORIZED.getStatusCode(), rawresp.getStatus());
- }
-
- /**
- * Set Up httpPrefix.
- *
- * @param port the port
- */
- protected void setHttpPrefix(int port) {
- httpPrefix = "http://" + SELF + ":" + port + "/";
- }
-
-}
diff --git a/participant/participant-impl/participant-impl-http/src/test/java/org/onap/policy/clamp/controlloop/participant/http/utils/CommonTestData.java b/participant/participant-impl/participant-impl-http/src/test/java/org/onap/policy/clamp/controlloop/participant/http/utils/CommonTestData.java
deleted file mode 100644
index 1f92a86e0..000000000
--- a/participant/participant-impl/participant-impl-http/src/test/java/org/onap/policy/clamp/controlloop/participant/http/utils/CommonTestData.java
+++ /dev/null
@@ -1,152 +0,0 @@
-/*-
- * ============LICENSE_START=======================================================
- * Copyright (C) 2021 Nordix Foundation.
- * Modifications Copyright (C) 2021 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.
- *
- * SPDX-License-Identifier: Apache-2.0
- * ============LICENSE_END=========================================================
- */
-
-package org.onap.policy.clamp.controlloop.participant.http.utils;
-
-import java.util.List;
-import java.util.Map;
-import java.util.UUID;
-import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
-import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState;
-import org.onap.policy.clamp.controlloop.participant.http.main.models.ConfigurationEntity;
-import org.onap.policy.clamp.controlloop.participant.http.main.models.RestParams;
-import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
-
-public class CommonTestData {
-
- private static final String TEST_KEY_NAME = "org.onap.domain.database.Http_PMSHMicroserviceControlLoopElement";
-
-
- /**
- * Get a controlLoop Element.
- * @return controlLoopElement object
- */
- public ControlLoopElement getControlLoopElement() {
- ControlLoopElement element = new ControlLoopElement();
- element.setId(UUID.randomUUID());
- element.setDefinition(new ToscaConceptIdentifier(TEST_KEY_NAME, "1.0.1"));
- element.setOrderedState(ControlLoopOrderedState.PASSIVE);
- return element;
- }
-
- /**
- * Get query params.
- * @return Map of query params
- */
- public Map<String, String> getQueryParams() {
- return Map.of("name", "dummy", "version", "1.0");
- }
-
- /**
- * Get path params.
- * @return Map of path params
- */
- public Map<String, Object> getPathParams() {
- return Map.of("id", "123", "name", "dummy");
- }
-
- /**
- * Rest params with GET request.
- * @return RestParams obj
- */
- public RestParams restParamsWithGet() {
- return new RestParams(
- new ToscaConceptIdentifier("getRequest", "1.0"),
- "GET",
- "get",
- 200,
- null,
- getQueryParams(),
- null
- );
- }
-
- /**
- * Rest params with POST request.
- * @return RestParams obj
- */
- public RestParams restParamsWithPost() {
- return new RestParams(
- new ToscaConceptIdentifier("postRequest", "1.0"),
- "POST",
- "post",
- 200,
- null,
- getQueryParams(),
- "Test body"
- );
- }
-
- /**
- * Rest params with POST request.
- * @return RestParams obj
- */
- public RestParams restParamsWithInvalidPost() {
- return new RestParams(
- new ToscaConceptIdentifier("postRequest", "1.0"),
- "POST",
- "post/{id}/{name}",
- 200,
- getPathParams(),
- getQueryParams(),
- "Test body"
- );
- }
-
- /**
- * Get invalid configuration entity.
- * @return ConfigurationEntity obj
- */
- public ConfigurationEntity getInvalidConfigurationEntity() {
- return new ConfigurationEntity(
- new ToscaConceptIdentifier("config1", "1.0.1"),
- List.of(restParamsWithGet(), restParamsWithInvalidPost())
- );
- }
-
- /**
- * Get configuration entity.
- * @return ConfigurationEntity obj
- */
- public ConfigurationEntity getConfigurationEntity() {
- return new ConfigurationEntity(
- new ToscaConceptIdentifier("config1", "1.0.1"),
- List.of(restParamsWithGet(), restParamsWithPost())
- );
- }
-
- /**
- * Get controlloop id.
- * @return ToscaConceptIdentifier controlLoopId
- */
- public ToscaConceptIdentifier getControlLoopId() {
- return new ToscaConceptIdentifier("PMSHInstance0", "1.0.0");
- }
-
- /**
- * Get headers for config request.
- * @return Map of headers
- */
- public Map<String, String> getHeaders() {
- return Map.of("Content-Type", "application/json", "Accept", "application/json");
- }
-
-}
diff --git a/participant/participant-impl/participant-impl-http/src/test/java/org/onap/policy/clamp/controlloop/participant/http/utils/ToscaUtils.java b/participant/participant-impl/participant-impl-http/src/test/java/org/onap/policy/clamp/controlloop/participant/http/utils/ToscaUtils.java
deleted file mode 100644
index fdba28c0e..000000000
--- a/participant/participant-impl/participant-impl-http/src/test/java/org/onap/policy/clamp/controlloop/participant/http/utils/ToscaUtils.java
+++ /dev/null
@@ -1,51 +0,0 @@
-/*-
- * ============LICENSE_START=======================================================
- * Copyright (C) 2021 Nordix Foundation.
- * ================================================================================
- * 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.
- *
- * SPDX-License-Identifier: Apache-2.0
- * ============LICENSE_END=========================================================
- */
-
-package org.onap.policy.clamp.controlloop.participant.http.utils;
-
-import lombok.AccessLevel;
-import lombok.NoArgsConstructor;
-import org.onap.policy.common.utils.coder.YamlJsonTranslator;
-import org.onap.policy.common.utils.resources.ResourceUtils;
-import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
-
-/**
- * Util class for Test scope.
- */
-@NoArgsConstructor(access = AccessLevel.PRIVATE)
-public final class ToscaUtils {
-
- private static final YamlJsonTranslator yamlTranslator = new YamlJsonTranslator();
- private static final String TOSCA_TEMPLATE_YAML = "src/test/resources/HttpParticipantConfig.yaml";
-
-
- /**
- * Read a service template yaml.
- * @return ToscaServiceTemplate
- */
- public static ToscaServiceTemplate readControlLoopFromTosca() {
- return serializeControlLoopYaml(TOSCA_TEMPLATE_YAML);
- }
-
- private static ToscaServiceTemplate serializeControlLoopYaml(String controlLoopFilePath) {
- String controlLoopString = ResourceUtils.getResourceAsString(controlLoopFilePath);
- return yamlTranslator.fromYaml(controlLoopString, ToscaServiceTemplate.class);
- }
-}
diff --git a/participant/participant-impl/participant-impl-http/src/test/java/org/onap/policy/clamp/controlloop/participant/http/webclient/ClHttpClientTest.java b/participant/participant-impl/participant-impl-http/src/test/java/org/onap/policy/clamp/controlloop/participant/http/webclient/ClHttpClientTest.java
deleted file mode 100644
index e3ff9dbba..000000000
--- a/participant/participant-impl/participant-impl-http/src/test/java/org/onap/policy/clamp/controlloop/participant/http/webclient/ClHttpClientTest.java
+++ /dev/null
@@ -1,129 +0,0 @@
-/*-
- * ============LICENSE_START=======================================================
- * Copyright (C) 2021 Nordix Foundation.
- * ================================================================================
- * 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.
- *
- * SPDX-License-Identifier: Apache-2.0
- * ============LICENSE_END=========================================================
- */
-
-package org.onap.policy.clamp.controlloop.participant.http.webclient;
-
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
-import static org.mockserver.model.HttpRequest.request;
-import static org.mockserver.model.HttpResponse.response;
-
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import javax.ws.rs.core.MediaType;
-import org.apache.commons.lang3.tuple.Pair;
-import org.junit.jupiter.api.AfterAll;
-import org.junit.jupiter.api.BeforeAll;
-import org.junit.jupiter.api.Test;
-import org.junit.jupiter.api.extension.ExtendWith;
-import org.mockserver.integration.ClientAndServer;
-import org.mockserver.model.Parameter;
-import org.onap.policy.clamp.controlloop.participant.http.main.models.ConfigRequest;
-import org.onap.policy.clamp.controlloop.participant.http.main.models.ConfigurationEntity;
-import org.onap.policy.clamp.controlloop.participant.http.main.webclient.ClHttpClient;
-import org.onap.policy.clamp.controlloop.participant.http.utils.CommonTestData;
-import org.onap.policy.common.utils.network.NetworkUtil;
-import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
-import org.springframework.test.context.junit.jupiter.SpringExtension;
-
-@ExtendWith(SpringExtension.class)
-class ClHttpClientTest {
-
- private static CommonTestData commonTestData;
-
- private static int mockServerPort;
-
- private String testMockUrl = "http://localhost";
-
- private Map<ToscaConceptIdentifier, Pair<Integer, String>> responseMap = new HashMap<>();
-
- private static ClientAndServer mockServer;
-
- /**
- * Set up Mock server.
- */
- @BeforeAll
- static void setUpMockServer() throws IOException {
- mockServerPort = NetworkUtil.allocPort();
- mockServer = ClientAndServer.startClientAndServer(mockServerPort);
- commonTestData = new CommonTestData();
- List<Parameter> queryParams = new ArrayList<>();
- commonTestData.getQueryParams().forEach((k, v) -> queryParams.add(new Parameter(k, v)));
-
- mockServer.when(request().withMethod("GET").withPath("/get")
- .withHeader("Content-type", MediaType.APPLICATION_JSON)
- .withHeader("Accept", MediaType.APPLICATION_JSON).withQueryStringParameters(queryParams))
- .respond(response().withBody("dummy body").withStatusCode(200)
- .withHeader("Content-Type", MediaType.APPLICATION_JSON));
-
- mockServer.when(request().withMethod("POST").withPath("/post")
- .withHeader("Content-type", MediaType.APPLICATION_JSON)
- .withHeader("Accept", MediaType.APPLICATION_JSON).withQueryStringParameters(queryParams)
- .withBody("Test body"))
- .respond(response().withStatusCode(200));
- }
-
- @AfterAll
- public static void stopServer() {
- mockServer.stop();
- mockServer = null;
- }
-
-
- @Test
- void test_validRequest() {
- //Add valid rest requests POST, GET
- ConfigurationEntity configurationEntity = commonTestData.getConfigurationEntity();
-
- Map<String, String> headers = commonTestData.getHeaders();
- ConfigRequest configRequest = new ConfigRequest(testMockUrl + ":" + mockServerPort, headers,
- List.of(configurationEntity), 10);
-
- ClHttpClient client = new ClHttpClient(configRequest, responseMap);
- assertDoesNotThrow(client::run);
- assertThat(responseMap).hasSize(2).containsKey(commonTestData
- .restParamsWithGet().getRestRequestId());
-
- Pair<Integer, String> restResponseMap = responseMap.get(commonTestData
- .restParamsWithGet().getRestRequestId());
- assertThat(restResponseMap.getKey()).isEqualTo(200);
- }
-
- @Test
- void test_invalidRequest() {
- //Add rest requests Invalid POST, Valid GET
- ConfigurationEntity configurationEntity = commonTestData.getInvalidConfigurationEntity();
-
- Map<String, String> headers = commonTestData.getHeaders();
- ConfigRequest configRequest = new ConfigRequest(testMockUrl + ":" + mockServerPort, headers,
- List.of(configurationEntity), 10);
-
- ClHttpClient client = new ClHttpClient(configRequest, responseMap);
- assertDoesNotThrow(client::run);
- assertThat(responseMap).hasSize(2).containsKey(commonTestData
- .restParamsWithGet().getRestRequestId());
- Pair<Integer, String> response = responseMap
- .get(commonTestData.restParamsWithInvalidPost().getRestRequestId());
- assertThat(response.getKey()).isEqualTo(404);
- }
-}