X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=main%2Fsrc%2Ftest%2Fjava%2Forg%2Fonap%2Fpolicy%2Fapi%2Fmain%2Frest%2FTestApiRestServer.java;h=5896b3528604194aa5a9811e26938f655e2b7618;hb=c4cb758f37e353db3da474f9ae8199d139bcec55;hp=4f4dd6b896c300e393c6a0766bfbdce1db1e9cf0;hpb=177d52c481f21809792a6cd25629014c1fdf23b1;p=policy%2Fapi.git diff --git a/main/src/test/java/org/onap/policy/api/main/rest/TestApiRestServer.java b/main/src/test/java/org/onap/policy/api/main/rest/TestApiRestServer.java index 4f4dd6b8..5896b352 100644 --- a/main/src/test/java/org/onap/policy/api/main/rest/TestApiRestServer.java +++ b/main/src/test/java/org/onap/policy/api/main/rest/TestApiRestServer.java @@ -2,8 +2,8 @@ * ============LICENSE_START======================================================= * Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved. * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved. - * Modifications Copyright (C) 2019-2020,2022 Nordix Foundation. - * Modifications Copyright (C) 2020-2022 Bell Canada. All rights reserved. + * Modifications Copyright (C) 2019-2020, 2022-2023 Nordix Foundation. + * Modifications Copyright (C) 2020-2023 Bell Canada. 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. @@ -24,24 +24,21 @@ package org.onap.policy.api.main.rest; 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.assertNotNull; -import static org.junit.Assert.assertNull; - +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; + +import jakarta.ws.rs.client.Invocation; +import jakarta.ws.rs.core.Response; +import jakarta.ws.rs.core.Response.Status; import java.io.File; import java.io.IOException; import java.util.List; import java.util.Map; -import javax.ws.rs.client.Invocation; -import javax.ws.rs.core.Response; -import javax.ws.rs.core.Response.Status; -import org.junit.BeforeClass; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; import org.onap.policy.api.main.PolicyApiApplication; -import org.onap.policy.api.main.rest.provider.statistics.ApiStatisticsManager; -import org.onap.policy.api.main.rest.provider.statistics.StatisticsReport; import org.onap.policy.api.main.rest.utils.CommonTestRestController; import org.onap.policy.common.endpoints.report.HealthCheckReport; import org.onap.policy.common.utils.coder.StandardCoder; @@ -51,26 +48,23 @@ import org.onap.policy.common.utils.resources.TextFileUtils; import org.onap.policy.common.utils.security.SelfSignedKeyStore; import org.onap.policy.models.errors.concepts.ErrorResponse; import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.web.server.LocalServerPort; +import org.springframework.boot.test.web.server.LocalServerPort; import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.annotation.DirtiesContext.ClassMode; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.DynamicPropertyRegistry; import org.springframework.test.context.DynamicPropertySource; -import org.springframework.test.context.junit4.SpringRunner; /** * Class to perform unit test of {@link ApiRestController}. * * @author Chenfei Gao (cgao@research.att.com) */ -@RunWith(SpringRunner.class) @SpringBootTest(classes = PolicyApiApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) -@ActiveProfiles("test") +@ActiveProfiles({ "test", "default" }) @DirtiesContext(classMode = ClassMode.AFTER_CLASS) -public class TestApiRestServer extends CommonTestRestController { +class TestApiRestServer extends CommonTestRestController { private static final String ALIVE = "alive"; private static final String SELF = NetworkUtil.getHostname(); @@ -79,7 +73,6 @@ public class TestApiRestServer extends CommonTestRestController { private static final String APP_YAML = "application/yaml"; private static final String HEALTHCHECK_ENDPOINT = "healthcheck"; - private static final String STATISTICS_ENDPOINT = "statistics"; private static final String OP_POLICY_NAME_VCPE = "operational.restart"; @@ -114,7 +107,10 @@ public class TestApiRestServer extends CommonTestRestController { private static final String POLICIES = "policies"; + private static final String TOSCA_POLICY_VER_RESOURCE = + "policytypes/onap.restart.tca.snapshot.yaml"; // @formatter:off + private static final String[] TOSCA_POLICY_RESOURCE_NAMES = {"policies/vCPE.policy.monitoring.input.tosca.json", "policies/vCPE.policy.monitoring.input.tosca.yaml", "policies/vDNS.policy.monitoring.input.tosca.json", "policies/vDNS.policy.monitoring.input.tosca.v2.yaml"}; @@ -125,6 +121,9 @@ public class TestApiRestServer extends CommonTestRestController { private static final String TOSCA_POLICYTYPE_OP_RESOURCE = "policytypes/onap.policies.controlloop.operational.Common.yaml"; + private static final String TOSCA_POLICYTYPE_VER_RESOURCE = + "policytypes/onap.policies.monitoring.cdap.tca.hi.lo.app.snapshot.yaml"; + private static final String[] TOSCA_POLICYTYPE_RESOURCE_NAMES = { "policytypes/onap.policies.monitoring.tcagen2.yaml", "policytypes/onap.policies.monitoring.dcaegen2.collectors.datafile.datafile-app-server.yaml", @@ -159,17 +158,14 @@ public class TestApiRestServer extends CommonTestRestController { @LocalServerPort private int apiPort; - @Autowired - private ApiStatisticsManager mgr; - /** * Initializes parameters and set up test environment. * * @throws IOException on I/O exceptions * @throws InterruptedException if interrupted */ - @BeforeClass - public static void setupParameters() throws IOException, InterruptedException { + @BeforeAll + static void setupParameters() throws IOException, InterruptedException { keystore = new SelfSignedKeyStore(); } @@ -184,7 +180,12 @@ public class TestApiRestServer extends CommonTestRestController { } @Test - public void testCreatePolicyTypes() throws Exception { + void testSwagger() throws Exception { + super.testSwagger(apiPort); + } + + @Test + void testCreatePolicyTypes() throws Exception { for (String resrcName : TOSCA_POLICYTYPE_RESOURCE_NAMES) { Response rawResponse = createResource(POLICYTYPES, resrcName, apiPort); assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus()); @@ -206,7 +207,7 @@ public class TestApiRestServer extends CommonTestRestController { } @Test - public void testCreatePolicies() throws Exception { + void testCreatePolicies() throws Exception { for (String resrcName : TOSCA_POLICY_RESOURCE_NAMES) { Response rawResponse = createResource(POLICYTYPES_TCA_POLICIES, resrcName, apiPort); assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus()); @@ -231,7 +232,7 @@ public class TestApiRestServer extends CommonTestRestController { } @Test - public void testSimpleCreatePolicies() throws Exception { + void testSimpleCreatePolicies() throws Exception { for (String resrcName : TOSCA_POLICIES_RESOURCE_NAMES) { Response rawResponse = createResource(POLICIES, resrcName, apiPort); assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus()); @@ -255,9 +256,18 @@ public class TestApiRestServer extends CommonTestRestController { .contains("item \"policy type\" value \"IDontExist:1.0.0\" INVALID, not found"); } + @Test + void testPoliciesVersioning() throws Exception { + var rawResponse = createResource(POLICYTYPES, TOSCA_POLICYTYPE_VER_RESOURCE, apiPort); + assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus()); + + rawResponse = createResource(POLICIES, TOSCA_POLICY_VER_RESOURCE, apiPort); + assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus()); + } + @SuppressWarnings("unchecked") @Test - public void testToscaCompliantOpDroolsPolicies() throws Exception { + void testToscaCompliantOpDroolsPolicies() throws Exception { Response rawResponse = createResource(POLICYTYPES, TOSCA_POLICYTYPE_OP_RESOURCE, apiPort); assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus()); @@ -308,49 +318,29 @@ public class TestApiRestServer extends CommonTestRestController { } @Test - public void testHealthCheckSuccessJson() throws Exception { + void testHealthCheckSuccessJson() throws Exception { testHealthCheckSuccess(APP_JSON); } @Test - public void testHealthCheckSuccessYaml() throws Exception { + void testHealthCheckSuccessYaml() throws Exception { testHealthCheckSuccess(APP_YAML); } private void testHealthCheckSuccess(String mediaType) throws Exception { - final Invocation.Builder invocationBuilder = sendHttpsRequest(HEALTHCHECK_ENDPOINT, mediaType, apiPort); + final Invocation.Builder invocationBuilder = sendHttpsRequest( + HEALTHCHECK_ENDPOINT, mediaType, apiPort); final HealthCheckReport report = invocationBuilder.get(HealthCheckReport.class); validateHealthCheckReport(NAME, SELF, true, 200, ALIVE, report); } @Test - public void testApiStatistics_200_Json() throws Exception { - testApiStatistics_200(APP_JSON); - } - - @Test - public void testApiStatistics_200_Yaml() throws Exception { - testApiStatistics_200(APP_YAML); - } - - private void testApiStatistics_200(String mediaType) throws Exception { - Invocation.Builder invocationBuilder = sendHttpsRequest(STATISTICS_ENDPOINT, mediaType, apiPort); - StatisticsReport report = invocationBuilder.get(StatisticsReport.class); - validateStatisticsReport(report, 200); - updateApiStatistics(); - invocationBuilder = sendHttpsRequest(STATISTICS_ENDPOINT, mediaType, apiPort); - report = invocationBuilder.get(StatisticsReport.class); - validateStatisticsReport(report, 200); - // ApiStatisticsManager.resetAllStatistics(); - } - - @Test - public void testReadPolicyTypesJson() throws Exception { + void testReadPolicyTypesJson() throws Exception { testReadPolicyTypes(APP_JSON); } @Test - public void testReadPolicyTypesYaml() throws Exception { + void testReadPolicyTypesYaml() throws Exception { testReadPolicyTypes(APP_YAML); } @@ -401,12 +391,12 @@ public class TestApiRestServer extends CommonTestRestController { } @Test - public void testDeletePolicyTypeJson() throws Exception { + void testDeletePolicyTypeJson() throws Exception { testDeletePolicyType(APP_JSON); } @Test - public void testDeletePolicyTypeYaml() throws Exception { + void testDeletePolicyTypeYaml() throws Exception { testDeletePolicyType(APP_YAML); } @@ -432,12 +422,12 @@ public class TestApiRestServer extends CommonTestRestController { } @Test - public void testReadPoliciesJson() throws Exception { + void testReadPoliciesJson() throws Exception { testReadPolicies(APP_JSON); } @Test - public void testReadPoliciesYaml() throws Exception { + void testReadPoliciesYaml() throws Exception { testReadPolicies(APP_YAML); } @@ -465,7 +455,7 @@ public class TestApiRestServer extends CommonTestRestController { } @Test - public void testNamingPolicyGet() throws Exception { + void testNamingPolicyGet() throws Exception { Response rawResponse = readResource("policytypes/onap.policies.Naming/versions/1.0.0/" + "policies/SDNC_Policy.ONAP_NF_NAMING_TIMESTAMP/versions/1.0.0", APP_JSON, apiPort); @@ -529,12 +519,12 @@ public class TestApiRestServer extends CommonTestRestController { } @Test - public void testDeletePoliciesJson() throws Exception { + void testDeletePoliciesJson() throws Exception { testDeletePolicies(APP_JSON); } @Test - public void testDeletePoliciesYaml() throws Exception { + void testDeletePoliciesYaml() throws Exception { testDeletePolicies(APP_YAML); } @@ -546,12 +536,12 @@ public class TestApiRestServer extends CommonTestRestController { } @Test - public void testDeletePolicyVersionJson() throws Exception { + void testDeletePolicyVersionJson() throws Exception { testDeletePolicyVersion(APP_JSON); } @Test - public void testDeletePolicyVersionYaml() throws Exception { + void testDeletePolicyVersionYaml() throws Exception { testDeletePolicyVersion(APP_YAML); } @@ -587,12 +577,12 @@ public class TestApiRestServer extends CommonTestRestController { } @Test - public void testGetAllVersionOfPolicyJson() throws Exception { + void testGetAllVersionOfPolicyJson() throws Exception { testGetAllVersionOfPolicy(APP_JSON); } @Test - public void testGetAllVersionOfPolicyYaml() throws Exception { + void testGetAllVersionOfPolicyYaml() throws Exception { testGetAllVersionOfPolicy(APP_YAML); } @@ -613,12 +603,12 @@ public class TestApiRestServer extends CommonTestRestController { } @Test - public void testGetPoliciesJson() throws Exception { + void testGetPoliciesJson() throws Exception { getPolicies(APP_JSON); } @Test - public void testGetPoliciesYaml() throws Exception { + void testGetPoliciesYaml() throws Exception { getPolicies(APP_YAML); } @@ -641,12 +631,12 @@ public class TestApiRestServer extends CommonTestRestController { } @Test - public void testGetSpecificPolicyJson() throws Exception { + void testGetSpecificPolicyJson() throws Exception { getSpecificPolicy(APP_JSON); } @Test - public void testGetSpecificPolicyYaml() throws Exception { + void testGetSpecificPolicyYaml() throws Exception { getSpecificPolicy(APP_YAML); } @@ -669,7 +659,7 @@ public class TestApiRestServer extends CommonTestRestController { } @Test - public void testDeleteSpecificPolicy() throws Exception { + void testDeleteSpecificPolicy() throws Exception { Response rawResponse; for (String resrcName : TOSCA_POLICYTYPE_RESOURCE_NAMES) { rawResponse = createResource(POLICYTYPES, resrcName, apiPort); @@ -698,29 +688,6 @@ public class TestApiRestServer extends CommonTestRestController { } - private void updateApiStatistics() { - mgr.updateTotalApiCallCount(); - mgr.updateApiCallSuccessCount(); - mgr.updateApiCallFailureCount(); - mgr.updateTotalPolicyGetCount(); - mgr.updateTotalPolicyPostCount(); - mgr.updateTotalPolicyTypeGetCount(); - mgr.updateTotalPolicyTypePostCount(); - mgr.updatePolicyGetSuccessCount(); - mgr.updatePolicyGetFailureCount(); - mgr.updatePolicyPostSuccessCount(); - mgr.updatePolicyPostFailureCount(); - mgr.updatePolicyTypeGetSuccessCount(); - mgr.updatePolicyTypeGetFailureCount(); - mgr.updatePolicyTypePostSuccessCount(); - mgr.updatePolicyTypePostFailureCount(); - } - - private void validateStatisticsReport(final StatisticsReport report, final int code) { - - assertEquals(code, report.getCode()); - } - private void validateHealthCheckReport(final String name, final String url, final boolean healthy, final int code, final String message, final HealthCheckReport report) { @@ -730,4 +697,4 @@ public class TestApiRestServer extends CommonTestRestController { assertEquals(code, report.getCode()); assertEquals(message, report.getMessage()); } -} \ No newline at end of file +}