aboutsummaryrefslogtreecommitdiffstats
path: root/policy-utils/src/test/java/org/onap/policy/drools/policies/DomainMakerTest.java
blob: 2c883304fc157a789b9fbcbc8afa657600ed6986 (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
/*
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
 *  Modifications Copyright (C) 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.
 * 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.drools.policies;

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.assertNotNull;
import static org.junit.Assert.assertTrue;

import com.worldturner.medeia.api.ValidationFailedException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import org.junit.Before;
import org.junit.Test;
import org.onap.policy.common.utils.coder.CoderException;
import org.onap.policy.common.utils.coder.StandardCoder;
import org.onap.policy.drools.models.domains.a.DomainAPolicy;
import org.onap.policy.drools.models.domains.a.Metadata;
import org.onap.policy.drools.models.domains.a.Nested;
import org.onap.policy.drools.models.domains.a.Properties;
import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;

public class DomainMakerTest {

    private DomainMaker domainMaker;

    @Before
    public void setUp() throws Exception {
        domainMaker = new DomainMaker();
    }

    @Test
    public void testIsConformantString() throws IOException {
        ToscaConceptIdentifier policyTypeId =
            new ToscaConceptIdentifier("policy.type.A", "1.0.0");
        String rawJsonPolicyType =
            getJsonFromFile("src/test/resources/policyA.json");

        assertTrue(domainMaker.isConformant(policyTypeId, rawJsonPolicyType));

        policyTypeId.setVersion("2.0.0");
        assertFalse(domainMaker.isConformant(policyTypeId, rawJsonPolicyType));
    }

    @Test
    public void testIsConformantToscaPolicy() throws IOException, CoderException {
        ToscaPolicy policy = getToscaPolicy("src/test/resources/policyA.json");
        assertTrue(domainMaker.isConformant(policy));

        policy.setType("policy.type.Z");
        assertFalse(domainMaker.isConformant(policy));
    }

    @Test
    public void testIsDomainConformant() {
        ToscaConceptIdentifier policyTypeId =
                new ToscaConceptIdentifier("policy.type.A", "1.0.0");

        DomainAPolicy domainAPolicy = createDomainPolicy();

        assertTrue(domainMaker.isDomainConformant(policyTypeId, domainAPolicy));

        // integer exceeding max. value
        domainAPolicy.getProperties().getNested().setNested3(999);
        assertFalse(domainMaker.isDomainConformant(policyTypeId, domainAPolicy));
        domainAPolicy.getProperties().getNested().setNested3(33); // restore good valude

        // not registered schema for policy type
        policyTypeId.setVersion("2.0.0");
        assertFalse(domainMaker.isDomainConformant(policyTypeId, domainAPolicy));
    }


    @Test
    public void testConformance() throws IOException, CoderException {
        ToscaPolicy policy1 = getToscaPolicy("src/test/resources/policyA.json");
        assertTrue(domainMaker.conformance(policy1));

        policy1.getProperties().remove("nested");
        assertThatThrownBy(() -> domainMaker.conformance(policy1))
                .isInstanceOf(ValidationFailedException.class)
                .hasMessageContaining("Required property nested is missing from object");

        DomainAPolicy domainAPolicy = createDomainPolicy();
        assertTrue(domainMaker.conformance(policy1.getTypeIdentifier(), domainAPolicy));
        assertDomainPolicy(domainAPolicy);

        domainAPolicy.getProperties().getNested().setNested1("");
        ToscaConceptIdentifier ident1 = policy1.getTypeIdentifier();
        assertThatThrownBy(() -> domainMaker.conformance(ident1, domainAPolicy))
                .isInstanceOf(ValidationFailedException.class)
                .hasMessageContaining("Pattern ^(.+)$ is not contained in text");
    }

    @Test
    public void testRegisterValidator() throws IOException, CoderException {
        ToscaConceptIdentifier policyTypeId =
                new ToscaConceptIdentifier("policy.type.external", "9.9.9");

        assertTrue(domainMaker.registerValidator(policyTypeId,
            getJsonFromFile("src/test/resources/policy.type.external-9.9.9.schema.json")));

        ToscaPolicy policy = getToscaPolicy("src/test/resources/policyA.json");
        policy.setType("policy.type.external");
        policy.setTypeVersion("9.9.9");
        assertTrue(domainMaker.isConformant(policy));

        policy.setTypeVersion("1.0.0");
        assertFalse(domainMaker.isConformant(policy));
    }

    @Test
    public void testConvertToDomainPolicy() throws IOException, CoderException {
        DomainAPolicy domainAPolicy =
                domainMaker.convertTo(getToscaPolicy("src/test/resources/policyA.json"), DomainAPolicy.class);
        assertDomainPolicy(domainAPolicy);

        assertNotNull(domainMaker.convertTo(getToscaPolicy("src/test/resources/policyA-no-policy-type.json"),
                DomainAPolicy.class));
    }

    @Test
    public void testConvertToSchema() {
        ToscaPolicyType type = new ToscaPolicyType();
        assertThatThrownBy(() -> domainMaker
            .convertToSchema(type))
            .isInstanceOf(UnsupportedOperationException.class);
    }

    @Test
    public void testIsRegistered() {
        ToscaConceptIdentifier policyTypeId1 =
                new ToscaConceptIdentifier("policy.type.A", "1.0.0");
        assertTrue(domainMaker.isRegistered(policyTypeId1));

        ToscaConceptIdentifier policyTypeId2 =
                new ToscaConceptIdentifier("policy.type.external", "7.7.9");
        assertFalse(domainMaker.isRegistered(policyTypeId2));

    }

    private String getJsonFromFile(String filePath) throws IOException {
        return new String(Files.readAllBytes(Paths.get(filePath)));
    }

    private ToscaPolicy getToscaPolicy(String filePath) throws CoderException, IOException {
        String policyJson = getJsonFromFile(filePath);
        return new StandardCoder().decode(policyJson, ToscaPolicy.class);
    }

    private DomainAPolicy createDomainPolicy() {
        return DomainAPolicy.builder().metadata(Metadata.builder().policyId("A").build())
                       .name("A")
                       .version("1.0.0")
                       .type("policy.type.A")
                       .typeVersion("1.0.0")
                       .properties(Properties.builder()
                           .nested(Nested.builder().nested1("nested1").nested2(true).nested3(50).build())
                           .build()).build();
    }

    private void assertDomainPolicy(DomainAPolicy domainAPolicy) {
        assertEquals("A", domainAPolicy.getName());
        assertEquals("1.0.0", domainAPolicy.getVersion());
        assertEquals("1.0.0", domainAPolicy.getTypeVersion());
        assertEquals("policy.type.A", domainAPolicy.getType());
        assertEquals("A", domainAPolicy.getMetadata().getPolicyId());
        assertEquals("nested1", domainAPolicy.getProperties().getNested().getNested1());
        assertEquals(true, domainAPolicy.getProperties().getNested().isNested2());
        assertEquals(50, domainAPolicy.getProperties().getNested().getNested3());
    }
}