Fix Sonar Issues models-tosca-simple
[policy/models.git] / models-tosca / src / test / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaPolicyTypesTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019-2021 Nordix Foundation.
4  *  Modifications Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.models.tosca.simple.concepts;
23
24 import static org.assertj.core.api.Assertions.assertThat;
25 import static org.assertj.core.api.Assertions.assertThatThrownBy;
26 import static org.junit.Assert.assertFalse;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.assertTrue;
29
30 import java.util.ArrayList;
31 import java.util.LinkedHashMap;
32 import java.util.List;
33 import java.util.Map;
34 import java.util.TreeMap;
35 import org.junit.Test;
36 import org.onap.policy.common.parameters.BeanValidationResult;
37 import org.onap.policy.models.base.PfConceptKey;
38 import org.onap.policy.models.base.Validated;
39 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
40
41 public class JpaToscaPolicyTypesTest {
42
43     private static final String KEY_IS_NULL = "key is marked .*on.*ull but is null";
44
45     @Test
46     public void testPolicyTypesNull() {
47         assertNotNull(new JpaToscaPolicyTypes());
48         assertNotNull(new JpaToscaPolicyTypes(new PfConceptKey()));
49         assertNotNull(new JpaToscaPolicyTypes(new PfConceptKey(), new TreeMap<PfConceptKey, JpaToscaPolicyType>()));
50         assertNotNull(new JpaToscaPolicyTypes(new JpaToscaPolicyTypes()));
51
52         assertThatThrownBy(() -> new JpaToscaPolicyTypes((PfConceptKey) null)).hasMessageMatching(KEY_IS_NULL);
53
54         assertThatThrownBy(() -> new JpaToscaPolicyTypes((JpaToscaPolicyTypes) null))
55                 .hasMessageMatching("copyConcept is marked .*on.*ull but is null");
56
57         assertThatThrownBy(() -> new JpaToscaPolicyTypes(null, null)).hasMessageMatching(KEY_IS_NULL);
58
59         assertThatThrownBy(() -> new JpaToscaPolicyTypes(new PfConceptKey(), null))
60                 .hasMessageMatching("conceptMap is marked .*on.*ull but is null");
61
62         assertThatThrownBy(() -> new JpaToscaPolicyTypes(null, new TreeMap<PfConceptKey, JpaToscaPolicyType>()))
63                 .hasMessageMatching(KEY_IS_NULL);
64     }
65
66     @Test
67     public void testPolicyTypes() {
68         List<Map<String, ToscaPolicyType>> ptMapList = new ArrayList<>();
69         ptMapList.add(new LinkedHashMap<>());
70
71         ToscaPolicyType pt0 = new ToscaPolicyType();
72         pt0.setName("pt0");
73         pt0.setVersion("0.0.1");
74         pt0.setDescription("pt0 description");
75
76         ptMapList.get(0).put("pt0", pt0);
77         assertNotNull(new JpaToscaPolicyTypes(ptMapList));
78         assertTrue(new JpaToscaPolicyTypes(ptMapList).validate("").isValid());
79         assertThatThrownBy(() -> new JpaToscaPolicyTypes(ptMapList).validate(null))
80                 .hasMessageMatching("fieldName is marked .*on.*ull but is null");
81
82         pt0.setDerivedFrom(null);
83         assertTrue(new JpaToscaPolicyTypes(ptMapList).validate("").isValid());
84
85         pt0.setDerivedFrom("tosca.PolicyTypes.Root");
86         assertTrue(new JpaToscaPolicyTypes(ptMapList).validate("").isValid());
87
88         pt0.setDerivedFrom("some.other.Thing");
89         BeanValidationResult result = new JpaToscaPolicyTypes(ptMapList).validate("");
90         assertFalse(result.isValid());
91         assertThat(result.getResult()).contains("parent").contains("some.other.Thing:0.0.0")
92                         .contains(Validated.NOT_FOUND);
93
94         pt0.setDerivedFrom(null);
95         assertTrue(new JpaToscaPolicyTypes(ptMapList).validate("").isValid());
96
97         ToscaPolicyType pt1 = new ToscaPolicyType();
98         pt1.setName("pt1");
99         pt1.setVersion("0.0.1");
100         pt1.setDescription("pt1 description");
101
102         ptMapList.get(0).put("pt1", pt1);
103         assertTrue(new JpaToscaPolicyTypes(ptMapList).validate("").isValid());
104
105         pt1.setDerivedFrom("pt0");
106         assertTrue(new JpaToscaPolicyTypes(ptMapList).validate("").isValid());
107
108         pt1.setDerivedFrom("pt2");
109         result = new JpaToscaPolicyTypes(ptMapList).validate("");
110         assertFalse(result.isValid());
111         assertThat(result.getResult()).contains("parent").contains("pt2:0.0.0").contains(Validated.NOT_FOUND);
112
113         pt1.setDerivedFrom("pt0");
114         assertTrue(new JpaToscaPolicyTypes(ptMapList).validate("").isValid());
115     }
116 }