aboutsummaryrefslogtreecommitdiffstats
path: root/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTypeFilterTest.java
blob: 6304091af18ba23d913974449dcdbbbcfc5f8147 (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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2019-2020 Nordix Foundation.
 *  Modifications Copyright (C) 2019 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.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.models.tosca.authorative.concepts;

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.assertTrue;

import com.google.gson.GsonBuilder;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.junit.BeforeClass;
import org.junit.Test;
import org.onap.policy.common.utils.coder.CoderException;
import org.onap.policy.common.utils.coder.StandardCoder;
import org.onap.policy.common.utils.resources.ResourceUtils;
import org.onap.policy.models.base.PfKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.yaml.snakeyaml.Yaml;

/**
 * Test of the {@link ToscaPolicyTypeFilter} class.
 *
 * @author Liam Fallon (liam.fallon@est.tech)
 */
public class ToscaPolicyTypeFilterTest {
    private static final String VERSION_100 = "1.0.0";

    private static final String VERSION_000 = "0.0.0";

    // Logger for this class
    private static final Logger LOGGER = LoggerFactory.getLogger(ToscaPolicyTypeFilterTest.class);

    // @formatter:off
    private static final String[] policyTypeResourceNames = {
        "policytypes/onap.policies.controlloop.Operational.yaml",
        "policytypes/onap.policies.optimization.resource.DistancePolicy.yaml",
        "policytypes/onap.policies.optimization.resource.VnfPolicy.yaml",
        "policytypes/onap.policies.optimization.resource.PciPolicy.yaml",
        "policytypes/onap.policies.optimization.resource.OptimizationPolicy.yaml",
        "policytypes/onap.policies.controlloop.guard.common.Blacklist.yaml",
        "policytypes/onap.policies.monitoring.dcaegen2.collectors.datafile.datafile-app-server.yaml",
        "policytypes/onap.policies.optimization.resource.HpaPolicy.yaml",
        "policytypes/onap.policies.optimization.resource.Vim_fit.yaml",
        "policytypes/onap.policies.optimization.service.SubscriberPolicy.yaml",
        "policytypes/onap.policies.optimization.resource.AffinityPolicy.yaml",
        "policytypes/onap.policies.optimization.service.QueryPolicy.yaml",
        "policytypes/onap.policies.controlloop.guard.common.MinMax.yaml",
        "policytypes/onap.policies.controlloop.guard.common.FrequencyLimiter.yaml",
        "policytypes/onap.policies.controlloop.guard.coordination.FirstBlocksSecond.yaml",
        "policytypes/onap.policies.Optimization.yaml",
        "policytypes/onap.policies.monitoring.cdap.tca.hi.lo.app.yaml"
    };
    // @formatter:on

    private static List<ToscaPolicyType> typeList = new ArrayList<>();

    /**
     * Set up a Tosca Policy type list for filtering.
     *
     * @throws CoderException on JSON decoding errors
     */
    @BeforeClass
    public static void setupTypeList() throws CoderException {
        for (String policyTypeResourceName : policyTypeResourceNames) {
            String policyTypeString = ResourceUtils.getResourceAsString(policyTypeResourceName);
            Object yamlObject = new Yaml().load(policyTypeString);
            String yamlAsJsonString = new GsonBuilder().setPrettyPrinting().create().toJson(yamlObject);

            ToscaServiceTemplate serviceTemplate =
                    new StandardCoder().decode(yamlAsJsonString, ToscaServiceTemplate.class);
            assertNotNull(serviceTemplate);

            addPolicyTypes(serviceTemplate.getPolicyTypes());
        }

        for (ToscaPolicyType type : typeList) {
            LOGGER.info("using policy type-" + type.getName() + ":" + type.getVersion());
        }
    }

    private static void addPolicyTypes(Map<String, ToscaPolicyType> foundPolicyTypeMap) {
        for (Entry<String, ToscaPolicyType> policyTypeEntry : foundPolicyTypeMap.entrySet()) {
            ToscaPolicyType policyType = policyTypeEntry.getValue();
            if (policyType.getName() == null) {
                policyType.setName(policyTypeEntry.getKey());
            }
            if (policyType.getVersion() == null) {
                policyType.setVersion(PfKey.NULL_KEY_VERSION);
            }
            if (!typeList.contains(policyType)) {
                typeList.add(policyType);
            }
        }
    }

    @Test
    public void testNullList() {
        ToscaPolicyTypeFilter filter = ToscaPolicyTypeFilter.builder().build();

        assertThatThrownBy(() -> {
            filter.filter(null);
        }).hasMessageMatching("originalList is marked .*on.*ull but is null");
    }

    @Test
    public void testFilterNothing() {
        ToscaPolicyTypeFilter filter = ToscaPolicyTypeFilter.builder().build();

        List<ToscaPolicyType> filteredList = filter.filter(typeList);
        assertTrue(filteredList.containsAll(typeList));
    }

    @Test
    public void testFilterLatestVersion() {
        ToscaPolicyTypeFilter filter =
                ToscaPolicyTypeFilter.builder().version(ToscaPolicyTypeFilter.LATEST_VERSION).build();

        List<ToscaPolicyType> filteredList = filter.filter(typeList);
        assertEquals(20, filteredList.size());
        assertEquals(VERSION_100, filteredList.get(0).getVersion());
        assertEquals(VERSION_100, filteredList.get(11).getVersion());

        typeList.get(12).setVersion("2.0.0");
        filteredList = filter.filter(typeList);
        assertEquals(20, filteredList.size());
        //
        // This seems to change around as to where this policy type
        // got changed - perhaps we change this test to find a specific name
        // to test for vs an index which never remains consistent?
        //
        assertEquals("2.0.0", filteredList.get(18).getVersion());
        //
        // And now this index changes again??
        //
        assertEquals(VERSION_100, filteredList.get(17).getVersion());

        typeList.get(12).setVersion(VERSION_100);
        filteredList = filter.filter(typeList);
        assertEquals(20, filteredList.size());
        assertEquals(VERSION_100, filteredList.get(0).getVersion());
        assertEquals(VERSION_100, filteredList.get(18).getVersion());
    }

    @Test
    public void testFilterNameVersion() {
        ToscaPolicyTypeFilter filter = ToscaPolicyTypeFilter.builder().name("onap.policies.Monitoring").build();
        List<ToscaPolicyType> filteredList = filter.filter(typeList);
        assertEquals(1, filteredList.size());

        filter = ToscaPolicyTypeFilter.builder().name("onap.policies.monitoring.cdap.tca.hi.lo.app").build();
        filteredList = filter.filter(typeList);
        assertEquals(1, filteredList.size());

        filter = ToscaPolicyTypeFilter.builder().name("onap.policies.optimization.LpaPolicy").build();
        filteredList = filter.filter(typeList);
        assertEquals(0, filteredList.size());

        filter = ToscaPolicyTypeFilter.builder().version(VERSION_100).build();
        filteredList = filter.filter(typeList);
        assertEquals(20, filteredList.size());

        filter = ToscaPolicyTypeFilter.builder().name("onap.policies.optimization.Vim_fit").version(VERSION_000)
                .build();
        filteredList = filter.filter(typeList);
        assertEquals(0, filteredList.size());

        filter = ToscaPolicyTypeFilter.builder().name("onap.policies.optimization.Vim_fit").version("0.0.1").build();
        filteredList = filter.filter(typeList);
        assertEquals(0, filteredList.size());
    }
}