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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.models.tosca.simple.concepts;
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;
29 import java.util.ArrayList;
30 import java.util.HashMap;
31 import java.util.List;
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.ToscaPolicy;
40 * DAO test for ToscaPolicy.
42 * @author Liam Fallon (liam.fallon@est.tech)
44 public class ToscaPolicyTest {
47 public void testPolicyPojo() {
48 assertNotNull(new ToscaPolicy());
49 assertNotNull(new ToscaPolicy(new PfConceptKey()));
50 assertNotNull(new ToscaPolicy(new PfConceptKey(), new PfConceptKey()));
51 assertNotNull(new ToscaPolicy(new ToscaPolicy()));
54 new ToscaPolicy((PfConceptKey) null);
55 fail("test should throw an exception");
56 } catch (Exception exc) {
57 assertEquals("key is marked @NonNull but is null", exc.getMessage());
61 new ToscaPolicy(null, null);
62 fail("test should throw an exception");
63 } catch (Exception exc) {
64 assertEquals("key is marked @NonNull but is null", exc.getMessage());
68 new ToscaPolicy(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());
75 new ToscaPolicy(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());
82 new ToscaPolicy((ToscaPolicy) null);
83 fail("test should throw an exception");
84 } catch (Exception exc) {
85 assertEquals("copyConcept is marked @NonNull but is null", exc.getMessage());
88 PfConceptKey tpKey = new PfConceptKey("tdt", "0.0.1");
89 PfConceptKey ptKey = new PfConceptKey("policyType", "0.0.1");
90 ToscaPolicy tp = new ToscaPolicy(tpKey, ptKey);
92 Map<String, String> propertyMap = new HashMap<>();
93 propertyMap.put("Property", "Property Value");
94 tp.setProperties(propertyMap);
95 assertEquals(propertyMap, tp.getProperties());
97 List<PfConceptKey> targets = new ArrayList<>();
98 PfConceptKey target = new PfConceptKey("target", "0.0.1");
100 tp.setTargets(targets);
101 assertEquals(targets, tp.getTargets());
103 ToscaPolicy tdtClone0 = new ToscaPolicy(tp);
104 assertEquals(tp, tdtClone0);
105 assertEquals(0, tp.compareTo(tdtClone0));
107 ToscaPolicy tdtClone1 = new ToscaPolicy();
108 tp.copyTo(tdtClone1);
109 assertEquals(tp, tdtClone1);
110 assertEquals(0, tp.compareTo(tdtClone1));
112 assertEquals(-1, tp.compareTo(null));
113 assertEquals(0, tp.compareTo(tp));
114 assertFalse(tp.compareTo(tp.getKey()) == 0);
116 PfConceptKey otherDtKey = new PfConceptKey("otherDt", "0.0.1");
117 ToscaPolicy otherDt = new ToscaPolicy(otherDtKey);
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));
131 fail("test should throw an exception");
132 } catch (Exception exc) {
133 assertEquals("target is marked @NonNull but is null", exc.getMessage());
136 assertEquals(3, tp.getKeys().size());
137 assertEquals(2, new ToscaPolicy().getKeys().size());
139 new ToscaPolicy().clean();
141 assertEquals(tdtClone0, tp);
143 assertFalse(new ToscaPolicy().validate(new PfValidationResult()).isValid());
144 System.err.println(tp.validate(new PfValidationResult()));
145 assertTrue(tp.validate(new PfValidationResult()).isValid());
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());
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());
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());
162 tp.getTargets().add(null);
163 assertFalse(tp.validate(new PfValidationResult()).isValid());
164 tp.getTargets().remove(null);
165 assertTrue(tp.validate(new PfValidationResult()).isValid());
169 fail("test should throw an exception");
170 } catch (Exception exc) {
171 assertEquals("resultIn is marked @NonNull but is null", exc.getMessage());