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
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.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.ToscaPolicy;
41 public class JpaToscaPoliciesTest {
43 private static final String KEY_IS_NULL = "key is marked .*on.*ull but is null";
46 public void testPoliciesErrors() {
47 assertNotNull(new JpaToscaPolicies());
48 assertNotNull(new JpaToscaPolicies(new PfConceptKey()));
49 assertNotNull(new JpaToscaPolicies(new PfConceptKey(), new TreeMap<PfConceptKey, JpaToscaPolicy>()));
50 assertNotNull(new JpaToscaPolicies(new JpaToscaPolicies()));
52 assertThatThrownBy(() -> new JpaToscaPolicies((PfConceptKey) null)).hasMessageMatching(KEY_IS_NULL);
54 assertThatThrownBy(() -> new JpaToscaPolicies((JpaToscaPolicies) null))
55 .hasMessageMatching("copyConcept is marked .*on.*ull but is null");
57 assertThatThrownBy(() -> new JpaToscaPolicies(null, null)).hasMessageMatching(KEY_IS_NULL);
59 assertThatThrownBy(() -> new JpaToscaPolicies(new PfConceptKey(), null))
60 .hasMessageMatching("conceptMap is marked .*on.*ull but is null");
62 assertThatThrownBy(() -> new JpaToscaPolicies(null, new TreeMap<PfConceptKey, JpaToscaPolicy>()))
63 .hasMessageMatching(KEY_IS_NULL);
67 public void testToscaPolicies() {
68 List<Map<String, ToscaPolicy>> polMapList = new ArrayList<>();
69 polMapList.add(new LinkedHashMap<>());
71 ToscaPolicy pol0 = new ToscaPolicy();
73 pol0.setVersion("0.0.1");
74 pol0.setDescription("pol0 description");
76 pol0.setTypeVersion("0.0.1");
78 polMapList.get(0).put("pol0", pol0);
79 assertNotNull(new JpaToscaPolicies(polMapList));
80 assertTrue(new JpaToscaPolicies(polMapList).validate("").isValid());
81 assertThatThrownBy(() -> new JpaToscaPolicies(polMapList).validate(null))
82 .hasMessageMatching("fieldName is marked .*on.*ull but is null");
84 pol0.setDerivedFrom(null);
85 assertTrue(new JpaToscaPolicies(polMapList).validate("").isValid());
87 pol0.setDerivedFrom("tosca.Policies.Root");
88 assertTrue(new JpaToscaPolicies(polMapList).validate("").isValid());
90 pol0.setDerivedFrom("some.other.Thing");
91 BeanValidationResult result = new JpaToscaPolicies(polMapList).validate("");
92 assertFalse(result.isValid());
93 assertThat(result.getResult()).contains("parent").contains("some.other.Thing:0.0.0")
94 .contains(Validated.NOT_FOUND);
96 pol0.setDerivedFrom(null);
97 assertTrue(new JpaToscaPolicies(polMapList).validate("").isValid());
99 ToscaPolicy pol1 = new ToscaPolicy();
100 pol1.setName("pol1");
101 pol1.setVersion("0.0.1");
102 pol1.setDescription("pol1 description");
104 pol1.setTypeVersion("0.0.1");
106 polMapList.get(0).put("pol1", pol1);
107 assertTrue(new JpaToscaPolicies(polMapList).validate("").isValid());
109 pol1.setDerivedFrom("pol0");
110 assertTrue(new JpaToscaPolicies(polMapList).validate("").isValid());
112 pol1.setDerivedFrom("pol2");
113 result = new JpaToscaPolicies(polMapList).validate("");
114 assertFalse(result.isValid());
115 assertThat(result.getResult()).contains("parent").contains("pol2:0.0.0").contains(Validated.NOT_FOUND);
117 pol1.setDerivedFrom("pol0");
118 assertTrue(new JpaToscaPolicies(polMapList).validate("").isValid());