diff options
author | Chenfei Gao <cgao@research.att.com> | 2019-04-11 18:47:26 -0400 |
---|---|---|
committer | Chenfei Gao <cgao@research.att.com> | 2019-04-11 20:11:06 -0400 |
commit | e696cd9f5bafd0830017ad38355c2e8fe64f980b (patch) | |
tree | 9dababf31b5c9972d807704d9ab8cb5811994b65 /main/src/test/java/org | |
parent | a229e2bccfdcf40c56c4d87d9e3cc5cd9dabb2a8 (diff) |
Add junit tests for endpoints
Add junit tests to improve code coverage to > 80%.
Modify db setting and remove db authorization from persistence.
Issue-ID: POLICY-1441
Change-Id: I6070ac9ba6adba836adeeb547610c27dccf577bd
Signed-off-by: Chenfei Gao <cgao@research.att.com>
Diffstat (limited to 'main/src/test/java/org')
6 files changed, 518 insertions, 0 deletions
diff --git a/main/src/test/java/org/onap/policy/api/main/parameters/CommonTestData.java b/main/src/test/java/org/onap/policy/api/main/parameters/CommonTestData.java index f0f971d4..50e484eb 100644 --- a/main/src/test/java/org/onap/policy/api/main/parameters/CommonTestData.java +++ b/main/src/test/java/org/onap/policy/api/main/parameters/CommonTestData.java @@ -42,6 +42,7 @@ public class CommonTestData { private static final String PROVIDER_GROUP_NAME = "PolicyProviderParameterGroup"; private static final String PROVIDER_IMPL = "org.onap.policy.models.provider.impl.DatabasePolicyModelsProviderImpl"; + private static final String DATABASE_DRIVER = "org.h2.Driver"; private static final String DATABASE_URL = "jdbc:h2:mem:testdb"; private static final String DATABASE_USER = "policy"; private static final String DATABASE_PASSWORD = "P01icY"; @@ -76,6 +77,7 @@ public class CommonTestData { databaseProviderParameters = new PolicyModelsProviderParameters(); databaseProviderParameters.setName(PROVIDER_GROUP_NAME); databaseProviderParameters.setImplementation(PROVIDER_IMPL); + databaseProviderParameters.setDatabaseDriver(DATABASE_DRIVER); databaseProviderParameters.setDatabaseUrl(DATABASE_URL); databaseProviderParameters.setDatabaseUser(DATABASE_USER); databaseProviderParameters.setDatabasePassword(DATABASE_PASSWORD); 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 4fb588c2..756c7ae0 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 @@ -21,7 +21,10 @@ package org.onap.policy.api.main.rest; +import static org.assertj.core.api.Assertions.assertThatCode; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; @@ -37,11 +40,14 @@ import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; import javax.ws.rs.client.Client; import javax.ws.rs.client.ClientBuilder; +import javax.ws.rs.client.Entity; 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.ClientConfig; +import org.glassfish.jersey.client.ClientProperties; import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature; import org.junit.After; import org.junit.Test; @@ -50,7 +56,14 @@ import org.onap.policy.api.main.parameters.CommonTestData; import org.onap.policy.api.main.parameters.RestServerParameters; import org.onap.policy.api.main.startstop.Main; import org.onap.policy.common.endpoints.report.HealthCheckReport; +import org.onap.policy.common.gson.GsonMessageBodyHandler; +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.errors.concepts.ErrorResponse; +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; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -66,17 +79,93 @@ public class TestApiRestServer { private static final String ALIVE = "alive"; private static final String SELF = "self"; private static final String NAME = "Policy API"; + private static final String HEALTHCHECK_ENDPOINT = "healthcheck"; private static final String STATISTICS_ENDPOINT = "statistics"; + + private static final String POLICYTYPES = "policytypes"; + private static final String POLICYTYPES_TCA = "policytypes/onap.policies.monitoring.cdap.tca.hi.lo.app"; + private static final String POLICYTYPES_COLLECTOR = + "policytypes/onap.policies.monitoring.dcaegen2.collectors.datafile.datafile-app-server"; + private static final String POLICYTYPES_TCA_VERSION = + "policytypes/onap.policies.monitoring.cdap.tca.hi.lo.app/versions/1.0.0"; + private static final String POLICYTYPES_TCA_LATEST = + "policytypes/onap.policies.monitoring.cdap.tca.hi.lo.app/versions/latest"; + private static final String POLICYTYPES_COLLECTOR_VERSION = + "policytypes/onap.policies.monitoring.dcaegen2.collectors.datafile.datafile-app-server/versions/1.0.0"; + private static final String POLICYTYPES_COLLECTOR_LATEST = + "policytypes/onap.policies.monitoring.dcaegen2.collectors.datafile.datafile-app-server/versions/latest"; + + private static final String POLICYTYPES_TCA_POLICIES = + "policytypes/onap.policies.monitoring.cdap.tca.hi.lo.app/versions/1.0.0/policies"; + private static final String POLICYTYPES_TCA_POLICIES_VCPE = + "policytypes/onap.policies.monitoring.cdap.tca.hi.lo.app/versions/1.0.0/policies/onap.restart.tca"; + private static final String POLICYTYPES_TCA_POLICIES_VCPE_VERSION = "policytypes/" + + "onap.policies.monitoring.cdap.tca.hi.lo.app/versions/1.0.0/policies/onap.restart.tca/versions/1.0.0"; + private static final String POLICYTYPES_TCA_POLICIES_VCPE_LATEST = "policytypes/" + + "onap.policies.monitoring.cdap.tca.hi.lo.app/versions/1.0.0/policies/onap.restart.tca/versions/latest"; + + private static final String GUARD_POLICIES = + "policytypes/onap.policy.controlloop.guard/versions/1.0.0/policies"; + private static final String GUARD_POLICIES_VDNS_FL = + "policytypes/onap.policy.controlloop.guard/versions/1.0.0/policies/guard.frequency.scaleout"; + private static final String GUARD_POLICIES_VDNS_MINMAX = + "policytypes/onap.policy.controlloop.guard/versions/1.0.0/policies/guard.minmax.scaleout"; + private static final String GUARD_POLICIES_VDNS_FL_VERSION = "policytypes/" + + "onap.policy.controlloop.guard/versions/1.0.0/policies/guard.frequency.scaleout/versions/1.0.0"; + private static final String GUARD_POLICIES_VDNS_MINMAX_VERSION = "policytypes/" + + "onap.policy.controlloop.guard/versions/1.0.0/policies/guard.minmax.scaleout/versions/1.0.0"; + + private static final String OPS_POLICIES = + "policytypes/onap.policy.controlloop.operational/versions/1.0.0/policies"; + private static final String OPS_POLICIES_VCPE = + "policytypes/onap.policy.controlloop.guard/versions/1.0.0/policies/operational.restart"; + private static final String OPS_POLICIES_VDNS = + "policytypes/onap.policy.controlloop.guard/versions/1.0.0/policies/operational.scaleout"; + private static final String OPS_POLICIES_VFIREWALL = + "policytypes/onap.policy.controlloop.guard/versions/1.0.0/policies/operational.modifyconfig"; + private static final String OPS_POLICIES_VCPE_VERSION = "policytypes/" + + "onap.policy.controlloop.guard/versions/1.0.0/policies/operational.restart/versions/1.0.0"; + private static final String OPS_POLICIES_VDNS_VERSION = "policytypes/" + + "onap.policy.controlloop.guard/versions/1.0.0/policies/operational.scaleout/versions/1.0.0"; + private static final String OPS_POLICIES_VFIREWALL_VERSION = "policytypes/" + + "onap.policy.controlloop.guard/versions/1.0.0/policies/operational.modifyconfig/versions/1.0.0"; + private static String KEYSTORE = System.getProperty("user.dir") + "/src/test/resources/ssl/policy-keystore"; private Main main; private ApiRestServer restServer; + private StandardCoder standardCoder = new StandardCoder(); + + // @formatter:off + private String[] toscaPolicyResourceNames = { + "policies/vCPE.policy.monitoring.input.tosca.json", + "policies/vDNS.policy.monitoring.input.tosca.json", + "policies/vFirewall.policy.monitoring.input.tosca.json", + }; + + private String[] toscaPolicyTypeResourceNames = { + "policytypes/onap.policy.monitoring.cdap.tca.hi.lo.app.json", + "policytypes/onap.policies.monitoring.dcaegen2.collectors.datafile.datafile-app-server.json" + }; + + private String[] legacyGuardPolicyResourceNames = { + "policies/vDNS.policy.guard.frequency.input.json", + "policies/vDNS.policy.guard.minmax.input.json", + }; + + private String[] legacyOperationalPolicyResourceNames = { + "policies/vCPE.policy.operational.input.json", + "policies/vDNS.policy.operational.input.json", + "policies/vFirewall.policy.operational.input.json" + }; + // @formatter:on /** * Method for cleanup after each test. */ @After public void teardown() { + try { if (NetworkUtil.isTcpPortOpen("localhost", 6969, 1, 1000L)) { if (main != null) { @@ -92,6 +181,7 @@ public class TestApiRestServer { @Test public void testHealthCheckSuccess() { + try { main = startApiService(true); final Invocation.Builder invocationBuilder = sendHttpRequest(HEALTHCHECK_ENDPOINT); @@ -105,6 +195,7 @@ public class TestApiRestServer { @Test public void testHealthCheckFailure() throws InterruptedException, IOException { + final RestServerParameters restServerParams = new CommonTestData().getRestServerParameters(false); restServerParams.setName(CommonTestData.API_GROUP_NAME); restServer = new ApiRestServer(restServerParams); @@ -123,6 +214,7 @@ public class TestApiRestServer { @Test public void testHttpsHealthCheckSuccess() { + try { main = startApiService(false); final Invocation.Builder invocationBuilder = sendHttpsRequest(HEALTHCHECK_ENDPOINT); @@ -136,6 +228,7 @@ public class TestApiRestServer { @Test public void testApiStatistics_200() { + try { main = startApiService(true); Invocation.Builder invocationBuilder = sendHttpRequest(STATISTICS_ENDPOINT); @@ -154,6 +247,7 @@ public class TestApiRestServer { @Test public void testApiStatistics_500() { + final RestServerParameters restServerParams = new CommonTestData().getRestServerParameters(false); restServerParams.setName(CommonTestData.API_GROUP_NAME); restServer = new ApiRestServer(restServerParams); @@ -171,6 +265,7 @@ public class TestApiRestServer { @Test public void testHttpsApiStatistics() { + try { main = startApiService(false); final Invocation.Builder invocationBuilder = sendHttpsRequest(STATISTICS_ENDPOINT); @@ -184,6 +279,7 @@ public class TestApiRestServer { @Test public void testApiStatisticsConstructorIsPrivate() { + try { final Constructor<ApiStatisticsManager> constructor = ApiStatisticsManager.class.getDeclaredConstructor(); assertTrue(Modifier.isPrivate(constructor.getModifiers())); @@ -194,7 +290,411 @@ public class TestApiRestServer { } } + @Test + public void testCreatePolicyTypes() { + + assertThatCode(() -> { + 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()); + } + }).doesNotThrowAnyException(); + } + + @Test + public void testReadPolicyTypes() { + + assertThatCode(() -> { + main = startApiService(true); + Response rawResponse = readResource(POLICYTYPES, true); + assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus()); + ToscaServiceTemplate response = rawResponse.readEntity(ToscaServiceTemplate.class); + assertTrue(response.getPolicyTypes().get(0).isEmpty()); + + rawResponse = readResource(POLICYTYPES_TCA, true); + assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawResponse.getStatus()); + ErrorResponse error = rawResponse.readEntity(ErrorResponse.class); + assertEquals("policy type with ID onap.policies.monitoring.cdap.tca.hi.lo.app:null does not exist", + error.getErrorMessage()); + + rawResponse = readResource(POLICYTYPES_TCA_VERSION, true); + assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawResponse.getStatus()); + error = rawResponse.readEntity(ErrorResponse.class); + assertEquals("policy type with ID onap.policies.monitoring.cdap.tca.hi.lo.app:1.0.0 does not exist", + error.getErrorMessage()); + + rawResponse = readResource(POLICYTYPES_TCA_LATEST, true); + assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawResponse.getStatus()); + error = rawResponse.readEntity(ErrorResponse.class); + assertEquals("policy type with ID onap.policies.monitoring.cdap.tca.hi.lo.app:null does not exist", + error.getErrorMessage()); + + rawResponse = readResource(POLICYTYPES_COLLECTOR, true); + assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawResponse.getStatus()); + error = rawResponse.readEntity(ErrorResponse.class); + assertEquals("policy type with ID " + + "onap.policies.monitoring.dcaegen2.collectors.datafile.datafile-app-server:null does not exist", + error.getErrorMessage()); + + rawResponse = readResource(POLICYTYPES_COLLECTOR_VERSION, true); + assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawResponse.getStatus()); + error = rawResponse.readEntity(ErrorResponse.class); + assertEquals("policy type with ID " + + "onap.policies.monitoring.dcaegen2.collectors.datafile.datafile-app-server:1.0.0 does not exist", + error.getErrorMessage()); + + rawResponse = readResource(POLICYTYPES_COLLECTOR_LATEST, true); + assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawResponse.getStatus()); + error = rawResponse.readEntity(ErrorResponse.class); + assertEquals("policy type with ID " + + "onap.policies.monitoring.dcaegen2.collectors.datafile.datafile-app-server:null does not exist", + error.getErrorMessage()); + }).doesNotThrowAnyException(); + } + + @Test + public void testDeletePolicyType() { + + assertThatCode(() -> { + main = startApiService(true); + Response rawResponse = deleteResource(POLICYTYPES_TCA_VERSION, true); + assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawResponse.getStatus()); + ErrorResponse error = rawResponse.readEntity(ErrorResponse.class); + assertEquals("policy type with ID onap.policies.monitoring.cdap.tca.hi.lo.app:1.0.0 does not exist", + error.getErrorMessage()); + + rawResponse = deleteResource(POLICYTYPES_COLLECTOR_VERSION, true); + assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawResponse.getStatus()); + error = rawResponse.readEntity(ErrorResponse.class); + assertEquals("policy type with ID " + + "onap.policies.monitoring.dcaegen2.collectors.datafile.datafile-app-server:1.0.0 does not exist", + error.getErrorMessage()); + }).doesNotThrowAnyException(); + } + + @Test + public void testCreatePolicies() { + + assertThatCode(() -> { + main = startApiService(true); + for (String resrcName : toscaPolicyResourceNames) { + Response rawResponse = createResource(POLICYTYPES_TCA_POLICIES, resrcName, true); + assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawResponse.getStatus()); + ErrorResponse error = rawResponse.readEntity(ErrorResponse.class); + assertEquals("policy type with ID onap.policies.monitoring.cdap.tca.hi.lo.app:1.0.0 does not exist", + error.getErrorMessage()); + } + }).doesNotThrowAnyException(); + } + + @Test + public void testReadPolicies() { + + assertThatCode(() -> { + main = startApiService(true); + Response rawResponse = readResource(POLICYTYPES_TCA_POLICIES, true); + assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawResponse.getStatus()); + ErrorResponse error = rawResponse.readEntity(ErrorResponse.class); + assertEquals("policy with ID null:null and " + + "type onap.policies.monitoring.cdap.tca.hi.lo.app:1.0.0 does not exist", + error.getErrorMessage()); + + rawResponse = readResource(POLICYTYPES_TCA_POLICIES_VCPE, true); + assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawResponse.getStatus()); + error = rawResponse.readEntity(ErrorResponse.class); + assertEquals("policy with ID onap.restart.tca:null and " + + "type onap.policies.monitoring.cdap.tca.hi.lo.app:1.0.0 does not exist", + error.getErrorMessage()); + + rawResponse = readResource(POLICYTYPES_TCA_POLICIES_VCPE_VERSION, true); + assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawResponse.getStatus()); + error = rawResponse.readEntity(ErrorResponse.class); + assertEquals("policy with ID onap.restart.tca:1.0.0 and " + + "type onap.policies.monitoring.cdap.tca.hi.lo.app:1.0.0 does not exist", + error.getErrorMessage()); + + rawResponse = readResource(POLICYTYPES_TCA_POLICIES_VCPE_LATEST, true); + assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawResponse.getStatus()); + error = rawResponse.readEntity(ErrorResponse.class); + assertEquals("policy with ID onap.restart.tca:null and " + + "type onap.policies.monitoring.cdap.tca.hi.lo.app:1.0.0 does not exist", + error.getErrorMessage()); + }).doesNotThrowAnyException(); + } + + @Test + public void testDeletePolicies() { + + assertThatCode(() -> { + main = startApiService(true); + Response rawResponse = deleteResource(POLICYTYPES_TCA_POLICIES_VCPE_VERSION, true); + assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawResponse.getStatus()); + ErrorResponse error = rawResponse.readEntity(ErrorResponse.class); + assertEquals("policy with ID onap.restart.tca:1.0.0 and " + + "type onap.policies.monitoring.cdap.tca.hi.lo.app:1.0.0 does not exist", + error.getErrorMessage()); + }).doesNotThrowAnyException(); + } + + @Test + public void testCreateGuardPolicies() { + + assertThatCode(() -> { + main = startApiService(true); + for (String resrcName : legacyGuardPolicyResourceNames) { + Response rawResponse = createGuardPolicy(GUARD_POLICIES, resrcName, true); + assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus()); + } + }).doesNotThrowAnyException(); + } + + @Test + public void testCreateOperationalPolicies() { + + assertThatCode(() -> { + main = startApiService(true); + for (String resrcName : legacyOperationalPolicyResourceNames) { + Response rawResponse = createOperationalPolicy(OPS_POLICIES, resrcName, true); + assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus()); + } + }).doesNotThrowAnyException(); + } + + @Test + public void testReadGuardPolicies() { + + assertThatCode(() -> { + main = startApiService(true); + Response rawResponse = readResource(GUARD_POLICIES_VDNS_FL, true); + assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), rawResponse.getStatus()); + ErrorResponse error = rawResponse.readEntity(ErrorResponse.class); + assertEquals("no policy found for policy ID: guard.frequency.scaleout", + error.getErrorMessage()); + + rawResponse = readResource(GUARD_POLICIES_VDNS_FL_VERSION, true); + assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), rawResponse.getStatus()); + error = rawResponse.readEntity(ErrorResponse.class); + assertEquals("no policy found for policy ID: guard.frequency.scaleout", + error.getErrorMessage()); + + rawResponse = readResource(GUARD_POLICIES_VDNS_MINMAX, true); + assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), rawResponse.getStatus()); + error = rawResponse.readEntity(ErrorResponse.class); + assertEquals("no policy found for policy ID: guard.minmax.scaleout", + error.getErrorMessage()); + + rawResponse = readResource(GUARD_POLICIES_VDNS_MINMAX_VERSION, true); + assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), rawResponse.getStatus()); + error = rawResponse.readEntity(ErrorResponse.class); + assertEquals("no policy found for policy ID: guard.minmax.scaleout", + error.getErrorMessage()); + }).doesNotThrowAnyException(); + } + + @Test + public void testReadOperationalPolicies() { + + assertThatCode(() -> { + main = startApiService(true); + Response rawResponse = readResource(OPS_POLICIES_VCPE, true); + assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), rawResponse.getStatus()); + ErrorResponse error = rawResponse.readEntity(ErrorResponse.class); + assertEquals("no policy found for policy ID: operational.restart", + error.getErrorMessage()); + + rawResponse = readResource(OPS_POLICIES_VCPE_VERSION, true); + assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), rawResponse.getStatus()); + error = rawResponse.readEntity(ErrorResponse.class); + assertEquals("no policy found for policy ID: operational.restart", + error.getErrorMessage()); + + rawResponse = readResource(OPS_POLICIES_VDNS, true); + assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), rawResponse.getStatus()); + error = rawResponse.readEntity(ErrorResponse.class); + assertEquals("no policy found for policy ID: operational.scaleout", + error.getErrorMessage()); + + rawResponse = readResource(OPS_POLICIES_VDNS_VERSION, true); + assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), rawResponse.getStatus()); + error = rawResponse.readEntity(ErrorResponse.class); + assertEquals("no policy found for policy ID: operational.scaleout", + error.getErrorMessage()); + + rawResponse = readResource(OPS_POLICIES_VFIREWALL, true); + assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), rawResponse.getStatus()); + error = rawResponse.readEntity(ErrorResponse.class); + assertEquals("no policy found for policy ID: operational.modifyconfig", + error.getErrorMessage()); + + rawResponse = readResource(OPS_POLICIES_VFIREWALL_VERSION, true); + assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), rawResponse.getStatus()); + error = rawResponse.readEntity(ErrorResponse.class); + assertEquals("no policy found for policy ID: operational.modifyconfig", + error.getErrorMessage()); + }).doesNotThrowAnyException(); + } + + @Test + public void testDeleteGuardPolicy() { + + assertThatCode(() -> { + main = startApiService(true); + Response rawResponse = deleteResource(GUARD_POLICIES_VDNS_FL_VERSION, true); + assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), rawResponse.getStatus()); + ErrorResponse error = rawResponse.readEntity(ErrorResponse.class); + assertEquals("no policy found for policy ID: guard.frequency.scaleout", + error.getErrorMessage()); + }).doesNotThrowAnyException(); + } + + @Test + public void testDeleteOperationalPolicy() { + + assertThatCode(() -> { + main = startApiService(true); + Response rawResponse = deleteResource(OPS_POLICIES_VCPE_VERSION, true); + assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), rawResponse.getStatus()); + ErrorResponse error = rawResponse.readEntity(ErrorResponse.class); + assertEquals("no policy found for policy ID: operational.restart", + error.getErrorMessage()); + }).doesNotThrowAnyException(); + } + + @Test + public void testHttpsCreatePolicyTypes() { + + assertThatCode(() -> { + main = startApiService(false); + for (String resrcName : toscaPolicyTypeResourceNames) { + Response rawResponse = createResource(POLICYTYPES, resrcName, false); + assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus()); + ToscaServiceTemplate response = rawResponse.readEntity(ToscaServiceTemplate.class); + assertNotNull(response); + assertFalse(response.getPolicyTypes().get(0).isEmpty()); + } + }).doesNotThrowAnyException(); + } + + @Test + public void testHttpsCreatePolicies() { + + assertThatCode(() -> { + main = startApiService(false); + for (String resrcName : toscaPolicyResourceNames) { + Response rawResponse = createResource(POLICYTYPES_TCA_POLICIES, resrcName, false); + assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawResponse.getStatus()); + ErrorResponse error = rawResponse.readEntity(ErrorResponse.class); + assertEquals("policy type with ID onap.policies.monitoring.cdap.tca.hi.lo.app:1.0.0 does not exist", + error.getErrorMessage()); + } + }).doesNotThrowAnyException(); + } + + @Test + public void testHttpsCreateGuardPolicies() { + + assertThatCode(() -> { + main = startApiService(false); + for (String resrcName : legacyGuardPolicyResourceNames) { + Response rawResponse = createGuardPolicy(GUARD_POLICIES, resrcName, false); + assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus()); + } + }).doesNotThrowAnyException(); + } + + @Test + public void testHttpsCreateOperationalPolicies() { + + assertThatCode(() -> { + main = startApiService(false); + for (String resrcName : legacyOperationalPolicyResourceNames) { + Response rawResponse = createOperationalPolicy(OPS_POLICIES, resrcName, false); + assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus()); + } + }).doesNotThrowAnyException(); + } + + private Response createResource(String endpoint, String resourceName, boolean http) throws Exception { + + ToscaServiceTemplate rawServiceTemplate = standardCoder.decode( + ResourceUtils.getResourceAsString(resourceName), ToscaServiceTemplate.class); + final Invocation.Builder invocationBuilder; + + if (http) { + invocationBuilder = sendHttpRequest(endpoint); + } else { + invocationBuilder = sendHttpsRequest(endpoint); + } + + Entity<ToscaServiceTemplate> entity = Entity.entity(rawServiceTemplate, MediaType.APPLICATION_JSON); + return invocationBuilder.post(entity); + } + + private Response createGuardPolicy(String endpoint, String resourceName, boolean http) throws Exception { + + LegacyGuardPolicyInput rawGuardPolicy = standardCoder.decode( + ResourceUtils.getResourceAsString(resourceName), LegacyGuardPolicyInput.class); + final Invocation.Builder invocationBuilder; + + if (http) { + invocationBuilder = sendHttpRequest(endpoint); + } else { + invocationBuilder = sendHttpsRequest(endpoint); + } + + Entity<LegacyGuardPolicyInput> entity = Entity.entity(rawGuardPolicy, MediaType.APPLICATION_JSON); + return invocationBuilder.post(entity); + } + + private Response createOperationalPolicy(String endpoint, String resourceName, boolean http) throws Exception { + + LegacyOperationalPolicy rawOpsPolicy = standardCoder.decode( + ResourceUtils.getResourceAsString(resourceName), LegacyOperationalPolicy.class); + final Invocation.Builder invocationBuilder; + + if (http) { + invocationBuilder = sendHttpRequest(endpoint); + } else { + invocationBuilder = sendHttpsRequest(endpoint); + } + + Entity<LegacyOperationalPolicy> entity = Entity.entity(rawOpsPolicy, MediaType.APPLICATION_JSON); + return invocationBuilder.post(entity); + } + + private Response readResource(String endpoint, boolean http) throws Exception { + + final Invocation.Builder invocationBuilder; + + if (http) { + invocationBuilder = sendHttpRequest(endpoint); + } else { + invocationBuilder = sendHttpsRequest(endpoint); + } + + return invocationBuilder.get(); + + } + + private Response deleteResource(String endpoint, boolean http) throws Exception { + + final Invocation.Builder invocationBuilder; + + if (http) { + invocationBuilder = sendHttpRequest(endpoint); + } else { + invocationBuilder = sendHttpsRequest(endpoint); + } + + return invocationBuilder.delete(); + } + private Main startApiService(final boolean http) { + final String[] apiConfigParameters = new String[2]; if (http) { apiConfigParameters[0] = "-c"; @@ -211,16 +711,22 @@ public class TestApiRestServer { } private void stopApiService(final Main main) throws PolicyApiException { + main.shutdown(); } private Invocation.Builder sendHttpRequest(final String endpoint) throws Exception { + final ClientConfig clientConfig = new ClientConfig(); final HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("healthcheck", "zb!XztG34"); clientConfig.register(feature); final Client client = ClientBuilder.newClient(clientConfig); + + client.property(ClientProperties.METAINF_SERVICES_LOOKUP_DISABLE, "true"); + client.register(GsonMessageBodyHandler.class); + final WebTarget webTarget = client.target("http://localhost:6969/policy/api/v1/" + endpoint); final Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON); @@ -255,6 +761,9 @@ public class TestApiRestServer { final HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("healthcheck", "zb!XztG34"); client.register(feature); + client.property(ClientProperties.METAINF_SERVICES_LOOKUP_DISABLE, "true"); + client.register(GsonMessageBodyHandler.class); + final WebTarget webTarget = client.target("https://localhost:6969/policy/api/v1/" + endpoint); final Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON); @@ -266,6 +775,7 @@ public class TestApiRestServer { } private void updateApiStatistics() { + ApiStatisticsManager.updateTotalApiCallCount(); ApiStatisticsManager.updateApiCallSuccessCount(); ApiStatisticsManager.updateApiCallFailureCount(); @@ -284,11 +794,13 @@ public class TestApiRestServer { } 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) { + assertEquals(name, report.getName()); assertEquals(url, report.getUrl()); assertEquals(healthy, report.isHealthy()); diff --git a/main/src/test/java/org/onap/policy/api/main/rest/provider/TestLegacyGuardPolicyProvider.java b/main/src/test/java/org/onap/policy/api/main/rest/provider/TestLegacyGuardPolicyProvider.java index 085255f3..76641aae 100644 --- a/main/src/test/java/org/onap/policy/api/main/rest/provider/TestLegacyGuardPolicyProvider.java +++ b/main/src/test/java/org/onap/policy/api/main/rest/provider/TestLegacyGuardPolicyProvider.java @@ -67,6 +67,7 @@ public class TestLegacyGuardPolicyProvider { 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())); diff --git a/main/src/test/java/org/onap/policy/api/main/rest/provider/TestLegacyOperationalPolicyProvider.java b/main/src/test/java/org/onap/policy/api/main/rest/provider/TestLegacyOperationalPolicyProvider.java index e9adae7c..8aa19793 100644 --- a/main/src/test/java/org/onap/policy/api/main/rest/provider/TestLegacyOperationalPolicyProvider.java +++ b/main/src/test/java/org/onap/policy/api/main/rest/provider/TestLegacyOperationalPolicyProvider.java @@ -64,6 +64,7 @@ public class TestLegacyOperationalPolicyProvider { 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())); diff --git a/main/src/test/java/org/onap/policy/api/main/rest/provider/TestPolicyProvider.java b/main/src/test/java/org/onap/policy/api/main/rest/provider/TestPolicyProvider.java index 7720575a..653f467e 100644 --- a/main/src/test/java/org/onap/policy/api/main/rest/provider/TestPolicyProvider.java +++ b/main/src/test/java/org/onap/policy/api/main/rest/provider/TestPolicyProvider.java @@ -67,6 +67,7 @@ public class TestPolicyProvider { 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())); diff --git a/main/src/test/java/org/onap/policy/api/main/rest/provider/TestPolicyTypeProvider.java b/main/src/test/java/org/onap/policy/api/main/rest/provider/TestPolicyTypeProvider.java index 76825b20..9dc77267 100644 --- a/main/src/test/java/org/onap/policy/api/main/rest/provider/TestPolicyTypeProvider.java +++ b/main/src/test/java/org/onap/policy/api/main/rest/provider/TestPolicyTypeProvider.java @@ -64,6 +64,7 @@ public class TestPolicyTypeProvider { 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())); |