2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2024 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.jupiter.api.Assertions.assertFalse;
27 import static org.junit.jupiter.api.Assertions.assertNotNull;
28 import static org.junit.jupiter.api.Assertions.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.jupiter.api.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.ToscaDataType;
41 class JpaToscaDataTypesTest {
43 private static final String KEY_IS_NULL = "key is marked .*on.*ull but is null";
46 void testDataTypes() {
47 assertNotNull(new JpaToscaDataTypes());
48 assertNotNull(new JpaToscaDataTypes(new PfConceptKey()));
49 assertNotNull(new JpaToscaDataTypes(new PfConceptKey(), new TreeMap<PfConceptKey, JpaToscaDataType>()));
50 assertNotNull(new JpaToscaDataTypes(new JpaToscaDataTypes()));
52 assertThatThrownBy(() -> new JpaToscaDataTypes((PfConceptKey) null)).hasMessageMatching(KEY_IS_NULL);
54 assertThatThrownBy(() -> new JpaToscaDataTypes((JpaToscaDataTypes) null))
55 .hasMessageMatching("copyConcept is marked .*on.*ull but is null");
57 assertThatThrownBy(() -> new JpaToscaDataTypes(null, null)).hasMessageMatching(KEY_IS_NULL);
59 assertThatThrownBy(() -> new JpaToscaDataTypes(new PfConceptKey(), null))
60 .hasMessageMatching("conceptMap is marked .*on.*ull but is null");
62 assertThatThrownBy(() -> new JpaToscaDataTypes(null, new TreeMap<PfConceptKey, JpaToscaDataType>()))
63 .hasMessageMatching(KEY_IS_NULL);
67 void testDerivedDataTypes() {
68 List<Map<String, ToscaDataType>> dtMapList = new ArrayList<>();
69 dtMapList.add(new LinkedHashMap<>());
71 ToscaDataType dt0 = new ToscaDataType();
73 dt0.setVersion("0.0.1");
74 dt0.setDescription("dt0 description");
76 dtMapList.get(0).put("dt0", dt0);
77 assertNotNull(new JpaToscaDataTypes(dtMapList));
78 assertTrue(new JpaToscaDataTypes(dtMapList).validate("").isValid());
79 assertThatThrownBy(() -> new JpaToscaDataTypes(dtMapList).validate(null))
80 .hasMessageMatching("fieldName is marked .*on.*ull but is null");
82 dt0.setDerivedFrom(null);
83 assertTrue(new JpaToscaDataTypes(dtMapList).validate("").isValid());
85 dt0.setDerivedFrom("tosca.datatypes.Root");
86 assertTrue(new JpaToscaDataTypes(dtMapList).validate("").isValid());
88 dt0.setDerivedFrom("some.other.Thing");
89 BeanValidationResult result = new JpaToscaDataTypes(dtMapList).validate("");
90 assertFalse(result.isValid());
91 assertThat(result.getResult()).contains("parent").contains("some.other.Thing:0.0.0")
92 .contains(Validated.NOT_FOUND);
94 dt0.setDerivedFrom(null);
95 assertTrue(new JpaToscaDataTypes(dtMapList).validate("").isValid());
97 ToscaDataType dt1 = new ToscaDataType();
99 dt1.setVersion("0.0.1");
100 dt1.setDescription("dt1 description");
102 dtMapList.get(0).put("dt1", dt1);
103 assertTrue(new JpaToscaDataTypes(dtMapList).validate("").isValid());
105 dt1.setDerivedFrom("dt0");
106 assertTrue(new JpaToscaDataTypes(dtMapList).validate("").isValid());
108 dt1.setDerivedFrom("dt2");
109 result = new JpaToscaDataTypes(dtMapList).validate("");
110 assertFalse(result.isValid());
111 assertThat(result.getResult()).contains("parent").contains("dt2:0.0.0").contains(Validated.NOT_FOUND);
113 dt1.setDerivedFrom("dt0");
114 assertTrue(new JpaToscaDataTypes(dtMapList).validate("").isValid());