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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.models.tosca.simple.concepts;
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;
30 import java.util.ArrayList;
31 import java.util.LinkedHashMap;
32 import java.util.List;
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.ToscaPolicyType;
40 public class JpaToscaPolicyTypesTest {
42 private static final String KEY_IS_NULL = "key is marked .*on.*ull but is null";
45 public void testPolicyTypes() {
46 assertNotNull(new JpaToscaPolicyTypes());
47 assertNotNull(new JpaToscaPolicyTypes(new PfConceptKey()));
48 assertNotNull(new JpaToscaPolicyTypes(new PfConceptKey(), new TreeMap<PfConceptKey, JpaToscaPolicyType>()));
49 assertNotNull(new JpaToscaPolicyTypes(new JpaToscaPolicyTypes()));
51 assertThatThrownBy(() -> new JpaToscaPolicyTypes((PfConceptKey) null)).hasMessageMatching(KEY_IS_NULL);
53 assertThatThrownBy(() -> new JpaToscaPolicyTypes((JpaToscaPolicyTypes) null))
54 .hasMessageMatching("copyConcept is marked .*on.*ull but is null");
56 assertThatThrownBy(() -> new JpaToscaPolicyTypes(null, null)).hasMessageMatching(KEY_IS_NULL);
58 assertThatThrownBy(() -> new JpaToscaPolicyTypes(new PfConceptKey(), null))
59 .hasMessageMatching("conceptMap is marked .*on.*ull but is null");
61 assertThatThrownBy(() -> new JpaToscaPolicyTypes(null, new TreeMap<PfConceptKey, JpaToscaPolicyType>()))
62 .hasMessageMatching(KEY_IS_NULL);
64 List<Map<String, ToscaPolicyType>> ptMapList = new ArrayList<>();
65 ptMapList.add(new LinkedHashMap<>());
67 ToscaPolicyType pt0 = new ToscaPolicyType();
69 pt0.setVersion("0.0.1");
70 pt0.setDescription("pt0 description");
72 ptMapList.get(0).put("pt0", pt0);
73 assertNotNull(new JpaToscaPolicyTypes(ptMapList));
74 assertTrue(new JpaToscaPolicyTypes(ptMapList).validate(new PfValidationResult()).isValid());
75 assertThatThrownBy(() -> new JpaToscaPolicyTypes(ptMapList).validate(null))
76 .hasMessageMatching("resultIn is marked .*on.*ull but is null");
78 pt0.setDerivedFrom(null);
79 assertTrue(new JpaToscaPolicyTypes(ptMapList).validate(new PfValidationResult()).isValid());
81 pt0.setDerivedFrom("tosca.PolicyTypes.Root");
82 assertTrue(new JpaToscaPolicyTypes(ptMapList).validate(new PfValidationResult()).isValid());
84 pt0.setDerivedFrom("some.other.Thing");
85 PfValidationResult result = new JpaToscaPolicyTypes(ptMapList).validate(new PfValidationResult());
86 assertFalse(result.isValid());
87 assertThat(result.toString()).contains("parent some.other.Thing:0.0.0 of entity not found");
89 pt0.setDerivedFrom(null);
90 assertTrue(new JpaToscaPolicyTypes(ptMapList).validate(new PfValidationResult()).isValid());
92 ToscaPolicyType pt1 = new ToscaPolicyType();
94 pt1.setVersion("0.0.1");
95 pt1.setDescription("pt1 description");
97 ptMapList.get(0).put("pt1", pt1);
98 assertTrue(new JpaToscaPolicyTypes(ptMapList).validate(new PfValidationResult()).isValid());
100 pt1.setDerivedFrom("pt0");
101 assertTrue(new JpaToscaPolicyTypes(ptMapList).validate(new PfValidationResult()).isValid());
103 pt1.setDerivedFrom("pt2");
104 result = new JpaToscaPolicyTypes(ptMapList).validate(new PfValidationResult());
105 assertFalse(result.isValid());
106 assertThat(result.toString()).contains("parent pt2:0.0.0 of entity not found");
108 pt1.setDerivedFrom("pt0");
109 assertTrue(new JpaToscaPolicyTypes(ptMapList).validate(new PfValidationResult()).isValid());