summaryrefslogtreecommitdiffstats
path: root/main/src/test/java
diff options
context:
space:
mode:
Diffstat (limited to 'main/src/test/java')
-rw-r--r--main/src/test/java/org/onap/policy/api/main/rest/provider/TestLegacyGuardPolicyProvider.java107
-rw-r--r--main/src/test/java/org/onap/policy/api/main/rest/provider/TestLegacyOperationalPolicyProvider.java98
-rw-r--r--main/src/test/java/org/onap/policy/api/main/rest/provider/TestPolicyProvider.java133
-rw-r--r--main/src/test/java/org/onap/policy/api/main/rest/provider/TestPolicyTypeProvider.java112
4 files changed, 377 insertions, 73 deletions
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 08f7fecd..085255f3 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
@@ -22,20 +22,26 @@
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.assertEquals;
+import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.fail;
+import static org.junit.Assert.assertTrue;
import java.util.Base64;
-import org.junit.Before;
+import java.util.Map;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
import org.junit.Test;
import org.onap.policy.api.main.parameters.ApiParameterGroup;
import org.onap.policy.common.parameters.ParameterService;
+import org.onap.policy.common.utils.coder.StandardCoder;
+import org.onap.policy.common.utils.resources.ResourceUtils;
import org.onap.policy.models.base.PfModelException;
import org.onap.policy.models.provider.PolicyModelsProviderParameters;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyInput;
+import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyOutput;
/**
* This class performs unit test of {@link LegacyGuardPolicyProvider}
@@ -44,38 +50,105 @@ import org.slf4j.LoggerFactory;
*/
public class TestLegacyGuardPolicyProvider {
- private static final Logger LOGGER = LoggerFactory.getLogger(TestPolicyProvider.class);
+ private static LegacyGuardPolicyProvider guardPolicyProvider;
+ private static PolicyModelsProviderParameters providerParams;
+ private static ApiParameterGroup apiParamGroup;
+ private static StandardCoder standardCoder;
- private LegacyGuardPolicyProvider guardPolicyProvider;
+ private static final String POLICY_RESOURCE = "policies/vDNS.policy.guard.frequency.input.json";
/**
- * Initialize parameters.
+ * Initializes parameters.
+ *
+ * @throws PfModelException the PfModel parsing exception
*/
- @Before
- public void setupParameters() throws PfModelException {
-
- PolicyModelsProviderParameters parameters = new PolicyModelsProviderParameters();
- parameters.setDatabaseUrl("jdbc:h2:mem:testdb");
- parameters.setDatabaseUser("policy");
- parameters.setDatabasePassword(Base64.getEncoder().encodeToString("P01icY".getBytes()));
- parameters.setPersistenceUnit("ToscaConceptTest");
- ApiParameterGroup paramGroup = new ApiParameterGroup("ApiGroup", null, parameters);
- ParameterService.register(paramGroup, true);
+ @BeforeClass
+ public static void setupParameters() throws PfModelException {
+
+ standardCoder = new StandardCoder();
+ providerParams = new PolicyModelsProviderParameters();
+ 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);
guardPolicyProvider = new LegacyGuardPolicyProvider();
}
+ /**
+ * Closes up DB connections and deregisters API parameter group.
+ *
+ * @throws PfModelException the PfModel parsing exception
+ */
+ @AfterClass
+ public static void tearDown() throws PfModelException {
+
+ guardPolicyProvider.close();
+ ParameterService.deregister(apiParamGroup);
+ }
+
+
@Test
public void testFetchGuardPolicy() {
+ assertThatThrownBy(() -> {
+ guardPolicyProvider.fetchGuardPolicy("dummy", null);
+ }).hasMessage("no policy found for policy ID: dummy");
+
+ assertThatThrownBy(() -> {
+ guardPolicyProvider.fetchGuardPolicy("dummy", "dummy");
+ }).hasMessage("no policy found for policy ID: dummy");
}
@Test
public void testCreateGuardPolicy() {
+ assertThatCode(() -> {
+ 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();
}
@Test
public void testDeleteGuardPolicy() {
+ assertThatThrownBy(() -> {
+ guardPolicyProvider.deleteGuardPolicy("dummy", null);
+ }).hasMessage("version is marked @NonNull but is null");
+
+ assertThatThrownBy(() -> {
+ guardPolicyProvider.deleteGuardPolicy("dummy", "dummy");
+ }).hasMessage("no policy found for policy ID: dummy");
+
+ assertThatCode(() -> {
+ 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());
+ }).doesNotThrowAnyException();
+
+ assertThatCode(() -> {
+ Map<String, LegacyGuardPolicyOutput> deletedPolicy = guardPolicyProvider
+ .deleteGuardPolicy("guard.frequency.scaleout", "1.0.0");
+ assertNotNull(deletedPolicy);
+ assertFalse(deletedPolicy.isEmpty());
+ assertTrue(deletedPolicy.containsKey("guard.frequency.scaleout"));
+ assertEquals("onap.policies.controlloop.guard.FrequencyLimiter",
+ deletedPolicy.get("guard.frequency.scaleout").getType());
+ assertEquals("1.0.0", deletedPolicy.get("guard.frequency.scaleout").getVersion());
+ }).doesNotThrowAnyException();
+
+ assertThatThrownBy(() -> {
+ guardPolicyProvider.deleteGuardPolicy("guard.frequency.scaleout", "1.0.0");
+ }).hasMessage("no policy found for policy ID: guard.frequency.scaleout");
}
}
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 86124c74..e9adae7c 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
@@ -22,20 +22,23 @@
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.assertEquals;
import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.fail;
+import static org.junit.Assert.assertTrue;
import java.util.Base64;
-import org.junit.Before;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
import org.junit.Test;
import org.onap.policy.api.main.parameters.ApiParameterGroup;
import org.onap.policy.common.parameters.ParameterService;
+import org.onap.policy.common.utils.coder.StandardCoder;
+import org.onap.policy.common.utils.resources.ResourceUtils;
import org.onap.policy.models.base.PfModelException;
import org.onap.policy.models.provider.PolicyModelsProviderParameters;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy;
/**
* This class performs unit test of {@link LegacyOperationalPolicyProvider}
@@ -44,38 +47,99 @@ import org.slf4j.LoggerFactory;
*/
public class TestLegacyOperationalPolicyProvider {
- private static final Logger LOGGER = LoggerFactory.getLogger(TestPolicyProvider.class);
+ private static LegacyOperationalPolicyProvider operationalPolicyProvider;
+ private static PolicyModelsProviderParameters providerParams;
+ private static ApiParameterGroup apiParamGroup;
+ private static StandardCoder standardCoder;
- private LegacyOperationalPolicyProvider operationalPolicyProvider;
+ private static final String POLICY_RESOURCE = "policies/vCPE.policy.operational.input.json";
/**
- * Initialize parameters.
+ * Initializes parameters.
+ *
+ * @throws PfModelException the PfModel parsing exception
*/
- @Before
- public void setupParameters() throws PfModelException {
-
- PolicyModelsProviderParameters parameters = new PolicyModelsProviderParameters();
- parameters.setDatabaseUrl("jdbc:h2:mem:testdb");
- parameters.setDatabaseUser("policy");
- parameters.setDatabasePassword(Base64.getEncoder().encodeToString("P01icY".getBytes()));
- parameters.setPersistenceUnit("ToscaConceptTest");
- ApiParameterGroup paramGroup = new ApiParameterGroup("ApiGroup", null, parameters);
- ParameterService.register(paramGroup, true);
+ @BeforeClass
+ public static void setupParameters() throws PfModelException {
+
+ standardCoder = new StandardCoder();
+ providerParams = new PolicyModelsProviderParameters();
+ 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);
operationalPolicyProvider = new LegacyOperationalPolicyProvider();
}
+ /**
+ * Closes up DB connections and deregisters API parameter group.
+ *
+ * @throws PfModelException the PfModel parsing exception
+ */
+ @AfterClass
+ public static void tearDown() throws PfModelException {
+
+ operationalPolicyProvider.close();
+ ParameterService.deregister(apiParamGroup);
+ }
+
@Test
public void testFetchOperationalPolicy() {
+ assertThatThrownBy(() -> {
+ operationalPolicyProvider.fetchOperationalPolicy("dummy", null);
+ }).hasMessage("no policy found for policy ID: dummy");
+
+ assertThatThrownBy(() -> {
+ operationalPolicyProvider.fetchOperationalPolicy("dummy", "dummy");
+ }).hasMessage("no policy found for policy ID: dummy");
}
@Test
public void testCreateOperationalPolicy() {
+ assertThatCode(() -> {
+ 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();
}
@Test
public void testDeleteOperationalPolicy() {
+ assertThatThrownBy(() -> {
+ operationalPolicyProvider.deleteOperationalPolicy("dummy", null);
+ }).hasMessage("version is marked @NonNull but is null");
+
+ assertThatThrownBy(() -> {
+ operationalPolicyProvider.deleteOperationalPolicy("dummy", "dummy");
+ }).hasMessage("no policy found for policy ID: dummy");
+
+ assertThatCode(() -> {
+ String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
+ LegacyOperationalPolicy policyToCreate = standardCoder.decode(policyString, LegacyOperationalPolicy.class);
+ LegacyOperationalPolicy createdPolicy = operationalPolicyProvider.createOperationalPolicy(policyToCreate);
+ assertNotNull(createdPolicy);
+ }).doesNotThrowAnyException();
+
+ assertThatCode(() -> {
+ LegacyOperationalPolicy deletedPolicy = operationalPolicyProvider
+ .deleteOperationalPolicy("operational.restart", "1.0.0");
+ assertNotNull(deletedPolicy);
+ assertEquals("operational.restart", deletedPolicy.getPolicyId());
+ assertTrue(deletedPolicy.getContent()
+ .startsWith("controlLoop%3A%0A%20%20version%3A%202.0.0%0A%20%20"));
+ }).doesNotThrowAnyException();
+
+ assertThatThrownBy(() -> {
+ operationalPolicyProvider.deleteOperationalPolicy("operational.restart", "1.0.0");
+ }).hasMessage("no policy found for policy ID: operational.restart");
}
}
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 eeb4dbae..7720575a 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
@@ -22,20 +22,21 @@
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.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.fail;
+import static org.junit.Assert.assertFalse;
import java.util.Base64;
-import org.junit.Before;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
import org.junit.Test;
import org.onap.policy.api.main.parameters.ApiParameterGroup;
import org.onap.policy.common.parameters.ParameterService;
+import org.onap.policy.common.utils.coder.StandardCoder;
+import org.onap.policy.common.utils.resources.ResourceUtils;
import org.onap.policy.models.base.PfModelException;
import org.onap.policy.models.provider.PolicyModelsProviderParameters;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
/**
* This class performs unit test of {@link PolicyProvider}
@@ -44,49 +45,141 @@ import org.slf4j.LoggerFactory;
*/
public class TestPolicyProvider {
- private static final Logger LOGGER = LoggerFactory.getLogger(TestPolicyProvider.class);
+ private static PolicyProvider policyProvider;
+ private static PolicyTypeProvider policyTypeProvider;
+ private static PolicyModelsProviderParameters providerParams;
+ private static ApiParameterGroup apiParamGroup;
+ private static StandardCoder standardCoder;
- private PolicyProvider policyProvider;
+ private static final String POLICY_RESOURCE = "policies/vCPE.policy.monitoring.input.tosca.json";
+ private static final String POLICY_TYPE_RESOURCE = "policytypes/onap.policy.monitoring.cdap.tca.hi.lo.app.json";
+ 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";
/**
- * Initialize parameters.
+ * Initializes parameters.
+ *
+ * @throws PfModelException the PfModel parsing exception
*/
- @Before
- public void setupParameters() throws PfModelException {
-
- PolicyModelsProviderParameters parameters = new PolicyModelsProviderParameters();
- parameters.setDatabaseUrl("jdbc:h2:mem:testdb");
- parameters.setDatabaseUser("policy");
- parameters.setDatabasePassword(Base64.getEncoder().encodeToString("P01icY".getBytes()));
- parameters.setPersistenceUnit("ToscaConceptTest");
- ApiParameterGroup paramGroup = new ApiParameterGroup("ApiGroup", null, parameters);
- ParameterService.register(paramGroup, true);
+ @BeforeClass
+ public static void setupParameters() throws PfModelException {
+
+ standardCoder = new StandardCoder();
+ providerParams = new PolicyModelsProviderParameters();
+ 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();
- System.out.println("xxxx");
+ }
+
+ /**
+ * Closes up DB connections and deregisters API parameter group.
+ *
+ * @throws PfModelException the PfModel parsing exception
+ */
+ @AfterClass
+ public static void tearDown() throws PfModelException {
+
+ policyTypeProvider.close();
+ policyProvider.close();
+ ParameterService.deregister(apiParamGroup);
}
@Test
public void testFetchPolicies() {
+ assertThatThrownBy(() -> {
+ policyProvider.fetchPolicies("dummy", "dummy", null, null);
+ }).hasMessage("policy with ID null:null and type dummy:dummy does not exist");
+
+ assertThatThrownBy(() -> {
+ policyProvider.fetchPolicies("dummy", "dummy", "dummy", null);
+ }).hasMessage("policy with ID dummy:null and type dummy:dummy does not exist");
+
+ assertThatThrownBy(() -> {
+ policyProvider.fetchPolicies("dummy", "dummy", "dummy", "dummy");
+ }).hasMessage("policy with ID dummy:dummy and type dummy:dummy does not exist");
}
@Test
public void testFetchLatestPolicies() {
+ assertThatThrownBy(() -> {
+ policyProvider.fetchLatestPolicies("dummy", "dummy", "dummy");
+ }).hasMessage("policy with ID dummy:null and type dummy:dummy does not exist");
}
@Test
public void testFetchDeployedPolicies() {
+ assertThatThrownBy(() -> {
+ policyProvider.fetchDeployedPolicies("dummy", "dummy", "dummy");
+ }).hasMessage("could not find policy with ID dummy and type dummy:dummy deployed in any pdp group");
}
@Test
public void testCreatePolicy() {
+ assertThatThrownBy(() -> {
+ policyProvider.createPolicy("dummy", "dummy", new ToscaServiceTemplate());
+ }).hasMessage("policy type with ID dummy:dummy does not exist");
+
+ assertThatCode(() -> {
+ String policyTypeString = ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE);
+ ToscaServiceTemplate policyTypeServiceTemplate =
+ standardCoder.decode(policyTypeString, ToscaServiceTemplate.class);
+ policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
+ }).doesNotThrowAnyException();
+
+ assertThatThrownBy(() -> {
+ String badPolicyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE_WITH_BAD_POLICYTYPE_ID);
+ ToscaServiceTemplate badPolicyServiceTemplate =
+ standardCoder.decode(badPolicyString, ToscaServiceTemplate.class);
+ policyProvider.createPolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0",
+ badPolicyServiceTemplate);
+ }).hasMessage("policy type id does not match");
+
+ assertThatThrownBy(() -> {
+ String badPolicyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE_WITH_BAD_POLICYTYPE_VERSION);
+ ToscaServiceTemplate badPolicyServiceTemplate =
+ standardCoder.decode(badPolicyString, ToscaServiceTemplate.class);
+ policyProvider.createPolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0",
+ 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();
}
@Test
public void testDeletePolicy() {
+ assertThatThrownBy(() -> {
+ policyProvider.deletePolicy("dummy", "dummy", "dummy", "dummy");
+ }).hasMessage("policy with ID dummy:dummy and type dummy:dummy does not exist");
+
+ 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();
+
+ String exceptionMessage = "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";
+ assertThatThrownBy(() -> {
+ policyProvider.deletePolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0",
+ "onap.restart.tca", "1.0.0");
+ }).hasMessage(exceptionMessage);
}
}
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 4c3f23b3..76825b20 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
@@ -22,20 +22,21 @@
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.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.fail;
+import static org.junit.Assert.assertFalse;
import java.util.Base64;
-import org.junit.Before;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
import org.junit.Test;
import org.onap.policy.api.main.parameters.ApiParameterGroup;
import org.onap.policy.common.parameters.ParameterService;
+import org.onap.policy.common.utils.coder.StandardCoder;
+import org.onap.policy.common.utils.resources.ResourceUtils;
import org.onap.policy.models.base.PfModelException;
import org.onap.policy.models.provider.PolicyModelsProviderParameters;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
/**
* This class performs unit test of {@link PolicyTypeProvider}
@@ -44,43 +45,116 @@ import org.slf4j.LoggerFactory;
*/
public class TestPolicyTypeProvider {
- private static final Logger LOGGER = LoggerFactory.getLogger(TestPolicyTypeProvider.class);
+ private static PolicyTypeProvider policyTypeProvider;
+ private static PolicyProvider policyProvider;
+ private static PolicyModelsProviderParameters providerParams;
+ private static ApiParameterGroup apiParamGroup;
+ private static StandardCoder standardCoder;
- private PolicyTypeProvider policyTypeProvider;
+ private static final String POLICY_RESOURCE = "policies/vCPE.policy.monitoring.input.tosca.json";
+ private static final String POLICY_TYPE_RESOURCE = "policytypes/onap.policy.monitoring.cdap.tca.hi.lo.app.json";
/**
- * Initialize parameters.
+ * Initializes parameters.
+ *
+ * @throws PfModelException the PfModel parsing exception
*/
- @Before
- public void setupParameters() throws PfModelException {
-
- PolicyModelsProviderParameters parameters = new PolicyModelsProviderParameters();
- parameters.setDatabaseUrl("jdbc:h2:mem:testdb");
- parameters.setDatabaseUser("policy");
- parameters.setDatabasePassword(Base64.getEncoder().encodeToString("P01icY".getBytes()));
- parameters.setPersistenceUnit("ToscaConceptTest");
- ApiParameterGroup paramGroup = new ApiParameterGroup("ApiGroup", null, parameters);
- ParameterService.register(paramGroup, true);
+ @BeforeClass
+ public static void setupParameters() throws PfModelException {
+
+ standardCoder = new StandardCoder();
+ providerParams = new PolicyModelsProviderParameters();
+ 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();
+ }
+
+ /**
+ * Closes up DB connections and deregisters API parameter group.
+ *
+ * @throws PfModelException the PfModel parsing exception
+ */
+ @AfterClass
+ public static void tearDown() throws PfModelException {
+
+ policyTypeProvider.close();
+ policyProvider.close();
+ ParameterService.deregister(apiParamGroup);
}
@Test
public void testFetchPolicyTypes() {
+ assertThatCode(() -> {
+ ToscaServiceTemplate serviceTemplate = policyTypeProvider.fetchPolicyTypes(null, null);
+ assertFalse(serviceTemplate.getPolicyTypes().isEmpty());
+ }).doesNotThrowAnyException();
+
+ assertThatThrownBy(() -> {
+ policyTypeProvider.fetchPolicyTypes("dummy", null);
+ }).hasMessage("policy type with ID dummy:null does not exist");
+
+ assertThatThrownBy(() -> {
+ policyTypeProvider.fetchPolicyTypes("dummy", "dummy");
+ }).hasMessage("policy type with ID dummy:dummy does not exist");
}
@Test
public void testFetchLatestPolicyTypes() {
+ assertThatThrownBy(() -> {
+ policyTypeProvider.fetchLatestPolicyTypes("dummy");
+ }).hasMessage("policy type with ID dummy:null does not exist");
}
@Test
public void testCreatePolicyType() {
+ assertThatCode(() -> {
+ String policyTypeString = ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE);
+ ToscaServiceTemplate policyTypeServiceTemplate =
+ standardCoder.decode(policyTypeString, ToscaServiceTemplate.class);
+ ToscaServiceTemplate serviceTemplate = policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
+ assertFalse(serviceTemplate.getPolicyTypes().get(0).isEmpty());
+ }).doesNotThrowAnyException();
}
@Test
public void testDeletePolicyType() {
+ assertThatCode(() -> {
+ String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
+ ToscaServiceTemplate policyServiceTemplate =
+ standardCoder.decode(policyString, ToscaServiceTemplate.class);
+ policyProvider.createPolicy(
+ "onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0", policyServiceTemplate);
+ }).doesNotThrowAnyException();
+
+ 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";
+ assertThatThrownBy(() -> {
+ 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().get(0).isEmpty());
+ }).doesNotThrowAnyException();
+
+ assertThatThrownBy(() -> {
+ policyTypeProvider.deletePolicyType("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0");
+ }).hasMessage("policy type with ID onap.policies.monitoring.cdap.tca.hi.lo.app:1.0.0 does not exist");
}
}