2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 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.clamp.controlloop.runtime.commissioning;
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.jupiter.api.Assertions.assertTrue;
27 import java.util.ArrayList;
28 import java.util.List;
29 import org.junit.jupiter.api.Test;
30 import org.onap.policy.clamp.controlloop.runtime.main.parameters.ClRuntimeParameterGroup;
31 import org.onap.policy.common.utils.coder.Coder;
32 import org.onap.policy.common.utils.coder.CoderException;
33 import org.onap.policy.common.utils.coder.StandardCoder;
34 import org.onap.policy.common.utils.coder.YamlJsonTranslator;
35 import org.onap.policy.common.utils.resources.ResourceUtils;
36 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
37 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
38 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeType;
39 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
41 class CommissioningProviderTest {
42 private static final String TOSCA_SERVICE_TEMPLATE_YAML =
43 "src/test/resources/rest/servicetemplates/pmsh_multiple_cl_tosca.yaml";
44 private static final String TEMPLATE_IS_NULL = ".*serviceTemplate is marked non-null but is null";
45 private static final Coder CODER = new StandardCoder();
46 private static final YamlJsonTranslator yamlTranslator = new YamlJsonTranslator();
47 private static int dbNum = 0;
48 private static final Object lockit = new Object();
50 private static String getParameterGroupAsString() {
52 return ResourceUtils.getResourceAsString("src/test/resources/parameters/TestParameters.json")
53 .replace("jdbc:h2:mem:testdb", "jdbc:h2:mem:commissioningdb" + dbNum);
57 * return a Cl Runtime Parameters.
59 * @throws CoderException .
61 public ClRuntimeParameterGroup getClRuntimeParameterGroup() throws CoderException {
62 synchronized (lockit) {
63 return CODER.decode(getParameterGroupAsString(), ClRuntimeParameterGroup.class);
68 * Test the fetching of control loop definitions (ToscaServiceTemplates).
73 void testGetControlLoopDefinitions() throws Exception {
74 List<ToscaNodeTemplate> listOfTemplates;
75 ClRuntimeParameterGroup clRuntimeParameterGroup = getClRuntimeParameterGroup();
77 try (var provider = new CommissioningProvider(clRuntimeParameterGroup)) {
78 ToscaServiceTemplate serviceTemplate = yamlTranslator.fromYaml(
79 ResourceUtils.getResourceAsString(TOSCA_SERVICE_TEMPLATE_YAML), ToscaServiceTemplate.class);
81 listOfTemplates = provider.getControlLoopDefinitions(null, null);
82 assertThat(listOfTemplates).isEmpty();
84 provider.createControlLoopDefinitions(serviceTemplate);
85 listOfTemplates = provider.getControlLoopDefinitions(null, null);
86 assertThat(listOfTemplates).hasSize(2);
90 provider.getControlLoopDefinitions("org.onap.domain.pmsh.PMSHControlLoopDefinition", "1.2.3");
91 assertThat(listOfTemplates).hasSize(1);
92 for (ToscaNodeTemplate template : listOfTemplates) {
93 // Other CL elements contain PMSD instead of PMSH in their name
94 assertThat(template.getName()).doesNotContain("PMSD");
98 listOfTemplates = provider.getControlLoopDefinitions("WrongControlLoopName", "0.0.0");
99 assertThat(listOfTemplates).isEmpty();
104 * Test the creation of control loop definitions (ToscaServiceTemplates).
106 * @throws Exception .
109 void testCreateControlLoopDefinitions() throws Exception {
110 List<ToscaNodeTemplate> listOfTemplates;
111 ClRuntimeParameterGroup clRuntimeParameterGroup = getClRuntimeParameterGroup();
113 try (var provider = new CommissioningProvider(clRuntimeParameterGroup)) {
114 // Test Service template is null
115 assertThatThrownBy(() -> provider.createControlLoopDefinitions(null)).hasMessageMatching(TEMPLATE_IS_NULL);
116 listOfTemplates = provider.getControlLoopDefinitions(null, null);
117 assertThat(listOfTemplates).isEmpty();
119 ToscaServiceTemplate serviceTemplate = yamlTranslator.fromYaml(
120 ResourceUtils.getResourceAsString(TOSCA_SERVICE_TEMPLATE_YAML), ToscaServiceTemplate.class);
122 // Response should return the number of node templates present in the service template
123 List<ToscaConceptIdentifier> affectedDefinitions =
124 provider.createControlLoopDefinitions(serviceTemplate).getAffectedControlLoopDefinitions();
125 assertThat(affectedDefinitions).hasSize(13);
126 listOfTemplates = provider.getControlLoopDefinitions(null, null);
127 assertThat(listOfTemplates).hasSize(2);
132 * Test the deletion of control loop definitions (ToscaServiceTemplate).
134 * @throws Exception .
137 void testDeleteControlLoopDefinitions() throws Exception {
138 List<ToscaNodeTemplate> listOfTemplates;
139 ClRuntimeParameterGroup clRuntimeParameterGroup = getClRuntimeParameterGroup();
141 try (var provider = new CommissioningProvider(clRuntimeParameterGroup)) {
142 ToscaServiceTemplate serviceTemplate = yamlTranslator.fromYaml(
143 ResourceUtils.getResourceAsString(TOSCA_SERVICE_TEMPLATE_YAML), ToscaServiceTemplate.class);
145 listOfTemplates = provider.getControlLoopDefinitions(null, null);
146 assertThat(listOfTemplates).isEmpty();
148 provider.createControlLoopDefinitions(serviceTemplate);
149 listOfTemplates = provider.getControlLoopDefinitions(null, null);
150 assertThat(listOfTemplates).hasSize(2);
152 provider.deleteControlLoopDefinition(serviceTemplate.getName(), serviceTemplate.getVersion());
153 listOfTemplates = provider.getControlLoopDefinitions(null, null);
154 assertThat(listOfTemplates).isEmpty();
159 * Test the fetching of control loop element definitions.
161 * @throws Exception .
164 void testGetControlLoopElementDefinitions() throws Exception {
165 ClRuntimeParameterGroup clRuntimeParameterGroup = getClRuntimeParameterGroup();
166 try (var provider = new CommissioningProvider(clRuntimeParameterGroup)) {
167 ToscaServiceTemplate serviceTemplate = yamlTranslator.fromYaml(
168 ResourceUtils.getResourceAsString(TOSCA_SERVICE_TEMPLATE_YAML), ToscaServiceTemplate.class);
170 provider.getControlLoopDefinitions(null, null);
172 provider.createControlLoopDefinitions(serviceTemplate);
173 List<ToscaNodeTemplate> controlLoopDefinitionList =
174 provider.getControlLoopDefinitions("org.onap.domain.pmsh.PMSHControlLoopDefinition", "1.2.3");
176 List<ToscaNodeTemplate> controlLoopElementNodeTemplates =
177 provider.getControlLoopElementDefinitions(controlLoopDefinitionList.get(0));
179 // 4 PMSH control loop elements definitions.
180 assertThat(controlLoopElementNodeTemplates).hasSize(4);
182 List<ToscaNodeType> derivedTypes = getDerivedNodeTypes(serviceTemplate);
183 for (ToscaNodeTemplate template : controlLoopElementNodeTemplates) {
184 assertTrue(checkNodeType(template, derivedTypes));
189 private boolean checkNodeType(ToscaNodeTemplate template, List<ToscaNodeType> derivedNodeTypes) {
190 String controlLoopElementType = "org.onap.policy.clamp.controlloop.ControlLoopElement";
191 for (ToscaNodeType derivedType : derivedNodeTypes) {
192 if (template.getType().equals(derivedType.getName()) || template.getType().equals(controlLoopElementType)) {
199 private List<ToscaNodeType> getDerivedNodeTypes(ToscaServiceTemplate serviceTemplate) {
200 String type = "org.onap.policy.clamp.controlloop.ControlLoopElement";
201 List<ToscaNodeType> nodeTypes = new ArrayList<>();
202 for (ToscaNodeType nodeType : serviceTemplate.getNodeTypes().values()) {
203 if (nodeType.getDerivedFrom().equals(type)) {
204 nodeTypes.add(nodeType);