* ONAP Policy API
* ================================================================================
* Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * Modifications Copyright (C) 2019-2020 Nordix Foundation.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
import java.util.ArrayList;
import java.util.Base64;
+import java.util.Collections;
import java.util.List;
import java.util.Map;
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.coder.StandardYamlCoder;
import org.onap.policy.common.utils.resources.ResourceUtils;
import org.onap.policy.models.base.PfModelException;
import org.onap.policy.models.pdp.concepts.Pdp;
import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyOutput;
/**
- * This class performs unit test of {@link LegacyGuardPolicyProvider}
+ * This class performs unit test of {@link LegacyGuardPolicyProvider}.
*
* @author Chenfei Gao (cgao@research.att.com)
*/
private static PolicyModelsProviderParameters providerParams;
private static ApiParameterGroup apiParamGroup;
private static StandardCoder standardCoder;
+ private static StandardYamlCoder standardYamlCoder;
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.json";
+ "policytypes/onap.policies.controlloop.guard.FrequencyLimiter.yaml";
private static final String POLICY_TYPE_ID = "onap.policies.controlloop.guard.FrequencyLimiter:1.0.0";
private static final String POLICY_TYPE_NAME = "onap.policies.controlloop.guard.FrequencyLimiter";
private static final String POLICY_TYPE_VERSION = "1.0.0";
- private static final String POLICY_ID = "guard.frequency.scaleout:1.0.0";
private static final String POLICY_NAME = "guard.frequency.scaleout";
private static final String POLICY_VERSION = "1";
private static final String LEGACY_MINOR_PATCH_SUFFIX = ".0.0";
*/
@Before
public void setupParameters() throws PfModelException {
-
standardCoder = new StandardCoder();
+ standardYamlCoder = new StandardYamlCoder();
providerParams = new PolicyModelsProviderParameters();
providerParams.setDatabaseDriver("org.h2.Driver");
providerParams.setDatabaseUrl("jdbc:h2:mem:testdb");
providerParams.setDatabaseUser("policy");
providerParams.setDatabasePassword(Base64.getEncoder().encodeToString("P01icY".getBytes()));
providerParams.setPersistenceUnit("ToscaConceptTest");
- apiParamGroup = new ApiParameterGroup("ApiGroup", null, providerParams);
+ apiParamGroup = new ApiParameterGroup("ApiGroup", null, providerParams, Collections.emptyList());
ParameterService.register(apiParamGroup, true);
guardPolicyProvider = new LegacyGuardPolicyProvider();
policyTypeProvider = new PolicyTypeProvider();
*/
@After
public void tearDown() throws PfModelException {
-
guardPolicyProvider.close();
policyTypeProvider.close();
ParameterService.deregister(apiParamGroup);
}
-
@Test
public void testFetchGuardPolicy() {
}).hasMessage("legacy policy version is not an integer");
assertThatCode(() -> {
- String policyTypeString = ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE);
- ToscaServiceTemplate policyTypeServiceTemplate =
- standardCoder.decode(policyTypeString, ToscaServiceTemplate.class);
+ ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder
+ .decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class);
policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE_VER1);
pdpSubGroup.setPdpType("type");
pdpSubGroup.setDesiredInstanceCount(123);
pdpSubGroup.setSupportedPolicyTypes(new ArrayList<>());
- pdpSubGroup.getSupportedPolicyTypes().add(new ToscaPolicyTypeIdentifier(
- POLICY_TYPE_NAME, POLICY_TYPE_VERSION));
+ pdpSubGroup.getSupportedPolicyTypes()
+ .add(new ToscaPolicyTypeIdentifier(POLICY_TYPE_NAME, POLICY_TYPE_VERSION));
pdpGroup.getPdpSubgroups().add(pdpSubGroup);
Pdp pdp = new Pdp();
// Create Policy Type
assertThatCode(() -> {
- String policyTypeString = ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE);
- ToscaServiceTemplate policyTypeServiceTemplate =
- standardCoder.decode(policyTypeString, ToscaServiceTemplate.class);
+ ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder
+ .decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class);
policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
}).doesNotThrowAnyException();
String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
LegacyGuardPolicyInput policyToCreate =
standardCoder.decode(policyString, LegacyGuardPolicyInput.class);
- Map<String, LegacyGuardPolicyOutput> policyCreated = guardPolicyProvider
- .createGuardPolicy(policyToCreate);
+ Map<String, LegacyGuardPolicyOutput> policyCreated =
+ guardPolicyProvider.createGuardPolicy(policyToCreate);
assertFalse(policyCreated.isEmpty());
}).doesNotThrowAnyException();
// Test fetchDeployedPolicies (deployedPolicyMap.isEmpty())==true
- assertThatThrownBy(
- () -> {
- guardPolicyProvider.fetchDeployedGuardPolicies(POLICY_NAME);
- }).hasMessage("could not find policy with ID " + POLICY_NAME + " and type "
- + POLICY_TYPE_ID + " deployed in any pdp group");
-
+ assertThatThrownBy(() -> {
+ guardPolicyProvider.fetchDeployedGuardPolicies(POLICY_NAME);
+ }).hasMessage("could not find policy with ID " + POLICY_NAME + " and type " + POLICY_TYPE_ID
+ + " deployed in any pdp group");
// Update pdpSubGroup
pdpSubGroup.setPolicies(new ArrayList<>());
- pdpSubGroup.getPolicies().add(
- new ToscaPolicyIdentifier(POLICY_NAME, POLICY_VERSION + LEGACY_MINOR_PATCH_SUFFIX));
- assertEquals(1, databaseProvider.createPdpGroups(groupList).get(0).getPdpSubgroups().get(0)
- .getPolicies().size());
+ pdpSubGroup.getPolicies()
+ .add(new ToscaPolicyIdentifier(POLICY_NAME, POLICY_VERSION + LEGACY_MINOR_PATCH_SUFFIX));
+ assertEquals(1,
+ databaseProvider.createPdpGroups(groupList).get(0).getPdpSubgroups().get(0).getPolicies().size());
// Test fetchDeployedPolicies
- assertThatCode(
- () -> {
- guardPolicyProvider.fetchDeployedGuardPolicies(POLICY_NAME);
- }).doesNotThrowAnyException();
+ assertThatCode(() -> {
+ guardPolicyProvider.fetchDeployedGuardPolicies(POLICY_NAME);
+ }).doesNotThrowAnyException();
// Test validateDeleteEligibility exception path(!pdpGroups.isEmpty())
- assertThatThrownBy(
- () -> {
- guardPolicyProvider.deleteGuardPolicy(
- POLICY_NAME, POLICY_VERSION);
- }).hasMessageContaining("policy with ID " + POLICY_NAME + ":" + POLICY_VERSION
+ assertThatThrownBy(() -> {
+ guardPolicyProvider.deleteGuardPolicy(POLICY_NAME, POLICY_VERSION);
+ }).hasMessageContaining("policy with ID " + POLICY_NAME + ":" + POLICY_VERSION
+ " cannot be deleted as it is deployed in pdp groups");
- }
- catch (Exception exc) {
+ } catch (Exception exc) {
fail("Test should not throw an exception");
}
}
@Test
- public void testCreateGuardPolicy() {
-
+ public void testCreateGuardPolicy() throws Exception {
assertThatThrownBy(() -> {
String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
LegacyGuardPolicyInput policyToCreate = standardCoder.decode(policyString, LegacyGuardPolicyInput.class);
guardPolicyProvider.createGuardPolicy(policyToCreate);
- }).hasMessage("policy type " + POLICY_TYPE_ID + " for policy " + POLICY_ID + " does not exist");
+ }).hasMessageContaining(
+ "no policy types are defined on the service template for the policies in the topology template");
+
+ 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());
- assertThatCode(() -> {
- String policyTypeString = ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE);
- ToscaServiceTemplate policyTypeServiceTemplate =
- standardCoder.decode(policyTypeString, ToscaServiceTemplate.class);
- policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
+ 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");
- 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();
+ 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
pdpSubGroup.setPdpType("type");
pdpSubGroup.setDesiredInstanceCount(123);
pdpSubGroup.setSupportedPolicyTypes(new ArrayList<>());
- pdpSubGroup.getSupportedPolicyTypes().add(new ToscaPolicyTypeIdentifier(
- policyTypeId, policyTypeVersion));
+ pdpSubGroup.getSupportedPolicyTypes().add(new ToscaPolicyTypeIdentifier(policyTypeId, policyTypeVersion));
pdpGroup.getPdpSubgroups().add(pdpSubGroup);
Pdp pdp = new Pdp();
// Create Policy Type
assertThatCode(() -> {
- String policyTypeString = ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE);
- ToscaServiceTemplate policyTypeServiceTemplate =
- standardCoder.decode(policyTypeString, ToscaServiceTemplate.class);
+ ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder
+ .decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class);
policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
}).doesNotThrowAnyException();
// Create Policy
assertThatCode(() -> {
String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
- LegacyGuardPolicyInput policyToCreate = standardCoder.decode(policyString,
- LegacyGuardPolicyInput.class);
+ LegacyGuardPolicyInput policyToCreate =
+ standardCoder.decode(policyString, LegacyGuardPolicyInput.class);
Map<String, LegacyGuardPolicyOutput> createdPolicy =
guardPolicyProvider.createGuardPolicy(policyToCreate);
assertNotNull(createdPolicy);
// Update pdpSubGroup
pdpSubGroup.setPolicies(new ArrayList<>());
pdpSubGroup.getPolicies().add(new ToscaPolicyIdentifier(policyId, policyVersion + legacyMinorPatchSuffix));
- assertEquals(1, databaseProvider.createPdpGroups(groupList).get(0).getPdpSubgroups().get(0)
- .getPolicies().size());
+ assertEquals(1,
+ databaseProvider.createPdpGroups(groupList).get(0).getPdpSubgroups().get(0).getPolicies().size());
assertThatThrownBy(() -> {
- guardPolicyProvider
- .deleteGuardPolicy("guard.frequency.scaleout", "1");
+ guardPolicyProvider.deleteGuardPolicy("guard.frequency.scaleout", "1");
}).hasMessageContaining("cannot be deleted as it is deployed in pdp groups");
- }
- catch (Exception exc) {
+ } catch (Exception exc) {
fail("Test should not throw an exception");
}
}
}).hasMessage("legacy policy version is not an integer");
assertThatCode(() -> {
- String policyTypeString = ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE);
- ToscaServiceTemplate policyTypeServiceTemplate =
- standardCoder.decode(policyTypeString, ToscaServiceTemplate.class);
+ ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder
+ .decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class);
policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
assertNotNull(createdPolicy);
assertFalse(createdPolicy.isEmpty());
- Map<String, LegacyGuardPolicyOutput> deletedPolicy = guardPolicyProvider
- .deleteGuardPolicy("guard.frequency.scaleout", "1");
+ Map<String, LegacyGuardPolicyOutput> deletedPolicy =
+ guardPolicyProvider.deleteGuardPolicy("guard.frequency.scaleout", "1");
assertNotNull(deletedPolicy);
assertFalse(deletedPolicy.isEmpty());
assertTrue(deletedPolicy.containsKey("guard.frequency.scaleout"));