diff options
author | a.sreekumar <ajith.sreekumar@est.tech> | 2020-02-25 16:49:33 +0000 |
---|---|---|
committer | a.sreekumar <ajith.sreekumar@est.tech> | 2020-02-27 09:50:48 +0000 |
commit | 6ddf46e995be34cc03d92a41948e14dfc5c9f4f9 (patch) | |
tree | 505ca1d25eba0bf62635a10e0eebd0d4449178a6 /testsuites/integration/integration-uservice-test/src/test/java | |
parent | aacc7442f046d44359934ea3d93f425a809e7616 (diff) |
Add integration tests for Parameter passing in APEX
Change-Id: I5d01766ffb0104f632bebc9b553e7dca83b4829f
Issue-ID: POLICY-2364
Signed-off-by: a.sreekumar <ajith.sreekumar@est.tech>
Diffstat (limited to 'testsuites/integration/integration-uservice-test/src/test/java')
2 files changed, 282 insertions, 0 deletions
diff --git a/testsuites/integration/integration-uservice-test/src/test/java/org/onap/policy/apex/testsuites/integration/uservice/taskparameters/RestClientEndpointForTaskParameters.java b/testsuites/integration/integration-uservice-test/src/test/java/org/onap/policy/apex/testsuites/integration/uservice/taskparameters/RestClientEndpointForTaskParameters.java new file mode 100644 index 000000000..f0b670008 --- /dev/null +++ b/testsuites/integration/integration-uservice-test/src/test/java/org/onap/policy/apex/testsuites/integration/uservice/taskparameters/RestClientEndpointForTaskParameters.java @@ -0,0 +1,105 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2020 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.apex.testsuites.integration.uservice.taskparameters; + +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.core.Response; + +/** + * The Class RestClientEndpointForTaskParameters. + */ +@Path("/apex") +public class RestClientEndpointForTaskParameters { + + private static String closedLoopId; + private static String serviceId; + + /** + * Get event that triggers policy for testing TaskParameters. + * + * @return the response + */ + @Path("/event/GetEvent") + @GET + public Response getEvent() { + return Response.status(200).entity("{\"event\": \"CLTriggerEvent\"}").build(); + } + + /** + * Fetch information of service using serviceId. + * + * @param servicId the service id + * + * @return the response + */ + @Path("/service/getInfoForServiceId/{servicId}") + @POST + public Response getInfoForServiceId(@PathParam("servicId") String servicId) { + serviceId = servicId; + return Response.status(200) + .entity("{\"name\": \"ServiceInfoEvent\", \"serviceDetails\": \"serviceDetailsFullBody\"}").build(); + } + + /** + * Closed loop action using closedLoopId. + * + * @param closedLpId the closedLoopId + * + * @return the response + */ + @Path("/action/doActionForCL/{closedLpId}") + @POST + public Response doActionForCL(@PathParam("closedLpId") String closedLpId) { + closedLoopId = closedLpId; + return Response.status(200).entity("{\"name\": \"CLOutputEvent\", \"status\": \"ClosedLoop Success\"}").build(); + } + + /** + * Get details that are set as part of the policy execution. + * + * @return the response + */ + @Path("/event/getDetails") + @GET + public Response getDetails() { + if (null == serviceId || null == closedLoopId) { + return Response.status(500).entity("Error: Flow incomplete").build(); + } + return Response.status(200).entity("{\"closedLoopId\": " + closedLoopId + ",\"serviceId\": " + serviceId + "}") + .build(); + } + + /** + * Clear details that are set as part of the policy execution. + * + * @return the response + */ + @Path("/event/clearDetails") + @GET + public Response clearDetails() { + closedLoopId = null; + serviceId = null; + return Response.status(200).entity("Details cleared.").build(); + } +} diff --git a/testsuites/integration/integration-uservice-test/src/test/java/org/onap/policy/apex/testsuites/integration/uservice/taskparameters/TestTaskParameters.java b/testsuites/integration/integration-uservice-test/src/test/java/org/onap/policy/apex/testsuites/integration/uservice/taskparameters/TestTaskParameters.java new file mode 100644 index 000000000..da93f919d --- /dev/null +++ b/testsuites/integration/integration-uservice-test/src/test/java/org/onap/policy/apex/testsuites/integration/uservice/taskparameters/TestTaskParameters.java @@ -0,0 +1,177 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2020 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.apex.testsuites.integration.uservice.taskparameters; + +import static org.awaitility.Awaitility.await; +import static org.junit.Assert.assertTrue; + +import java.util.concurrent.TimeUnit; +import javax.ws.rs.client.Client; +import javax.ws.rs.client.ClientBuilder; +import javax.ws.rs.core.Response; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.onap.policy.apex.auth.clieditor.ApexCommandLineEditorMain; +import org.onap.policy.apex.model.basicmodel.concepts.ApexException; +import org.onap.policy.apex.service.engine.main.ApexMain; +import org.onap.policy.common.endpoints.http.server.HttpServletServer; +import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance; +import org.onap.policy.common.gson.GsonMessageBodyHandler; +import org.onap.policy.common.utils.network.NetworkUtil; +import org.slf4j.ext.XLogger; +import org.slf4j.ext.XLoggerFactory; + +/** + * This class runs integration tests for taskParameters. + * Task parameters are read from the ApexConfig, and they can be accessed in task logic. + * In this case, the taskParameters are used to set values in executionProperties. + * URL dynamically populated using executionProperties is hit and values get updated in + * {@link RestClientEndpointForTaskParameters} which acts as a temporary server for requests. + */ +public class TestTaskParameters { + + private static final XLogger LOGGER = XLoggerFactory.getXLogger(TestTaskParameters.class); + + private static HttpServletServer server; + private static final int PORT = 32801; + private static final String HOST = "localhost"; + + /** + * Compile the policy. + */ + @BeforeClass + public static void compilePolicy() { + // @formatter:off + final String[] cliArgs = { + "-c", + "src/test/resources/policies/taskparameters/TaskParametersTestPolicyModel.apex", + "-l", + "target/TaskParametersTestPolicyModel.log", + "-o", + "target/TaskParametersTestPolicyModel.json" + }; + // @formatter:on + + new ApexCommandLineEditorMain(cliArgs); + } + + /** + * Sets up a server for testing. + * + * @throws Exception the exception + */ + @BeforeClass + public static void setUp() throws Exception { + if (NetworkUtil.isTcpPortOpen(HOST, PORT, 3, 50L)) { + throw new IllegalStateException("port " + PORT + " is still in use"); + } + + server = HttpServletServerFactoryInstance.getServerFactory().build("TestTaskParameters", false, null, PORT, + "/TestTaskParametersRest", false, false); + + server.addServletClass(null, RestClientEndpointForTaskParameters.class.getName()); + server.setSerializationProvider(GsonMessageBodyHandler.class.getName()); + + server.start(); + + if (!NetworkUtil.isTcpPortOpen(HOST, PORT, 60, 500L)) { + throw new IllegalStateException("port " + PORT + " is still not in use"); + } + + } + + /** + * Tear down. + * + * @throws Exception the exception + */ + @AfterClass + public static void tearDown() throws Exception { + if (server != null) { + server.stop(); + } + } + + /** + * Clear relative file root environment variable. + */ + @Before + public void clearRelativeFileRoot() { + System.clearProperty("APEX_RELATIVE_FILE_ROOT"); + } + + /** + * Test taskParameters with no taskIds. + * When taskIds are not provided, all taskParameters provided in config will be updated to all tasks. + */ + @Test + public void testTaskParameters_with_noTaskIds() throws Exception { + String responseEntity = testTaskParameters( + "src/test/resources/testdata/taskparameters/TaskParameterTestConfig_with_noTaskIds.json"); + assertTrue(responseEntity.contains("{\"closedLoopId\": closedLoopId123,\"serviceId\": serviceId123}")); + } + + /** + * Test taskParameters with valid taskIds. + * When valid taskIds are provided, the the taskParameter will be updated in that particular task alone. + */ + @Test + public void testTaskParameters_with_validTaskIds() throws Exception { + String responseEntity = testTaskParameters( + "src/test/resources/testdata/taskparameters/TaskParameterTestConfig_with_validTaskIds.json"); + assertTrue(responseEntity.contains("{\"closedLoopId\": closedLoopIdxyz,\"serviceId\": serviceIdxyz}")); + } + + /** + * Test taskParameters with invalid taskIds. + * When invalid taskIds are provided, or when a taskParameter assigned to a particular taskId is tried to be + * accessed in a taskLogic of a different task, such taskParameters won't be accessible in the task + */ + @Test + public void testTaskParameters_with_invalidTaskIds() throws Exception { + String responseEntity = testTaskParameters( + "src/test/resources/testdata/taskparameters/TaskParameterTestConfig_with_invalidTaskIds.json"); + assertTrue(responseEntity.contains("{\"closedLoopId\": INVALID - closedLoopId not available in TaskParameters," + + "\"serviceId\": INVALID - serviceId not available in TaskParameters}")); + } + + private String testTaskParameters(String apexConfigPath) throws ApexException { + final Client client = ClientBuilder.newClient(); + final String[] args = {apexConfigPath}; + // clear the details set in server + client.target("http://" + HOST + ":" + PORT + "/TestTaskParametersRest/apex/event/clearDetails") + .request("application/json").get(); + final ApexMain apexMain = new ApexMain(args); + + String getDetailsUrl = "http://" + HOST + ":" + PORT + "/TestTaskParametersRest/apex/event/getDetails"; + // wait for success response code to be received, until a timeout + await().atMost(2000, TimeUnit.MILLISECONDS) + .until(() -> 200 == client.target(getDetailsUrl).request("application/json").get().getStatus()); + apexMain.shutdown(); + Response response = client.target(getDetailsUrl).request("application/json").get(); + String responseEntity = response.readEntity(String.class); + + LOGGER.info("testTaskParameters-OUTSTRING=\n {}", responseEntity); + return responseEntity; + } +} |