Changes for Checkstyle 8.32
[policy/models.git] / models-tosca / src / test / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaPoliciesTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019-2020 Nordix Foundation.
4  *  Modifications Copyright (C) 2019 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.models.base.PfConceptKey;
37 import org.onap.policy.models.base.PfValidationResult;
38 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
39
40 public class JpaToscaPoliciesTest {
41
42     private static final String KEY_IS_NULL = "key is marked .*on.*ull but is null";
43
44     @Test
45     public void testPolicies() {
46         assertNotNull(new JpaToscaPolicies());
47         assertNotNull(new JpaToscaPolicies(new PfConceptKey()));
48         assertNotNull(new JpaToscaPolicies(new PfConceptKey(), new TreeMap<PfConceptKey, JpaToscaPolicy>()));
49         assertNotNull(new JpaToscaPolicies(new JpaToscaPolicies()));
50
51         assertThatThrownBy(() -> new JpaToscaPolicies((PfConceptKey) null)).hasMessageMatching(KEY_IS_NULL);
52
53         assertThatThrownBy(() -> new JpaToscaPolicies((JpaToscaPolicies) null))
54                 .hasMessageMatching("copyConcept is marked .*on.*ull but is null");
55
56         assertThatThrownBy(() -> new JpaToscaPolicies(null, null)).hasMessageMatching(KEY_IS_NULL);
57
58         assertThatThrownBy(() -> new JpaToscaPolicies(new PfConceptKey(), null))
59                 .hasMessageMatching("conceptMap is marked .*on.*ull but is null");
60
61         assertThatThrownBy(() -> new JpaToscaPolicies(null, new TreeMap<PfConceptKey, JpaToscaPolicy>()))
62                 .hasMessageMatching(KEY_IS_NULL);
63
64         List<Map<String, ToscaPolicy>> polMapList = new ArrayList<>();
65         polMapList.add(new LinkedHashMap<>());
66
67         ToscaPolicy pol0 = new ToscaPolicy();
68         pol0.setName("pol0");
69         pol0.setVersion("0.0.1");
70         pol0.setDescription("pol0 description");
71         pol0.setType("pt0");
72         pol0.setTypeVersion("0.0.1");
73
74         polMapList.get(0).put("pol0", pol0);
75         assertNotNull(new JpaToscaPolicies(polMapList));
76         assertTrue(new JpaToscaPolicies(polMapList).validate(new PfValidationResult()).isValid());
77         assertThatThrownBy(() -> new JpaToscaPolicies(polMapList).validate(null))
78                 .hasMessageMatching("resultIn is marked .*on.*ull but is null");
79
80         pol0.setDerivedFrom(null);
81         assertTrue(new JpaToscaPolicies(polMapList).validate(new PfValidationResult()).isValid());
82
83         pol0.setDerivedFrom("tosca.Policies.Root");
84         assertTrue(new JpaToscaPolicies(polMapList).validate(new PfValidationResult()).isValid());
85
86         pol0.setDerivedFrom("some.other.Thing");
87         PfValidationResult result = new JpaToscaPolicies(polMapList).validate(new PfValidationResult());
88         assertFalse(result.isValid());
89         assertThat(result.toString()).contains("parent some.other.Thing:0.0.0 of entity not found");
90
91         pol0.setDerivedFrom(null);
92         assertTrue(new JpaToscaPolicies(polMapList).validate(new PfValidationResult()).isValid());
93
94         ToscaPolicy pol1 = new ToscaPolicy();
95         pol1.setName("pol1");
96         pol1.setVersion("0.0.1");
97         pol1.setDescription("pol1 description");
98         pol1.setType("pt0");
99         pol1.setTypeVersion("0.0.1");
100
101         polMapList.get(0).put("pol1", pol1);
102         assertTrue(new JpaToscaPolicies(polMapList).validate(new PfValidationResult()).isValid());
103
104         pol1.setDerivedFrom("pol0");
105         assertTrue(new JpaToscaPolicies(polMapList).validate(new PfValidationResult()).isValid());
106
107         pol1.setDerivedFrom("pol2");
108         result = new JpaToscaPolicies(polMapList).validate(new PfValidationResult());
109         assertFalse(result.isValid());
110         assertThat(result.toString()).contains("parent pol2:0.0.0 of entity not found");
111
112         pol1.setDerivedFrom("pol0");
113         assertTrue(new JpaToscaPolicies(polMapList).validate(new PfValidationResult()).isValid());
114     }
115 }