From d42ca06bba29863d176ac6bc57a4ff34c9fd781a Mon Sep 17 00:00:00 2001 From: Bilal A Date: Wed, 14 Aug 2019 15:03:07 -0400 Subject: Adding more policy-api junits Issue-ID: POLICY-1771 Signed-off-by: Bilal A Change-Id: If85abb8280e28da7391bb4b4cb502473a404065b --- .../policy/api/main/rest/TestApiRestServer.java | 297 ++++++++++++++++++++- 1 file changed, 296 insertions(+), 1 deletion(-) 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 442f9dba..81547295 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 @@ -33,6 +33,7 @@ import java.lang.reflect.Constructor; import java.lang.reflect.Modifier; import java.security.SecureRandom; import java.security.cert.X509Certificate; +import java.util.Base64; import java.util.Properties; import javax.net.ssl.SSLContext; @@ -52,16 +53,22 @@ import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature; import org.junit.After; import org.junit.Test; import org.onap.policy.api.main.exception.PolicyApiException; +import org.onap.policy.api.main.parameters.ApiParameterGroup; import org.onap.policy.api.main.parameters.CommonTestData; +import org.onap.policy.api.main.rest.provider.PolicyProvider; +import org.onap.policy.api.main.rest.provider.PolicyTypeProvider; import org.onap.policy.api.main.startstop.Main; import org.onap.policy.common.endpoints.http.server.RestServer; import org.onap.policy.common.endpoints.parameters.RestServerParameters; import org.onap.policy.common.endpoints.report.HealthCheckReport; import org.onap.policy.common.gson.GsonMessageBodyHandler; +import org.onap.policy.common.parameters.ParameterService; import org.onap.policy.common.utils.coder.StandardCoder; import org.onap.policy.common.utils.network.NetworkUtil; import org.onap.policy.common.utils.resources.ResourceUtils; +import org.onap.policy.models.base.PfModelException; import org.onap.policy.models.errors.concepts.ErrorResponse; +import org.onap.policy.models.provider.PolicyModelsProviderParameters; import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyInput; import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy; @@ -174,13 +181,44 @@ public class TestApiRestServer { "policies/vDNS.policy.operational.input.json", "policies/vFirewall.policy.operational.input.json" }; + private static PolicyModelsProviderParameters providerParams; + private static ApiParameterGroup apiParamGroup; + private static PolicyProvider policyProvider; + private static PolicyTypeProvider policyTypeProvider; + // @formatter:on + /** + * Initializes parameters. + * + * @throws PfModelException the PfModel parsing exception + */ + private void setupParameters() throws PfModelException { + + standardCoder = new StandardCoder(); + providerParams = new PolicyModelsProviderParameters(); + providerParams.setDatabaseDriver("org.h2.Driver"); + providerParams.setDatabaseUrl("jdbc:h2:mem:testdb"); + providerParams.setDatabaseUser("policy"); + providerParams.setDatabasePassword(Base64.getEncoder().encodeToString("P01icY".getBytes())); + providerParams.setPersistenceUnit("ToscaConceptTest"); + apiParamGroup = new ApiParameterGroup("ApiGroup", null, providerParams); + ParameterService.register(apiParamGroup, true); + policyTypeProvider = new PolicyTypeProvider(); + policyProvider = new PolicyProvider(); + } + /** * Method for cleanup after each test. */ @After public void teardown() throws Exception { + if (policyTypeProvider != null) { + policyTypeProvider.close(); + } + if (policyProvider != null) { + policyProvider.close(); + } if (main != null) { stopApiService(main); } else if (restServer != null) { @@ -367,6 +405,48 @@ public class TestApiRestServer { }).doesNotThrowAnyException(); } + @Test + public void testReadPolicyTypesPersistent() throws Exception { + + setupParameters(); + main = startApiService(true); + for (String resrcName : toscaPolicyTypeResourceNames) { + Response rawResponse = createResource(POLICYTYPES, resrcName, true); + assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus()); + ToscaServiceTemplate response = rawResponse.readEntity(ToscaServiceTemplate.class); + assertNotNull(response); + assertFalse(response.getPolicyTypes().get(0).isEmpty()); + } + + for (String resrcName : toscaPolicyResourceNames) { + Response rawResponse = createResource(POLICYTYPES_TCA_POLICIES, resrcName, true); + assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus()); + } + + Response rawResponse = readResource(POLICYTYPES_TCA, true); + assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus()); + + rawResponse = readResource(POLICYTYPES_TCA_VERSION, true); + assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus()); + + + rawResponse = readResource(POLICYTYPES_TCA_LATEST, true); + assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus()); + + + rawResponse = readResource(POLICYTYPES_COLLECTOR, true); + assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus()); + + + rawResponse = readResource(POLICYTYPES_COLLECTOR_VERSION, true); + assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus()); + + + rawResponse = readResource(POLICYTYPES_COLLECTOR_LATEST, true); + assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus()); + + } + @Test public void testDeletePolicyType() { @@ -387,6 +467,24 @@ public class TestApiRestServer { }).doesNotThrowAnyException(); } + @Test + public void testDeletePolicyTypePersistent() throws Exception { + + setupParameters(); //setup DB + + main = startApiService(true);// create policy types + for (String resrcName : toscaPolicyTypeResourceNames) { + Response rawResponse = createResource(POLICYTYPES, resrcName, true); + assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus()); + ToscaServiceTemplate response = rawResponse.readEntity(ToscaServiceTemplate.class); + assertNotNull(response); + assertFalse(response.getPolicyTypes().get(0).isEmpty()); + } + Response rawResponse = deleteResource(POLICYTYPES_TCA_VERSION, true); + assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus()); + + } + @Test public void testCreatePolicies() { @@ -437,6 +535,39 @@ public class TestApiRestServer { }).doesNotThrowAnyException(); } + @Test + public void testReadPoliciesPersistent() throws Exception { + + setupParameters(); + main = startApiService(true); + for (String resrcName : toscaPolicyTypeResourceNames) { + Response rawResponse = createResource(POLICYTYPES, resrcName, true); + assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus()); + ToscaServiceTemplate response = rawResponse.readEntity(ToscaServiceTemplate.class); + assertNotNull(response); + assertFalse(response.getPolicyTypes().get(0).isEmpty()); + } + + for (String resrcName : toscaPolicyResourceNames) { + Response rawResponse = createResource(POLICYTYPES_TCA_POLICIES, resrcName, true); + assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus()); + } + + Response rawResponse; + rawResponse = readResource(POLICYTYPES_TCA, true); + assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus()); + + rawResponse = readResource(POLICYTYPES_TCA_POLICIES_VCPE, true); + assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus()); + + rawResponse = readResource(POLICYTYPES_TCA_POLICIES_VCPE_VERSION, true); + assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus()); + + rawResponse = readResource(POLICYTYPES_TCA_POLICIES_VCPE_LATEST, true); + assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus()); + + } + @Test public void testDeletePolicies() { @@ -451,6 +582,49 @@ public class TestApiRestServer { }).doesNotThrowAnyException(); } + @Test + public void testDeletePolicyVersion() throws Exception { + + setupParameters(); //setup DB + + main = startApiService(true);// create policy types + for (String resrcName : toscaPolicyTypeResourceNames) { + Response rawResponse = createResource(POLICYTYPES, resrcName, true); + assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus()); + ToscaServiceTemplate response = rawResponse.readEntity(ToscaServiceTemplate.class); + assertNotNull(response); + assertFalse(response.getPolicyTypes().get(0).isEmpty()); + } + for (String resrcName : toscaPolicyResourceNames) { + Response rawResponse = createResource(POLICYTYPES_TCA_POLICIES, resrcName, true); + assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus()); + } + Response rawResponse = deleteResource(POLICYTYPES_TCA_POLICIES_VCPE_VERSION, true); + assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus()); + } + + @Test + public void testGetAllVersionOfPolicy() throws Exception { + + setupParameters(); //setup DB + + main = startApiService(true);// create policy types + for (String resrcName : toscaPolicyTypeResourceNames) { + Response rawResponse = createResource(POLICYTYPES, resrcName, true); + assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus()); + ToscaServiceTemplate response = rawResponse.readEntity(ToscaServiceTemplate.class); + assertNotNull(response); + assertFalse(response.getPolicyTypes().get(0).isEmpty()); + } + for (String resrcName : toscaPolicyResourceNames) { + Response rawResponse = createResource(POLICYTYPES_TCA_POLICIES, resrcName, true); + assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus()); + } + Response rawResponse = readResource(POLICYTYPES_TCA_POLICIES, true); + assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus()); + + } + @Test public void testCreateGuardPolicies() { @@ -463,6 +637,19 @@ public class TestApiRestServer { }).doesNotThrowAnyException(); } + @Test + public void testCreateGuardPolicies1() throws Exception { + + setupParameters(); //setup DB + + main = startApiService(true); + for (String resrcName : legacyGuardPolicyResourceNames) { + Response rawResponse = createGuardPolicy(GUARD_POLICIES, resrcName, true); + assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus()); + } + + } + @Test public void testCreateOperationalPolicies() { @@ -475,6 +662,19 @@ public class TestApiRestServer { }).doesNotThrowAnyException(); } + @Test + public void testCreateOperationalPolicies1() throws Exception { + + setupParameters(); //setup DB + + main = startApiService(true); + for (String resrcName : legacyOperationalPolicyResourceNames) { + Response rawResponse = createOperationalPolicy(OPS_POLICIES, resrcName, true); + assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus()); + } + + } + @Test public void testReadGuardPolicies() { @@ -506,6 +706,33 @@ public class TestApiRestServer { }).doesNotThrowAnyException(); } + @Test + public void testReadGuardPolicies1() throws Exception { + + setupParameters(); //setup DB + + main = startApiService(true); + for (String resrcName : legacyGuardPolicyResourceNames) { + Response rawResponse = createGuardPolicy(GUARD_POLICIES, resrcName, true); + assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus()); + } + + Response rawResponse = readResource(GUARD_POLICIES_VDNS_FL_LATEST, true); + assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus()); + + + rawResponse = readResource(GUARD_POLICIES_VDNS_FL_VERSION, true); + assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus()); + + rawResponse = readResource(GUARD_POLICIES_VDNS_MINMAX_LATEST, true); + assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus()); + + + rawResponse = readResource(GUARD_POLICIES_VDNS_MINMAX_VERSION, true); + assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus()); + + } + @Test public void testReadOperationalPolicies() { @@ -549,6 +776,43 @@ public class TestApiRestServer { }).doesNotThrowAnyException(); } + @Test + public void testReadOperationalPolicies1() throws Exception { + + setupParameters(); //setup DB + + main = startApiService(true); + for (String resrcName : legacyOperationalPolicyResourceNames) { + Response rawResponse = createOperationalPolicy(OPS_POLICIES, resrcName, true); + assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus()); + } + + Response rawResponse = readResource(OPS_POLICIES_VCPE_LATEST, true); + assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus()); + + + rawResponse = readResource(OPS_POLICIES_VCPE_VERSION, true); + assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus()); + + + rawResponse = readResource(OPS_POLICIES_VDNS_LATEST, true); + assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus()); + + + rawResponse = readResource(OPS_POLICIES_VDNS_VERSION, true); + assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus()); + + + rawResponse = readResource(OPS_POLICIES_VFIREWALL_LATEST, true); + assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus()); + + + rawResponse = readResource(OPS_POLICIES_VFIREWALL_VERSION, true); + assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus()); + + + } + @Test public void testDeleteGuardPolicy() { @@ -562,6 +826,22 @@ public class TestApiRestServer { }).doesNotThrowAnyException(); } + @Test + public void testDeleteGuardPolicy1() throws Exception { + + setupParameters(); //setup DB + main = startApiService(true); + + for (String resrcName : legacyGuardPolicyResourceNames) { + Response rawResponse = createGuardPolicy(GUARD_POLICIES, resrcName, true); + assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus()); + } + + Response rawResponse = deleteResource(GUARD_POLICIES_VDNS_FL_VERSION, true); + assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus()); + + } + @Test public void testGetDeployedVersionsOfGuardPolicy() { assertThatCode(() -> { @@ -584,6 +864,21 @@ public class TestApiRestServer { }).doesNotThrowAnyException(); } + @Test + public void testDeleteOperationalPolicy1() throws Exception { + + setupParameters(); //setup DB + + main = startApiService(true); + for (String resrcName : legacyOperationalPolicyResourceNames) { + Response rawResponse = createOperationalPolicy(OPS_POLICIES, resrcName, true); + assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus()); + } + Response rawResponse = deleteResource(OPS_POLICIES_VCPE_VERSION, true); + assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus()); + + } + @Test public void testHttpsCreatePolicyTypes() { @@ -877,4 +1172,4 @@ public class TestApiRestServer { assertEquals(code, report.getCode()); assertEquals(message, report.getMessage()); } -} \ No newline at end of file +} -- cgit 1.2.3-korg