2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2025 OpenInfra Foundation Europe. All rights reserved.
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.clamp.models.acm.document.base;
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
26 import java.util.List;
28 import org.junit.jupiter.api.Test;
29 import org.onap.policy.clamp.models.acm.document.concepts.DocToscaDataType;
30 import org.onap.policy.clamp.models.acm.document.concepts.DocToscaNodeTemplate;
31 import org.onap.policy.clamp.models.acm.document.concepts.DocToscaPolicy;
32 import org.onap.policy.clamp.models.acm.document.concepts.DocToscaProperty;
33 import org.onap.policy.clamp.models.acm.document.concepts.DocToscaRequirement;
34 import org.onap.policy.clamp.models.acm.document.concepts.DocToscaServiceTemplate;
35 import org.onap.policy.clamp.models.acm.document.concepts.DocToscaTopologyTemplate;
36 import org.onap.policy.common.parameters.BeanValidationResult;
38 class ToscaServiceTemplateValidationTest {
40 public static final String AUTOMATION_COMPOSITION_ELEMENT =
41 "org.onap.policy.clamp.acm.AutomationCompositionElement";
42 public static final String AUTOMATION_COMPOSITION_NODE_TYPE = "org.onap.policy.clamp.acm.AutomationComposition";
43 private static final String NAME_VERSION1 = "name:1.0.0";
44 private static final String NAME_VERSION2 = "name:1.0.1";
45 private static final String NAME = "name";
46 private static final String TYPE = "type";
47 private static final String VERSION = "1.0.0";
51 var doc = new DocToscaServiceTemplate();
53 () -> ToscaServiceTemplateValidation.validate(null, doc, AUTOMATION_COMPOSITION_NODE_TYPE))
54 .isInstanceOf(NullPointerException.class);
55 var result = getValidate(doc);
56 assertThat(result.isValid()).isFalse();
58 var docDataType = new DocToscaDataType();
59 docDataType.setName(NAME);
60 docDataType.setVersion(VERSION);
61 var docProperty = new DocToscaProperty();
62 docProperty.setType(TYPE);
63 docProperty.setTypeVersion(VERSION);
64 docDataType.setProperties(Map.of(NAME_VERSION1, docProperty));
65 docDataType.setDerivedFrom("NotExist");
66 doc.setDataTypes(Map.of(NAME_VERSION1, docDataType));
67 result = getValidate(doc);
68 assertThat(result.isValid()).isFalse();
71 docDataType.setDerivedFrom(NAME);
72 result = getValidate(doc);
73 assertThat(result.isValid()).isFalse();
75 doc.setDataTypes(null);
76 var docTopologyTemplate = new DocToscaTopologyTemplate();
77 doc.setToscaTopologyTemplate(docTopologyTemplate);
78 result = getValidate(doc);
79 assertThat(result.isValid()).isFalse();
81 var docPolicy = new DocToscaPolicy();
82 docPolicy.setType(TYPE);
83 docPolicy.setTypeVersion(VERSION);
84 docTopologyTemplate.setPolicies(Map.of(NAME_VERSION1, docPolicy));
85 result = getValidate(doc);
86 assertThat(result.isValid()).isFalse();
88 docTopologyTemplate.setPolicies(null);
89 var docNodeTemplates = new DocToscaNodeTemplate();
90 docNodeTemplates.setType(TYPE);
91 docNodeTemplates.setTypeVersion(VERSION);
92 var docRequirement = new DocToscaRequirement();
93 docRequirement.setType(TYPE);
94 docRequirement.setTypeVersion(VERSION);
95 docNodeTemplates.setRequirements(List.of(Map.of(NAME_VERSION1, docRequirement)));
96 docTopologyTemplate.setNodeTemplates(Map.of(NAME_VERSION1, docNodeTemplates));
97 result = getValidate(doc);
98 assertThat(result.isValid()).isFalse();
100 docRequirement.setDerivedFrom("NotExist");
101 result = getValidate(doc);
102 assertThat(result.isValid()).isFalse();
105 private BeanValidationResult getValidate(DocToscaServiceTemplate doc) {
106 var result = new BeanValidationResult("DocToscaServiceTemplate", doc);
107 ToscaServiceTemplateValidation.validate(result, doc, AUTOMATION_COMPOSITION_NODE_TYPE);
112 void testValidateToscaTopologyTemplate() {
113 var result = getValidateToscaTopologyTemplate(null);
114 assertThat(result.isValid()).isFalse();
116 var doc = new DocToscaTopologyTemplate();
117 assertThatThrownBy(() -> ToscaServiceTemplateValidation
118 .validateToscaTopologyTemplate(null, doc, AUTOMATION_COMPOSITION_NODE_TYPE))
119 .isInstanceOf(NullPointerException.class);
120 result = getValidateToscaTopologyTemplate(doc);
121 assertThat(result.isValid()).isFalse();
123 var docNodeTemplate1 = new DocToscaNodeTemplate();
124 docNodeTemplate1.setType(AUTOMATION_COMPOSITION_NODE_TYPE);
125 docNodeTemplate1.setTypeVersion(VERSION);
126 var docNodeTemplate2 = new DocToscaNodeTemplate();
127 docNodeTemplate2.setType(AUTOMATION_COMPOSITION_NODE_TYPE);
128 docNodeTemplate2.setTypeVersion("1.0.1");
129 doc.setNodeTemplates(Map.of(NAME_VERSION1, docNodeTemplate1, NAME_VERSION2, docNodeTemplate2));
130 result = getValidateToscaTopologyTemplate(doc);
131 assertThat(result.isValid()).isFalse();
134 private BeanValidationResult getValidateToscaTopologyTemplate(DocToscaTopologyTemplate doc) {
135 var result = new BeanValidationResult("DocToscaTopologyTemplate", doc);
136 ToscaServiceTemplateValidation.validateToscaTopologyTemplate(result, doc, AUTOMATION_COMPOSITION_NODE_TYPE);