aboutsummaryrefslogtreecommitdiffstats
path: root/src/test/java/org/onap/policy/clamp/policy/pdpgroup/PdpGroupAnalyzerTest.java
blob: e6717418a013d14dcd6bc38b51a68c83eb1400db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*-
 * ============LICENSE_START=======================================================
 * ONAP POLICY-CLAMP
 * ================================================================================
 * Copyright (C) 2021 AT&T Intellectual Property. All rights
 *                             reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END============================================
 * ===================================================================
 *
 */

package org.onap.policy.clamp.policy.pdpgroup;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.Arrays;
import org.junit.BeforeClass;
import org.junit.Test;
import org.onap.policy.clamp.loop.template.PolicyModel;
import org.onap.policy.models.pdp.concepts.PdpGroup;
import org.onap.policy.models.pdp.concepts.PdpGroups;
import org.onap.policy.models.pdp.concepts.PdpSubGroup;
import org.onap.policy.models.pdp.enums.PdpState;
import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;

/**
 * This test class validates the PdpGroupAnalyzer class.
 */
public class PdpGroupAnalyzerTest {

    private static PdpGroups pdpGroups;

    /**
     * This method preloads the pdpGroups for the tests.
     */
    @BeforeClass
    public static void setupPdpGroup() {
        // Create Pdp Groups
        // Those that do not work first
        PdpSubGroup pdpSubgroupBad = new PdpSubGroup();
        pdpSubgroupBad.setPdpType("subGroupBad");
        pdpSubgroupBad.setSupportedPolicyTypes(Arrays.asList(new ToscaConceptIdentifier("org.onap.test", "2.0.0"),
                new ToscaConceptIdentifier("org.onap.test.*", "1.0.0"),
                new ToscaConceptIdentifier("org.onip.testos", "1.0.0"),
                new ToscaConceptIdentifier("org.onap.testos3", "2.0.0"),
                new ToscaConceptIdentifier("org.onap.tes", "1.0.0"),
                new ToscaConceptIdentifier("org.onap", "1.0.0")
        ));
        pdpSubgroupBad.setPolicies(Arrays.asList(new ToscaConceptIdentifier("org.onap", "1.0.0")));
        pdpSubgroupBad.setPolicies(Arrays.asList(new ToscaConceptIdentifier("org.onap.testos", "2.0.0")));
        pdpSubgroupBad.setPolicies(Arrays.asList(new ToscaConceptIdentifier("org.onap.testos", "1.0.1")));
        pdpSubgroupBad.setPolicies(Arrays.asList(new ToscaConceptIdentifier("org.onap.testos.new", "1.0.0")));
        pdpSubgroupBad.setPolicies(Arrays.asList(new ToscaConceptIdentifier("org.onap.*", "1.0.0")));

        PdpSubGroup pdpSubgroup1 = new PdpSubGroup();
        pdpSubgroup1.setPdpType("subGroup1");
        pdpSubgroup1.setSupportedPolicyTypes(
                Arrays.asList(new ToscaConceptIdentifier("org.onap.*", "1.0.0")));

        PdpSubGroup pdpSubgroup2 = new PdpSubGroup();
        pdpSubgroup2.setPdpType("subGroup2");
        pdpSubgroup2.setSupportedPolicyTypes(Arrays.asList(new ToscaConceptIdentifier("org.onap.test", "1.0.0")));
        pdpSubgroup2.setPolicies(Arrays.asList(new ToscaConceptIdentifier("org.onap.testos", "1.0.0")));

        PdpSubGroup pdpSubgroup3 = new PdpSubGroup();
        pdpSubgroup3.setPdpType("subGroup3");
        pdpSubgroup3.setSupportedPolicyTypes(Arrays.asList(new ToscaConceptIdentifier("org.onap.test*", "1.0.0")));
        pdpSubgroup3.setPolicies(Arrays.asList(new ToscaConceptIdentifier("org.onap.testos", "2.0.0")));

        // Should match pdpSubgroup1
        PdpGroup pdpGroup1 = new PdpGroup();
        pdpGroup1.setName("pdpGroup1");
        pdpGroup1.setPdpGroupState(PdpState.ACTIVE);
        pdpGroup1.setPdpSubgroups(Arrays.asList(pdpSubgroup1, pdpSubgroupBad));

        // Should match pdpSubgroup1, pdpSubgroup2, pdpSubgroup3
        // Should match also for the policy (pdpSubgroup2)
        PdpGroup pdpGroup2 = new PdpGroup();
        pdpGroup2.setName("pdpGroup2");
        pdpGroup2.setPdpGroupState(PdpState.ACTIVE);
        pdpGroup2.setPdpSubgroups(Arrays.asList(pdpSubgroup1, pdpSubgroup2, pdpSubgroup3, pdpSubgroupBad));

        /// Should not match
        PdpGroup pdpGroup3 = new PdpGroup();
        pdpGroup3.setName("pdpGroup3");
        pdpGroup3.setPdpGroupState(PdpState.ACTIVE);
        pdpGroup3.setPdpSubgroups(Arrays.asList(pdpSubgroupBad));

        // Should not match
        PdpGroup pdpGroup4 = new PdpGroup();
        pdpGroup4.setName("pdpGroup4");
        pdpGroup4.setPdpGroupState(PdpState.PASSIVE);
        pdpGroup4.setPdpSubgroups(Arrays.asList(pdpSubgroup1));

        pdpGroups = new PdpGroups();
        pdpGroups.setGroups(Arrays.asList(pdpGroup1, pdpGroup2, pdpGroup3, pdpGroup4));
    }

    @Test
    public void testUpdatePdpGroupOfPolicyModels() {
        // Create policyModel
        PolicyModel policyModel = new PolicyModel();
        policyModel.setCreatedBy("user");
        policyModel.setPolicyAcronym("TEST");
        policyModel.setPolicyModelTosca("yaml");
        policyModel.setPolicyModelType("org.onap.test");
        policyModel.setUpdatedBy("user");
        policyModel.setVersion("1.0.0");
        PdpGroupsAnalyzer.updatePdpGroupOfPolicyModels(Arrays.asList(policyModel), pdpGroups);

        assertThat(policyModel.getPolicyPdpGroup().toString()).isEqualTo(
                "{\"supportedPdpGroups\":[{\"pdpGroup1\":[\"subGroup1\"]},"
                        + "{\"pdpGroup2\":[\"subGroup1\",\"subGroup2\",\"subGroup3\"]}]}");
    }

    @Test
    public void testGetPdpGroupDeploymentsOfOnePolicy() {
        assertThat(
                PdpGroupsAnalyzer.getPdpGroupDeploymentOfOnePolicy(pdpGroups, "org.onap.testos", "1.0.0").toString())
                .isEqualTo("{\"pdpGroupInfo\":{\"pdpGroup\":\"pdpGroup2\",\"pdpSubGroup\":\"subGroup2\"}}");
    }

    @Test
    public void testGetPdpGroupDeploymentsOfOnePolicyNull() {
        assertThat(
                PdpGroupsAnalyzer.getPdpGroupDeploymentOfOnePolicy(pdpGroups, "org.onap.DoNotExist", "1.0.0"))
                .isNull();
    }
}