diff options
Diffstat (limited to 'main/src/test/java/org')
5 files changed, 244 insertions, 136 deletions
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 abc87383..124e8583 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 @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved. - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved. * Modifications Copyright (C) 2019 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -104,13 +104,16 @@ public class TestApiRestServer { "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/" + private static final String POLICYTYPES_TCA_POLICIES_VCPE_VERSION1 = "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_VERSION2 = "policytypes/" + + "onap.policies.monitoring.cdap.tca.hi.lo.app/versions/1.0.0/policies/onap.restart.tca/versions/2.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 POLICYTYPES_TCA_POLICIES_VCPE_DEPLOYED = "policytypes/" + "onap.policies.monitoring.cdap.tca.hi.lo.app/versions/1.0.0/policies/onap.restart.tca/versions/deployed"; + private static final String GUARD_POLICYTYPE = "onap.policies.controlloop.Guard"; private static final String GUARD_POLICIES = "policytypes/onap.policies.controlloop.Guard/versions/1.0.0/policies"; private static final String GUARD_POLICIES_VDNS_FL_LATEST = "policytypes/onap.policies.controlloop.Guard/versions/1.0.0/policies/guard.frequency.scaleout" @@ -126,6 +129,7 @@ public class TestApiRestServer { private static final String GUARD_POLICIES_VDNS_MINMAX_VERSION = "policytypes/" + "onap.policies.controlloop.Guard/versions/1.0.0/policies/guard.minmax.scaleout/versions/1"; + private static final String OPS_POLICYTYPE = "onap.policies.controlloop.Operational"; private static final String OPS_POLICIES = "policytypes/onap.policies.controlloop.Operational/versions/1.0.0/policies"; private static final String OPS_POLICIES_VCPE_LATEST = @@ -153,19 +157,19 @@ public class TestApiRestServer { // @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/vCPE.policy.monitoring.input.tosca.v2.yaml", "policies/vDNS.policy.monitoring.input.tosca.json", - "policies/vDNS.policy.monitoring.input.tosca.yaml", + "policies/vDNS.policy.monitoring.input.tosca.v2.yaml", "policies/vFirewall.policy.monitoring.input.tosca.json", - "policies/vFirewall.policy.monitoring.input.tosca.yaml" + "policies/vFirewall.policy.monitoring.input.tosca.v2.yaml" }; private static final String[] TOSCA_POLICIES_RESOURCE_NAMES = { "policies/vCPE.policies.optimization.input.tosca.json", - "policies/vCPE.policies.optimization.input.tosca.yaml" + "policies/vCPE.policies.optimization.input.tosca.v2.yaml" }; - private static final String[] TOSCA_PPOLICYTYPE_RESOURCE_NAMES = { + private static final String[] TOSCA_POLICYTYPE_RESOURCE_NAMES = { "policytypes/onap.policies.monitoring.cdap.tca.hi.lo.app.yaml", "policytypes/onap.policies.monitoring.dcaegen2.collectors.datafile.datafile-app-server.yaml", "policytypes/onap.policies.Optimization.yaml", @@ -190,12 +194,23 @@ public class TestApiRestServer { "policies/vDNS.policy.guard.minmax.input.json" }; + private static final String[] LEGACY_GUARD_POLICY_NAMES = { + "guard.frequency.scaleout", + "guard.minmax.scaleout" + }; + private static final String[] LEGACY_OPERATIONAL_POLICY_RESOURCE_NAMES = { "policies/vCPE.policy.operational.input.json", "policies/vDNS.policy.operational.input.json", "policies/vFirewall.policy.operational.input.json" }; + private static final String[] LEGACY_OPERATIONAL_POLICY_NAMES = { + "operational.restart", + "operational.scaleout", + "operational.modifyconfig" + }; + private static PolicyModelsProviderParameters providerParams; private static ApiParameterGroup apiParamGroup; private static PolicyProvider policyProvider; @@ -271,23 +286,17 @@ public class TestApiRestServer { */ @Before public void beforeClearDatabase() throws Exception { + Response rawResponse = readResource(POLICYTYPES, APP_JSON); ToscaServiceTemplate response = rawResponse.readEntity(ToscaServiceTemplate.class); for (ToscaEntityKey policyTypeKey : response.getPolicyTypesAsMap().keySet()) { - String getPoliciesPath = - "policytypes/" + policyTypeKey.getName() + "/versions/" + policyTypeKey.getVersion() + "/policies"; - - Response rawPolicyResponse = readResource(getPoliciesPath, APP_JSON); - if (Response.Status.OK.getStatusCode() == rawPolicyResponse.getStatus()) { - ToscaServiceTemplate policyResponse = rawPolicyResponse.readEntity(ToscaServiceTemplate.class); - - for (ToscaEntityKey policyKey : policyResponse.getToscaTopologyTemplate().getPoliciesAsMap().keySet()) { - String deletePolicyPath = - "policytypes/" + policyTypeKey.getName() + "/versions/" + policyTypeKey.getVersion() - + "/policies/" + policyKey.getName() + "/versions/" + policyKey.getVersion(); - deleteResource(deletePolicyPath, APP_JSON); - } + if (GUARD_POLICYTYPE.equals(policyTypeKey.getName()) + || OPS_POLICYTYPE.equals(policyTypeKey.getName())) { + deleteLegacyPolicies(LEGACY_GUARD_POLICY_NAMES, GUARD_POLICYTYPE); + deleteLegacyPolicies(LEGACY_OPERATIONAL_POLICY_NAMES, OPS_POLICYTYPE); + } else { + deleteToscaPolicies(policyTypeKey); } String deletePolicyTypePath = @@ -312,7 +321,7 @@ public class TestApiRestServer { @Test public void testCreatePolicyTypes() throws Exception { - for (String resrcName : TOSCA_PPOLICYTYPE_RESOURCE_NAMES) { + for (String resrcName : TOSCA_POLICYTYPE_RESOURCE_NAMES) { Response rawResponse = createResource(POLICYTYPES, resrcName); assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus()); ToscaServiceTemplate response = rawResponse.readEntity(ToscaServiceTemplate.class); @@ -329,7 +338,7 @@ public class TestApiRestServer { Response rawResponse2 = createResource(POLICYTYPES, standardCoder.encode(response)); assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), rawResponse2.getStatus()); ErrorResponse errorResponse = rawResponse2.readEntity(ErrorResponse.class); - assertEquals("no policy types specified on service template", errorResponse.getErrorMessage()); + assertEquals("no policy types specified in the service template", errorResponse.getErrorMessage()); } @Test @@ -378,7 +387,7 @@ public class TestApiRestServer { Response rawResponse2 = createResource(POLICIES, "src/test/resources/policies/BadTestPolicy.yaml"); assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), rawResponse2.getStatus()); ErrorResponse errorResponse = rawResponse2.readEntity(ErrorResponse.class); - assertEquals("policy type NULL:0.0.0 for policy onap.restart.tca:1.0.0 does not exist", + assertEquals("policy type NULL:0.0.0 for policy onap.restart.tca:2.0.0 does not exist", errorResponse.getErrorMessage()); } @@ -531,7 +540,10 @@ public class TestApiRestServer { rawResponse = readResource(POLICYTYPES_TCA_POLICIES_VCPE, mediaType); assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus()); - rawResponse = readResource(POLICYTYPES_TCA_POLICIES_VCPE_VERSION, mediaType); + rawResponse = readResource(POLICYTYPES_TCA_POLICIES_VCPE_VERSION1, mediaType); + assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus()); + + rawResponse = readResource(POLICYTYPES_TCA_POLICIES_VCPE_VERSION2, mediaType); assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus()); rawResponse = readResource(POLICYTYPES_TCA_POLICIES_VCPE_LATEST, mediaType); @@ -551,7 +563,7 @@ public class TestApiRestServer { private void testDeletePolicies(String mediaType) throws Exception { createPolicyTypes(); - Response rawResponse = deleteResource(POLICYTYPES_TCA_POLICIES_VCPE_VERSION, mediaType); + Response rawResponse = deleteResource(POLICYTYPES_TCA_POLICIES_VCPE_VERSION1, mediaType); assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawResponse.getStatus()); ErrorResponse error = rawResponse.readEntity(ErrorResponse.class); assertEquals( @@ -571,7 +583,7 @@ public class TestApiRestServer { } private void testDeletePolicyVersion(String mediaType) throws Exception { - for (String resrcName : TOSCA_PPOLICYTYPE_RESOURCE_NAMES) { + for (String resrcName : TOSCA_POLICYTYPE_RESOURCE_NAMES) { Response rawResponse = createResource(POLICYTYPES, resrcName); assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus()); ToscaServiceTemplate response = rawResponse.readEntity(ToscaServiceTemplate.class); @@ -582,10 +594,10 @@ public class TestApiRestServer { Response rawResponse = createResource(POLICYTYPES_TCA_POLICIES, resrcName); assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus()); } - Response rawResponse = deleteResource(POLICYTYPES_TCA_POLICIES_VCPE_VERSION, mediaType); + Response rawResponse = deleteResource(POLICYTYPES_TCA_POLICIES_VCPE_VERSION1, mediaType); assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus()); - rawResponse = readResource(POLICYTYPES_TCA_POLICIES_VCPE_VERSION, mediaType); + rawResponse = readResource(POLICYTYPES_TCA_POLICIES_VCPE_VERSION1, mediaType); assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawResponse.getStatus()); ErrorResponse errorResponse = rawResponse.readEntity(ErrorResponse.class); assertEquals( @@ -593,6 +605,17 @@ public class TestApiRestServer { + "onap.policies.monitoring.cdap.tca.hi.lo.app:1.0.0 does not exist", errorResponse.getErrorMessage()); + rawResponse = deleteResource(POLICYTYPES_TCA_POLICIES_VCPE_VERSION2, mediaType); + assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus()); + + rawResponse = readResource(POLICYTYPES_TCA_POLICIES_VCPE_VERSION2, mediaType); + assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawResponse.getStatus()); + errorResponse = rawResponse.readEntity(ErrorResponse.class); + assertEquals( + "policy with ID onap.restart.tca:2.0.0 and type " + + "onap.policies.monitoring.cdap.tca.hi.lo.app:1.0.0 does not exist", + errorResponse.getErrorMessage()); + rawResponse = readResource(POLICYTYPES_TCA_POLICIES_VCPE, mediaType); assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawResponse.getStatus()); errorResponse = rawResponse.readEntity(ErrorResponse.class); @@ -621,7 +644,7 @@ public class TestApiRestServer { } private void testGetAllVersionOfPolicy(String mediaType) throws Exception { - for (String resrcName : TOSCA_PPOLICYTYPE_RESOURCE_NAMES) { + for (String resrcName : TOSCA_POLICYTYPE_RESOURCE_NAMES) { Response rawResponse = createResource(POLICYTYPES, resrcName); assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus()); ToscaServiceTemplate response = rawResponse.readEntity(ToscaServiceTemplate.class); @@ -920,7 +943,7 @@ public class TestApiRestServer { } private void createPolicyTypes() throws Exception { - for (String resrcName : TOSCA_PPOLICYTYPE_RESOURCE_NAMES) { + for (String resrcName : TOSCA_POLICYTYPE_RESOURCE_NAMES) { Response rawResponse = createResource(POLICYTYPES, resrcName); assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus()); } @@ -1006,4 +1029,33 @@ public class TestApiRestServer { assertEquals(code, report.getCode()); assertEquals(message, report.getMessage()); } + + private void deleteToscaPolicies(ToscaEntityKey policyTypeKey) throws Exception { + + String getPoliciesPath = + "policytypes/" + policyTypeKey.getName() + "/versions/" + policyTypeKey.getVersion() + "/policies"; + + Response rawPolicyResponse = readResource(getPoliciesPath, APP_JSON); + if (Response.Status.OK.getStatusCode() == rawPolicyResponse.getStatus()) { + ToscaServiceTemplate policyResponse = rawPolicyResponse.readEntity(ToscaServiceTemplate.class); + + for (ToscaEntityKey policyKey : policyResponse.getToscaTopologyTemplate().getPoliciesAsMap().keySet()) { + String deletePolicyPath = + "policytypes/" + policyTypeKey.getName() + "/versions/" + policyTypeKey.getVersion() + + "/policies/" + policyKey.getName() + "/versions/" + policyKey.getVersion(); + deleteResource(deletePolicyPath, APP_JSON); + } + } + } + + private void deleteLegacyPolicies(String[] legacyPolicyNames, String legacyPolicyType) throws Exception { + + for (String policyName : legacyPolicyNames) { + String policyPath = + "policytypes/" + legacyPolicyType + "/versions/1.0.0/policies/" + policyName + "/versions/1"; + if (Response.Status.OK.getStatusCode() == readResource(policyPath, APP_JSON).getStatus()) { + deleteResource(policyPath, APP_JSON); + } + } + } }
\ No newline at end of file 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 3104a100..71ce44b1 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 @@ -78,6 +78,8 @@ public class TestLegacyGuardPolicyProvider { private static final String POLICY_RESOURCE = "policies/vDNS.policy.guard.frequency.input.json"; private static final String POLICY_RESOURCE_VER1 = "policies/vDNS.policy.guard.frequency.input.ver1.json"; private static final String POLICY_RESOURCE_VER2 = "policies/vDNS.policy.guard.frequency.input.ver2.json"; + private static final String POLICY_RESOURCE_WITH_NO_VERSION = + "policies/vDNS.policy.guard.frequency.no.policyversion.json"; private static final String POLICY_TYPE_RESOURCE = "policytypes/onap.policies.controlloop.guard.FrequencyLimiter.yaml"; private static final String POLICY_TYPE_ID = "onap.policies.controlloop.guard.FrequencyLimiter:1.0.0"; @@ -271,7 +273,7 @@ public class TestLegacyGuardPolicyProvider { } @Test - public void testCreateGuardPolicy() { + public void testCreateGuardPolicy() throws Exception { assertThatThrownBy(() -> { String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE); @@ -279,21 +281,33 @@ public class TestLegacyGuardPolicyProvider { guardPolicyProvider.createGuardPolicy(policyToCreate); }).hasMessage("policy type " + POLICY_TYPE_ID + " for policy " + POLICY_ID + " does not exist"); - assertThatCode(() -> { - ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder.decode( - ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class); - policyTypeProvider.createPolicyType(policyTypeServiceTemplate); + ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder.decode( + ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class); + policyTypeProvider.createPolicyType(policyTypeServiceTemplate); - String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE); - LegacyGuardPolicyInput policyToCreate = standardCoder.decode(policyString, LegacyGuardPolicyInput.class); - Map<String, LegacyGuardPolicyOutput> createdPolicy = guardPolicyProvider.createGuardPolicy(policyToCreate); - assertNotNull(createdPolicy); - assertFalse(createdPolicy.isEmpty()); - assertTrue(createdPolicy.containsKey("guard.frequency.scaleout")); - assertEquals("onap.policies.controlloop.guard.FrequencyLimiter", - createdPolicy.get("guard.frequency.scaleout").getType()); - assertEquals("1.0.0", createdPolicy.get("guard.frequency.scaleout").getVersion()); - }).doesNotThrowAnyException(); + String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE); + LegacyGuardPolicyInput policyToCreate = standardCoder.decode(policyString, LegacyGuardPolicyInput.class); + Map<String, LegacyGuardPolicyOutput> createdPolicy = guardPolicyProvider.createGuardPolicy(policyToCreate); + assertNotNull(createdPolicy); + assertFalse(createdPolicy.isEmpty()); + assertTrue(createdPolicy.containsKey("guard.frequency.scaleout")); + assertEquals("onap.policies.controlloop.guard.FrequencyLimiter", + createdPolicy.get("guard.frequency.scaleout").getType()); + assertEquals("1.0.0", createdPolicy.get("guard.frequency.scaleout").getVersion()); + + assertThatThrownBy(() -> { + String badPolicyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE_WITH_NO_VERSION); + LegacyGuardPolicyInput badPolicyToCreate = + standardCoder.decode(badPolicyString, LegacyGuardPolicyInput.class); + guardPolicyProvider.createGuardPolicy(badPolicyToCreate); + }).hasMessage("mandatory field 'policy-version' is missing in the policy: guard.frequency.scaleout"); + + assertThatThrownBy(() -> { + String duplicatePolicyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE); + LegacyGuardPolicyInput duplicatePolicyToCreate = + standardCoder.decode(duplicatePolicyString, LegacyGuardPolicyInput.class); + guardPolicyProvider.createGuardPolicy(duplicatePolicyToCreate); + }).hasMessage("guard policy guard.frequency.scaleout:1 already exists; its latest version is 1"); } @Test 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 31e22e3f..72e57a98 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 @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP Policy API * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved. * Modifications Copyright (C) 2019 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -74,6 +74,8 @@ public class TestLegacyOperationalPolicyProvider { private static StandardYamlCoder standardYamlCoder; private static final String POLICY_RESOURCE = "policies/vCPE.policy.operational.input.json"; + private static final String POLICY_RESOURCE_WITH_NO_VERSION = + "policies/vDNS.policy.operational.no.policyversion.json"; private static final String POLICY_TYPE_RESOURCE = "policytypes/onap.policies.controlloop.Operational.yaml"; private static final String POLICY_TYPE_ID = "onap.policies.controlloop.Operational:1.0.0"; private static final String POLICY_ID = "operational.restart:1.0.0"; @@ -119,7 +121,7 @@ public class TestLegacyOperationalPolicyProvider { } @Test - public void testFetchOperationalPolicy() { + public void testFetchOperationalPolicy() throws Exception { assertThatThrownBy(() -> { operationalPolicyProvider.fetchOperationalPolicy("dummy", null); @@ -129,31 +131,24 @@ public class TestLegacyOperationalPolicyProvider { operationalPolicyProvider.fetchOperationalPolicy("dummy", "dummy"); }).hasMessage("legacy policy version is not an integer"); - assertThatCode(() -> { - ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder.decode( - ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class); - policyTypeProvider.createPolicyType(policyTypeServiceTemplate); - - String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE); - LegacyOperationalPolicy policyToCreate = standardCoder.decode(policyString, LegacyOperationalPolicy.class); - LegacyOperationalPolicy createdPolicy = operationalPolicyProvider.createOperationalPolicy(policyToCreate); - assertNotNull(createdPolicy); + ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder.decode( + ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class); + policyTypeProvider.createPolicyType(policyTypeServiceTemplate); - policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE); - policyToCreate = standardCoder.decode(policyString, LegacyOperationalPolicy.class); - createdPolicy = operationalPolicyProvider.createOperationalPolicy(policyToCreate); - assertNotNull(createdPolicy); + String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE); + LegacyOperationalPolicy policyToCreate = standardCoder.decode(policyString, LegacyOperationalPolicy.class); + LegacyOperationalPolicy createdPolicy = operationalPolicyProvider.createOperationalPolicy(policyToCreate); + assertNotNull(createdPolicy); - LegacyOperationalPolicy firstVersion = - operationalPolicyProvider.fetchOperationalPolicy("operational.restart", "1"); - assertNotNull(firstVersion); - assertEquals("1", firstVersion.getPolicyVersion()); + LegacyOperationalPolicy firstVersion = + operationalPolicyProvider.fetchOperationalPolicy("operational.restart", "1"); + assertNotNull(firstVersion); + assertEquals("1", firstVersion.getPolicyVersion()); - LegacyOperationalPolicy latestVersion = - operationalPolicyProvider.fetchOperationalPolicy("operational.restart", null); - assertNotNull(latestVersion); - assertEquals("2", latestVersion.getPolicyVersion()); - }).doesNotThrowAnyException(); + LegacyOperationalPolicy latestVersion = + operationalPolicyProvider.fetchOperationalPolicy("operational.restart", null); + assertNotNull(latestVersion); + assertEquals("1", latestVersion.getPolicyVersion()); assertThatThrownBy(() -> { operationalPolicyProvider.fetchOperationalPolicy("operational.restart", "1.0.0"); @@ -163,11 +158,8 @@ public class TestLegacyOperationalPolicyProvider { operationalPolicyProvider.fetchOperationalPolicy("operational.restart", "latest");; }).hasMessage("legacy policy version is not an integer"); - assertThatCode(() -> { - operationalPolicyProvider.deleteOperationalPolicy("operational.restart", "1"); - operationalPolicyProvider.deleteOperationalPolicy("operational.restart", "2"); - policyTypeProvider.deletePolicyType("onap.policies.controlloop.Operational", "1.0.0"); - }).doesNotThrowAnyException(); + operationalPolicyProvider.deleteOperationalPolicy("operational.restart", "1"); + policyTypeProvider.deletePolicyType("onap.policies.controlloop.Operational", "1.0.0"); } @Test @@ -263,7 +255,7 @@ public class TestLegacyOperationalPolicyProvider { } @Test - public void testCreateOperationalPolicy() { + public void testCreateOperationalPolicy() throws Exception { assertThatThrownBy(() -> { String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE); @@ -271,18 +263,30 @@ public class TestLegacyOperationalPolicyProvider { operationalPolicyProvider.createOperationalPolicy(policyToCreate); }).hasMessage("policy type " + POLICY_TYPE_ID + " for policy " + POLICY_ID + " does not exist"); - assertThatCode(() -> { - ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder.decode( - ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class); - policyTypeProvider.createPolicyType(policyTypeServiceTemplate); + ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder.decode( + ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class); + policyTypeProvider.createPolicyType(policyTypeServiceTemplate); - String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE); - LegacyOperationalPolicy policyToCreate = standardCoder.decode(policyString, LegacyOperationalPolicy.class); - LegacyOperationalPolicy createdPolicy = operationalPolicyProvider.createOperationalPolicy(policyToCreate); - assertNotNull(createdPolicy); - assertEquals("operational.restart", createdPolicy.getPolicyId()); - assertTrue(createdPolicy.getContent().startsWith("controlLoop%3A%0A%20%20version%3A%202.0.0%0A%20%20")); - }).doesNotThrowAnyException(); + String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE); + LegacyOperationalPolicy policyToCreate = standardCoder.decode(policyString, LegacyOperationalPolicy.class); + LegacyOperationalPolicy createdPolicy = operationalPolicyProvider.createOperationalPolicy(policyToCreate); + assertNotNull(createdPolicy); + assertEquals("operational.restart", createdPolicy.getPolicyId()); + assertTrue(createdPolicy.getContent().startsWith("controlLoop%3A%0A%20%20version%3A%202.0.0%0A%20%20")); + + assertThatThrownBy(() -> { + String badPolicyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE_WITH_NO_VERSION); + LegacyOperationalPolicy badPolicyToCreate = + standardCoder.decode(badPolicyString, LegacyOperationalPolicy.class); + operationalPolicyProvider.createOperationalPolicy(badPolicyToCreate); + }).hasMessage("mandatory field 'policy-version' is missing in the policy: operational.scaleout"); + + assertThatThrownBy(() -> { + String duplicatePolicyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE); + LegacyOperationalPolicy duplicatePolicyToCreate = + standardCoder.decode(duplicatePolicyString, LegacyOperationalPolicy.class); + operationalPolicyProvider.createOperationalPolicy(duplicatePolicyToCreate); + }).hasMessage("operational policy operational.restart:1 already exists; its latest version is 1"); } @Test 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 4221c670..aed40bc9 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 @@ -76,6 +76,9 @@ public class TestPolicyProvider { private static final String POLICY_RESOURCE_WITH_BAD_POLICYTYPE_ID = "policies/vCPE.policy.bad.policytypeid.json"; private static final String POLICY_RESOURCE_WITH_BAD_POLICYTYPE_VERSION = "policies/vCPE.policy.bad.policytypeversion.json"; + private static final String POLICY_RESOURCE_WITH_NO_POLICY_VERSION = "policies/vCPE.policy.no.policyversion.json"; + private static final String POLICY_RESOURCE_WITH_DUPLICATE_POLICY_VERSION = + "policies/vCPE.policy.duplicate.policyversion.json"; private static final String MULTIPLE_POLICIES_RESOURCE = "policies/vCPE.policies.optimization.input.tosca.json"; // @formatter:off @@ -86,7 +89,8 @@ public class TestPolicyProvider { "policytypes/onap.policies.optimization.service.QueryPolicy.yaml", "policytypes/onap.policies.optimization.service.SubscriberPolicy.yaml", "policytypes/onap.policies.optimization.resource.Vim_fit.yaml", - "policytypes/onap.policies.optimization.resource.VnfPolicy.yaml" + "policytypes/onap.policies.optimization.resource.VnfPolicy.yaml", + "policytypes/onap.policies.monitoring.cdap.tca.hi.lo.app.yaml" }; // @formatter:on @@ -245,17 +249,15 @@ public class TestPolicyProvider { } @Test - public void testCreatePolicy() { + public void testCreatePolicy() throws Exception { assertThatThrownBy(() -> { policyProvider.createPolicy("dummy", "1.0.0", new ToscaServiceTemplate()); }).hasMessage("policy type with ID dummy:1.0.0 does not exist"); - assertThatCode(() -> { - ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder.decode( - ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class); - policyTypeProvider.createPolicyType(policyTypeServiceTemplate); - }).doesNotThrowAnyException(); + ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder.decode( + ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class); + policyTypeProvider.createPolicyType(policyTypeServiceTemplate); assertThatThrownBy(() -> { String badPolicyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE_WITH_BAD_POLICYTYPE_ID); @@ -273,13 +275,35 @@ public class TestPolicyProvider { badPolicyServiceTemplate); }).hasMessage("policy type version does not match"); - assertThatCode(() -> { - String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE); - ToscaServiceTemplate policyServiceTemplate = standardCoder.decode(policyString, ToscaServiceTemplate.class); - ToscaServiceTemplate serviceTemplate = policyProvider - .createPolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0", policyServiceTemplate); - assertFalse(serviceTemplate.getToscaTopologyTemplate().getPolicies().get(0).isEmpty()); - }).doesNotThrowAnyException(); + assertThatThrownBy(() -> { + String badPolicyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE_WITH_NO_POLICY_VERSION); + ToscaServiceTemplate badPolicyServiceTemplate = + standardCoder.decode(badPolicyString, ToscaServiceTemplate.class); + policyProvider.createPolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0", + badPolicyServiceTemplate); + }).hasMessage("mandatory 'version' field is missing in policies: onap.restart.tca"); + + assertThatThrownBy(() -> { + String badPolicyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE_WITH_DUPLICATE_POLICY_VERSION); + ToscaServiceTemplate badPolicyServiceTemplate = + standardCoder.decode(badPolicyString, ToscaServiceTemplate.class); + policyProvider.createPolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0", + badPolicyServiceTemplate); + }).hasMessage("the same version of policies 'onap.restart.tca:1.0.0' appear multiple times in the payload"); + + String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE); + ToscaServiceTemplate policyServiceTemplate = standardCoder.decode(policyString, ToscaServiceTemplate.class); + ToscaServiceTemplate serviceTemplate = policyProvider + .createPolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0", policyServiceTemplate); + assertFalse(serviceTemplate.getToscaTopologyTemplate().getPolicies().get(0).isEmpty()); + + assertThatThrownBy(() -> { + String badPolicyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE); + ToscaServiceTemplate badPolicyServiceTemplate = + standardCoder.decode(badPolicyString, ToscaServiceTemplate.class); + policyProvider.createPolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0", + badPolicyServiceTemplate); + }).hasMessage("policy onap.restart.tca:1.0.0 already exists; its latest version is 1.0.0"); } @Test @@ -306,6 +330,13 @@ public class TestPolicyProvider { ToscaServiceTemplate multiPoliciesServiceTemplate = standardCoder.decode(multiPoliciesString, ToscaServiceTemplate.class); policyProvider.createPolicies(multiPoliciesServiceTemplate); + + assertThatThrownBy(() -> { + String badPolicyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE_WITH_DUPLICATE_POLICY_VERSION); + ToscaServiceTemplate badPolicyServiceTemplate = + standardCoder.decode(badPolicyString, ToscaServiceTemplate.class); + policyProvider.createPolicies(badPolicyServiceTemplate); + }).hasMessage("the same version of policies 'onap.restart.tca:1.0.0' appear multiple times in the payload"); } @Test 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 a17a7c9a..cc84febd 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 @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP Policy API * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved. * Modifications Copyright (C) 2019 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -23,7 +23,6 @@ package org.onap.policy.api.main.rest.provider; -import static org.assertj.core.api.Assertions.assertThatCode; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertFalse; @@ -56,6 +55,10 @@ public class TestPolicyTypeProvider { private static final String POLICY_RESOURCE = "policies/vCPE.policy.monitoring.input.tosca.yaml"; private static final String POLICY_TYPE_RESOURCE = "policytypes/onap.policies.monitoring.cdap.tca.hi.lo.app.yaml"; + private static final String POLICY_TYPE_RESOURCE_WITH_NO_VERSION = + "policytypes/onap.policies.optimization.Resource.no.version.yaml"; + private static final String POLICY_TYPE_NAME = "onap.policies.monitoring.cdap.tca.hi.lo.app"; + private static final String POLICY_TYPE_VERSION = "1.0.0"; /** * Initializes parameters. @@ -92,12 +95,10 @@ public class TestPolicyTypeProvider { } @Test - public void testFetchPolicyTypes() { + public void testFetchPolicyTypes() throws Exception { - assertThatCode(() -> { - ToscaServiceTemplate serviceTemplate = policyTypeProvider.fetchPolicyTypes(null, null); - assertFalse(serviceTemplate.getPolicyTypes().isEmpty()); - }).doesNotThrowAnyException(); + ToscaServiceTemplate serviceTemplate = policyTypeProvider.fetchPolicyTypes(null, null); + assertFalse(serviceTemplate.getPolicyTypes().isEmpty()); assertThatThrownBy(() -> { policyTypeProvider.fetchPolicyTypes("dummy", null); @@ -117,31 +118,41 @@ public class TestPolicyTypeProvider { } @Test - public void testCreatePolicyType() { + public void testCreatePolicyType() throws Exception { - assertThatCode(() -> { - ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder.decode( + ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder.decode( + ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class); + ToscaServiceTemplate serviceTemplate = policyTypeProvider.createPolicyType(policyTypeServiceTemplate); + assertFalse(serviceTemplate.getPolicyTypes().isEmpty()); + + String errorMessage = "policy type onap.policies.monitoring.cdap.tca.hi.lo.app:1.0.0 already exists; " + + "its latest version is 1.0.0"; + assertThatThrownBy(() -> { + ToscaServiceTemplate duplicatePolicyType = standardYamlCoder.decode( ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class); - ToscaServiceTemplate serviceTemplate = policyTypeProvider.createPolicyType(policyTypeServiceTemplate); - assertFalse(serviceTemplate.getPolicyTypes().isEmpty()); - }).doesNotThrowAnyException(); + policyTypeProvider.createPolicyType(duplicatePolicyType); + }).hasMessage(errorMessage); + + assertThatThrownBy(() -> { + ToscaServiceTemplate badPolicyType = standardYamlCoder.decode(ResourceUtils.getResourceAsString( + POLICY_TYPE_RESOURCE_WITH_NO_VERSION), ToscaServiceTemplate.class); + policyTypeProvider.createPolicyType(badPolicyType); + }).hasMessage("mandatory 'version' field is missing in policy types: onap.policies.optimization.Resource"); + + policyTypeProvider.deletePolicyType(POLICY_TYPE_NAME, POLICY_TYPE_VERSION); } @Test - public void testDeletePolicyType() { + public void testDeletePolicyType() throws Exception { - assertThatCode(() -> { - ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder.decode( - ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class); - ToscaServiceTemplate serviceTemplate = policyTypeProvider.createPolicyType(policyTypeServiceTemplate); - assertFalse(serviceTemplate.getPolicyTypes().isEmpty()); - }).doesNotThrowAnyException(); + ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder.decode( + ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class); + ToscaServiceTemplate serviceTemplate = policyTypeProvider.createPolicyType(policyTypeServiceTemplate); + assertFalse(serviceTemplate.getPolicyTypes().isEmpty()); - assertThatCode(() -> { - ToscaServiceTemplate policyServiceTemplate = standardYamlCoder.decode( - ResourceUtils.getResourceAsString(POLICY_RESOURCE), ToscaServiceTemplate.class); - policyProvider.createPolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0", policyServiceTemplate); - }).doesNotThrowAnyException(); + ToscaServiceTemplate policyServiceTemplate = standardYamlCoder.decode( + ResourceUtils.getResourceAsString(POLICY_RESOURCE), ToscaServiceTemplate.class); + policyProvider.createPolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0", policyServiceTemplate); String exceptionMessage = "policy type with ID onap.policies.monitoring.cdap.tca.hi.lo.app:1.0.0 " + "cannot be deleted as it is parameterized by policies onap.restart.tca:1.0.0"; @@ -149,17 +160,13 @@ public class TestPolicyTypeProvider { policyTypeProvider.deletePolicyType("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0"); }).hasMessage(exceptionMessage); - assertThatCode(() -> { - ToscaServiceTemplate serviceTemplate = policyProvider - .deletePolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0", "onap.restart.tca", "1.0.0"); - assertFalse(serviceTemplate.getToscaTopologyTemplate().getPolicies().get(0).isEmpty()); - }).doesNotThrowAnyException(); - - assertThatCode(() -> { - ToscaServiceTemplate serviceTemplate = - policyTypeProvider.deletePolicyType("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0"); - assertFalse(serviceTemplate.getPolicyTypes().isEmpty()); - }).doesNotThrowAnyException(); + serviceTemplate = policyProvider + .deletePolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0", "onap.restart.tca", "1.0.0"); + assertFalse(serviceTemplate.getToscaTopologyTemplate().getPolicies().get(0).isEmpty()); + + serviceTemplate = + policyTypeProvider.deletePolicyType("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0"); + assertFalse(serviceTemplate.getPolicyTypes().isEmpty()); assertThatThrownBy(() -> { policyTypeProvider.deletePolicyType("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0"); |