Restructure for authorative models
[policy/models.git] / models-tosca / src / test / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaPolicyTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.models.tosca.simple.concepts;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertTrue;
27 import static org.junit.Assert.fail;
28
29 import java.util.ArrayList;
30 import java.util.HashMap;
31 import java.util.List;
32 import java.util.Map;
33
34 import org.junit.Test;
35 import org.onap.policy.models.base.PfConceptKey;
36 import org.onap.policy.models.base.PfValidationResult;
37 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicy;
38
39 /**
40  * DAO test for ToscaPolicy.
41  *
42  * @author Liam Fallon (liam.fallon@est.tech)
43  */
44 public class JpaToscaPolicyTest {
45
46     @Test
47     public void testPolicyPojo() {
48         assertNotNull(new JpaToscaPolicy());
49         assertNotNull(new JpaToscaPolicy(new PfConceptKey()));
50         assertNotNull(new JpaToscaPolicy(new PfConceptKey(), new PfConceptKey()));
51         assertNotNull(new JpaToscaPolicy(new JpaToscaPolicy()));
52
53         try {
54             new JpaToscaPolicy((PfConceptKey) null);
55             fail("test should throw an exception");
56         } catch (Exception exc) {
57             assertEquals("key is marked @NonNull but is null", exc.getMessage());
58         }
59
60         try {
61             new JpaToscaPolicy(null, null);
62             fail("test should throw an exception");
63         } catch (Exception exc) {
64             assertEquals("key is marked @NonNull but is null", exc.getMessage());
65         }
66
67         try {
68             new JpaToscaPolicy(new PfConceptKey(), null);
69             fail("test should throw an exception");
70         } catch (Exception exc) {
71             assertEquals("type is marked @NonNull but is null", exc.getMessage());
72         }
73
74         try {
75             new JpaToscaPolicy(null, new PfConceptKey());
76             fail("test should throw an exception");
77         } catch (Exception exc) {
78             assertEquals("key is marked @NonNull but is null", exc.getMessage());
79         }
80
81         try {
82             new JpaToscaPolicy((JpaToscaPolicy) null);
83             fail("test should throw an exception");
84         } catch (Exception exc) {
85             assertEquals("copyConcept is marked @NonNull but is null", exc.getMessage());
86         }
87
88         PfConceptKey tpKey = new PfConceptKey("tdt", "0.0.1");
89         PfConceptKey ptKey = new PfConceptKey("policyType", "0.0.1");
90         JpaToscaPolicy tp = new JpaToscaPolicy(tpKey, ptKey);
91
92         Map<String, String> propertyMap = new HashMap<>();
93         propertyMap.put("Property", "Property Value");
94         tp.setProperties(propertyMap);
95         assertEquals(propertyMap, tp.getProperties());
96
97         List<PfConceptKey> targets = new ArrayList<>();
98         PfConceptKey target = new PfConceptKey("target", "0.0.1");
99         targets.add(target);
100         tp.setTargets(targets);
101         assertEquals(targets, tp.getTargets());
102
103         JpaToscaPolicy tdtClone0 = new JpaToscaPolicy(tp);
104         assertEquals(tp, tdtClone0);
105         assertEquals(0, tp.compareTo(tdtClone0));
106
107         JpaToscaPolicy tdtClone1 = new JpaToscaPolicy();
108         tp.copyTo(tdtClone1);
109         assertEquals(tp, tdtClone1);
110         assertEquals(0, tp.compareTo(tdtClone1));
111
112         assertEquals(-1, tp.compareTo(null));
113         assertEquals(0, tp.compareTo(tp));
114         assertFalse(tp.compareTo(tp.getKey()) == 0);
115
116         PfConceptKey otherDtKey = new PfConceptKey("otherDt", "0.0.1");
117         JpaToscaPolicy otherDt = new JpaToscaPolicy(otherDtKey);
118
119         assertFalse(tp.compareTo(otherDt) == 0);
120         otherDt.setKey(tpKey);
121         assertFalse(tp.compareTo(otherDt) == 0);
122         otherDt.setType(ptKey);
123         assertFalse(tp.compareTo(otherDt) == 0);
124         otherDt.setProperties(propertyMap);
125         assertFalse(tp.compareTo(otherDt) == 0);
126         otherDt.setTargets(targets);
127         assertEquals(0, tp.compareTo(otherDt));
128
129         try {
130             tp.copyTo(null);
131             fail("test should throw an exception");
132         } catch (Exception exc) {
133             assertEquals("target is marked @NonNull but is null", exc.getMessage());
134         }
135
136         assertEquals(3, tp.getKeys().size());
137         assertEquals(2, new JpaToscaPolicy().getKeys().size());
138
139         new JpaToscaPolicy().clean();
140         tp.clean();
141         assertEquals(tdtClone0, tp);
142
143         assertFalse(new JpaToscaPolicy().validate(new PfValidationResult()).isValid());
144         System.err.println(tp.validate(new PfValidationResult()));
145         assertTrue(tp.validate(new PfValidationResult()).isValid());
146
147         tp.getProperties().put(null, null);
148         assertFalse(tp.validate(new PfValidationResult()).isValid());
149         tp.getProperties().remove(null);
150         assertTrue(tp.validate(new PfValidationResult()).isValid());
151
152         tp.getProperties().put("Key", null);
153         assertFalse(tp.validate(new PfValidationResult()).isValid());
154         tp.getProperties().remove("Key");
155         assertTrue(tp.validate(new PfValidationResult()).isValid());
156
157         tp.getProperties().put(null, "Value");
158         assertFalse(tp.validate(new PfValidationResult()).isValid());
159         tp.getProperties().remove(null);
160         assertTrue(tp.validate(new PfValidationResult()).isValid());
161
162         tp.getTargets().add(null);
163         assertFalse(tp.validate(new PfValidationResult()).isValid());
164         tp.getTargets().remove(null);
165         assertTrue(tp.validate(new PfValidationResult()).isValid());
166
167         try {
168             tp.validate(null);
169             fail("test should throw an exception");
170         } catch (Exception exc) {
171             assertEquals("resultIn is marked @NonNull but is null", exc.getMessage());
172         }
173     }
174 }