* ============LICENSE_START=======================================================
* ONAP Policy Models
* ================================================================================
- * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
- * Modifications Copyright (C) 2019 Nordix Foundation.
+ * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
+ * Modifications Copyright (C) 2019-2021 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 static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.util.ArrayList;
import java.util.Arrays;
+import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import org.junit.Test;
+import org.onap.policy.common.parameters.ValidationResult;
import org.onap.policy.models.pdp.enums.PdpState;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
/**
- * Test the copy constructor, as {@link ModelsTest} tests the other methods.
+ * Test methods not tested by {@link ModelsTest}.
*/
public class PdpGroupTest {
+ private static final String VERSION = "1.2.3";
+ private static final String NAME = "my-name";
+ private static final String PDP_TYPE1 = "type-1";
+ private static final String PDP_TYPE2 = "type-2";
+ private static final String PDP_TYPE3 = "type-3";
@Test
public void testCopyConstructor() {
PdpGroup orig = new PdpGroup();
// verify with null values
- assertEquals("PdpGroup(name=null, version=null, description=null, pdpGroupState=null, "
- + "properties=null, pdpSubgroups=[])", new PdpGroup(orig).toString());
+ assertEquals("PdpGroup(name=null, description=null, pdpGroupState=null, " + "properties=null, pdpSubgroups=[])",
+ new PdpGroup(orig).toString());
// verify with all values
orig.setDescription("my-descript");
- orig.setName("my-name");
- orig.setVersion("1.2.3");
+ orig.setName(NAME);
+ orig.setVersion(VERSION);
orig.setDescription("my-description");
orig.setPdpGroupState(PdpState.SAFE);
props.put("key-B", "value-B");
orig.setProperties(props);
- assertEquals("PdpGroup(name=my-name, version=1.2.3, description=my-description, "
- + "pdpGroupState=SAFE, properties={key-A=value-A, key-B=value-B}, "
- + "pdpSubgroups=[PdpSubGroup(pdpType=null, supportedPolicyTypes=[], policies=[], "
- + "currentInstanceCount=10, desiredInstanceCount=0, properties=null, pdpInstances=[]), "
- + "PdpSubGroup(pdpType=null, supportedPolicyTypes=[], policies=[], currentInstanceCount=11, "
- + "desiredInstanceCount=0, properties=null, pdpInstances=[])])", new PdpGroup(orig).toString());
+ assertEquals("PdpGroup(name=my-name, description=my-description, "
+ + "pdpGroupState=SAFE, properties={key-A=value-A, key-B=value-B}, "
+ + "pdpSubgroups=[PdpSubGroup(pdpType=null, supportedPolicyTypes=[], policies=[], "
+ + "currentInstanceCount=10, desiredInstanceCount=0, properties=null, pdpInstances=[]), "
+ + "PdpSubGroup(pdpType=null, supportedPolicyTypes=[], policies=[], currentInstanceCount=11, "
+ + "desiredInstanceCount=0, properties=null, pdpInstances=[])])", new PdpGroup(orig).toString());
}
@Test
assertEquals(hash, group.hashCode());
group.setDescription("B");
- assertTrue(hash != group.hashCode());
+ assertNotEquals(hash, group.hashCode());
}
@Test
public void testCompareTo() {
PdpGroup pdpGroup0 = new PdpGroup();
pdpGroup0.setName("Name0");
- pdpGroup0.setVersion("1.2.3");
+ pdpGroup0.setVersion(VERSION);
PdpGroup pdpGroup1 = new PdpGroup();
pdpGroup1.setName("Name0");
- pdpGroup1.setVersion("1.2.3");
+ pdpGroup1.setVersion(VERSION);
assertEquals(0, pdpGroup0.compareTo(pdpGroup1));
assertEquals(1, mapList.size());
assertEquals(1, mapList.get(0).size());
}
+
+ @Test
+ public void testValidatePapRest_GroupUpdateFlow() {
+ PdpGroup group = new PdpGroup();
+ group.setName(NAME);
+ // with supported policy type and policies
+ PdpSubGroup subgroup1 = new PdpSubGroup();
+ subgroup1.setDesiredInstanceCount(1);
+ subgroup1.setPdpType(PDP_TYPE1);
+ subgroup1.setSupportedPolicyTypes(Arrays.asList(new ToscaConceptIdentifier("a-type-name", "3.2.1")));
+ subgroup1.setPolicies(Collections.emptyList());
+ group.setPdpSubgroups(Arrays.asList(subgroup1));
+
+ ValidationResult result = group.validatePapRest(true);
+ assertNotNull(result);
+ assertTrue(result.isValid());
+ assertNull(result.getResult());
+
+ // without supported policy type and policies
+ PdpSubGroup subgroup2 = new PdpSubGroup();
+ subgroup2.setDesiredInstanceCount(1);
+ subgroup2.setPdpType(PDP_TYPE1);
+ group.setPdpSubgroups(Arrays.asList(subgroup2));
+
+ // valid
+ result = group.validatePapRest(true);
+ assertNotNull(result);
+ assertTrue(result.isValid());
+ assertNull(result.getResult());
+
+ // invalid
+ result = group.validatePapRest(false);
+ assertNotNull(result);
+ assertFalse(result.isValid());
+ assertNotNull(result.getResult());
+ }
+
+ @Test
+ public void testValidatePapRest() {
+ PdpGroup group = new PdpGroup();
+ group.setName(NAME);
+
+ PdpSubGroup subgroup1 = new PdpSubGroup();
+ subgroup1.setDesiredInstanceCount(1);
+ subgroup1.setPdpType(PDP_TYPE1);
+ subgroup1.setSupportedPolicyTypes(Arrays.asList(new ToscaConceptIdentifier("a-type-name", "3.2.1")));
+ subgroup1.setPolicies(Collections.emptyList());
+
+ PdpSubGroup subgroup2 = new PdpSubGroup(subgroup1);
+ subgroup2.setPdpType(PDP_TYPE2);
+
+ PdpSubGroup subgroup3 = new PdpSubGroup(subgroup1);
+ subgroup3.setPdpType(PDP_TYPE3);
+
+ group.setPdpSubgroups(Arrays.asList(subgroup1, subgroup2, subgroup3));
+
+ // valid
+ ValidationResult result = group.validatePapRest(false);
+ assertNotNull(result);
+ assertTrue(result.isValid());
+ assertNull(result.getResult());
+
+ // null name
+ PdpGroup group2 = new PdpGroup(group);
+ group2.setName(null);
+ assertInvalid(group2);
+
+ // null subgroup list
+ group2 = new PdpGroup(group);
+ group2.setPdpSubgroups(null);
+ assertInvalid(group2);
+
+ // empty subgroup list
+ group2 = new PdpGroup(group);
+ group2.setPdpSubgroups(Collections.emptyList());
+ assertInvalid(group2);
+
+ // null subgroup
+ group2 = new PdpGroup(group);
+ group2.setPdpSubgroups(Arrays.asList(subgroup1, null));
+ assertInvalid(group2);
+
+ // invalid subgroup
+ group2 = new PdpGroup(group);
+ PdpSubGroup subgroupX = new PdpSubGroup(subgroup1);
+ subgroupX.setPdpType(null);
+ group2.setPdpSubgroups(Arrays.asList(subgroupX));
+ assertInvalid(group2);
+
+ // duplicate PDP type
+ group2 = new PdpGroup(group);
+ group2.setPdpSubgroups(Arrays.asList(subgroup1, subgroup2, subgroup1));
+ assertInvalid(group2);
+ }
+
+ private void assertInvalid(PdpGroup group) {
+ ValidationResult result = group.validatePapRest(false);
+ assertNotNull(result);
+ assertFalse(result.isValid());
+ assertNotNull(result.getResult());
+ }
}