2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.models.pdp.concepts;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertNull;
27 import static org.junit.Assert.assertSame;
28 import static org.junit.Assert.assertTrue;
30 import java.util.Arrays;
31 import java.util.Collections;
32 import java.util.Iterator;
33 import java.util.List;
35 import org.junit.Test;
36 import org.onap.policy.common.parameters.ValidationResult;
37 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
39 public class PdpGroupsTest {
42 public void testValidatePapRest_toMapList() {
43 PdpGroup group1 = new PdpGroup();
44 group1.setName("group-1");
46 PdpSubGroup subgrp = new PdpSubGroup();
47 subgrp.setDesiredInstanceCount(1);
48 subgrp.setPdpType("pdp-type");
49 subgrp.setSupportedPolicyTypes(Arrays.asList(new ToscaPolicyTypeIdentifier("policy-type", "9.8.7")));
50 subgrp.setPolicies(Collections.emptyList());
52 group1.setPdpSubgroups(Arrays.asList(subgrp));
54 PdpGroup group2 = new PdpGroup();
55 group2.setName("group-2");
56 group2.setPdpSubgroups(Arrays.asList(subgrp));
58 PdpGroups groups = new PdpGroups();
59 groups.setGroups(Arrays.asList(group1, group2));
62 ValidationResult result = groups.validatePapRest();
63 assertNotNull(result);
64 assertTrue(result.isValid());
65 assertNull(result.getResult());
68 List<Map<String, PdpGroup>> lst = groups.toMapList();
69 assertEquals(1, lst.size());
71 Map<String, PdpGroup> map = lst.get(0);
72 assertEquals(2, map.size());
74 Iterator<PdpGroup> iter = map.values().iterator();
75 assertSame(group1, iter.next());
76 assertSame(group2, iter.next());
79 groups = new PdpGroups();
80 groups.setGroups(null);
81 assertInvalid(groups);
84 groups = new PdpGroups();
85 groups.setGroups(Arrays.asList(group1, null));
86 assertInvalid(groups);
89 PdpGroup groupX = new PdpGroup(group1);
91 groups.setGroups(Arrays.asList(group1, groupX));
92 assertInvalid(groups);
95 groups = new PdpGroups();
96 groups.setGroups(Arrays.asList(group1, group2, group1));
97 assertInvalid(groups);
100 private void assertInvalid(PdpGroups groups) {
101 ValidationResult result = groups.validatePapRest();
102 assertNotNull(result);
103 assertFalse(result.isValid());
104 assertNotNull(result.getResult());