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.Assert.assertFalse;
26 import static org.junit.Assert.assertTrue;
28 import java.util.ArrayList;
29 import java.util.List;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.onap.policy.clamp.controlloop.runtime.main.parameters.ClRuntimeParameterGroup;
33 import org.onap.policy.common.utils.coder.Coder;
34 import org.onap.policy.common.utils.coder.CoderException;
35 import org.onap.policy.common.utils.coder.StandardCoder;
36 import org.onap.policy.common.utils.coder.YamlJsonTranslator;
37 import org.onap.policy.common.utils.resources.ResourceUtils;
38 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
39 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
40 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeType;
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
44 public class CommissioningProviderTest {
45 private static final String TOSCA_SERVICE_TEMPLATE_YAML =
46 "src/test/resources/rest/servicetemplates/pmsh_multiple_cl_tosca.yaml";
47 private static final String TEMPLATE_IS_NULL = ".*serviceTemplate is marked non-null but is null";
48 private static final Coder CODER = new StandardCoder();
49 private static final YamlJsonTranslator yamlTranslator = new YamlJsonTranslator();
50 private static int dbNum = 0;
51 private static final Object lockit = new Object();
52 private PolicyModelsProviderParameters databaseProviderParameters;
54 private static String getParameterGroupAsString() {
56 return ResourceUtils.getResourceAsString("src/test/resources/parameters/TestParameters.json")
57 .replace("jdbc:h2:mem:testdb", "jdbc:h2:mem:commissioningdb" + dbNum);
61 * Sets up db provider parameters before each test.
63 * @throws CoderException .
66 public void setupDbProviderParameters() throws CoderException {
67 synchronized (lockit) {
68 databaseProviderParameters = CODER.decode(getParameterGroupAsString(), ClRuntimeParameterGroup.class)
69 .getDatabaseProviderParameters();
74 * Test the fetching of control loop definitions (ToscaServiceTemplates).
79 public void testGetControlLoopDefinitions() throws Exception {
80 List<ToscaNodeTemplate> listOfTemplates;
82 try (CommissioningProvider provider = new CommissioningProvider(databaseProviderParameters)) {
83 ToscaServiceTemplate serviceTemplate = yamlTranslator
84 .fromYaml(ResourceUtils.getResourceAsString(TOSCA_SERVICE_TEMPLATE_YAML),
85 ToscaServiceTemplate.class);
88 listOfTemplates = provider.getControlLoopDefinitions(null, null);
89 assertThat(listOfTemplates).isEmpty();
91 provider.createControlLoopDefinitions(serviceTemplate);
92 listOfTemplates = provider.getControlLoopDefinitions(null, null);
93 assertThat(listOfTemplates).hasSize(2);
96 listOfTemplates = provider.getControlLoopDefinitions("org.onap.domain.pmsh.PMSHControlLoopDefinition",
98 assertThat(listOfTemplates).hasSize(1);
99 for (ToscaNodeTemplate template : listOfTemplates) {
100 //Other CL elements contain PMSD instead of PMSH in their name
101 assertFalse(template.getName().contains("PMSD"));
105 listOfTemplates = provider.getControlLoopDefinitions("WrongControlLoopName", "0.0.0");
106 assertThat(listOfTemplates).isEmpty();
111 * Test the creation of control loop definitions (ToscaServiceTemplates).
113 * @throws Exception .
116 public void testCreateControlLoopDefinitions() throws Exception {
117 List<ToscaNodeTemplate> listOfTemplates;
119 try (CommissioningProvider provider = new CommissioningProvider(databaseProviderParameters)) {
120 //Test Service template is null
121 assertThatThrownBy(() -> provider.createControlLoopDefinitions(null)).hasMessageMatching(TEMPLATE_IS_NULL);
122 listOfTemplates = provider.getControlLoopDefinitions(null, null);
123 assertThat(listOfTemplates).isEmpty();
125 ToscaServiceTemplate serviceTemplate = yamlTranslator
126 .fromYaml(ResourceUtils.getResourceAsString(TOSCA_SERVICE_TEMPLATE_YAML),
127 ToscaServiceTemplate.class);
129 // Response should return the number of node templates present in the service template
130 List<ToscaConceptIdentifier> affectedDefinitions =
131 provider.createControlLoopDefinitions(serviceTemplate).getAffectedControlLoopDefinitions();
132 assertThat(affectedDefinitions).hasSize(13);
133 listOfTemplates = provider.getControlLoopDefinitions(null, null);
134 assertThat(listOfTemplates).hasSize(2);
139 * Test the deletion of control loop definitions (ToscaServiceTemplate).
141 * @throws Exception .
144 public void testDeleteControlLoopDefinitions() throws Exception {
145 List<ToscaNodeTemplate> listOfTemplates;
147 try (CommissioningProvider provider = new CommissioningProvider(databaseProviderParameters)) {
148 ToscaServiceTemplate serviceTemplate = yamlTranslator
149 .fromYaml(ResourceUtils.getResourceAsString(TOSCA_SERVICE_TEMPLATE_YAML),
150 ToscaServiceTemplate.class);
152 listOfTemplates = provider.getControlLoopDefinitions(null, null);
153 assertThat(listOfTemplates).isEmpty();
155 provider.createControlLoopDefinitions(serviceTemplate);
156 listOfTemplates = provider.getControlLoopDefinitions(null, null);
157 assertThat(listOfTemplates).hasSize(2);
159 provider.deleteControlLoopDefinition(serviceTemplate.getName(), serviceTemplate.getVersion());
160 listOfTemplates = provider.getControlLoopDefinitions(null, null);
161 assertThat(listOfTemplates).isEmpty();
166 * Test the fetching of control loop element definitions.
168 * @throws Exception .
171 public void testGetControlLoopElementDefinitions() throws Exception {
172 try (CommissioningProvider provider = new CommissioningProvider(databaseProviderParameters)) {
173 ToscaServiceTemplate serviceTemplate = yamlTranslator
174 .fromYaml(ResourceUtils.getResourceAsString(TOSCA_SERVICE_TEMPLATE_YAML),
175 ToscaServiceTemplate.class);
177 provider.getControlLoopDefinitions(null, null);
179 provider.createControlLoopDefinitions(serviceTemplate);
180 List<ToscaNodeTemplate> controlLoopDefinitionList = provider.getControlLoopDefinitions(
181 "org.onap.domain.pmsh.PMSHControlLoopDefinition", "1.2.3");
183 List<ToscaNodeTemplate> controlLoopElementNodeTemplates =
184 provider.getControlLoopElementDefinitions(controlLoopDefinitionList.get(0));
186 // 4 PMSH control loop elements definitions.
187 assertThat(controlLoopElementNodeTemplates).hasSize(4);
189 List<ToscaNodeType> derivedTypes = getDerivedNodeTypes(serviceTemplate);
190 for (ToscaNodeTemplate template : controlLoopElementNodeTemplates) {
191 assertTrue(checkNodeType(template, derivedTypes));
196 private boolean checkNodeType(ToscaNodeTemplate template, List<ToscaNodeType> derivedNodeTypes) {
197 String controlLoopElementType = "org.onap.policy.clamp.controlloop.ControlLoopElement";
198 for (ToscaNodeType derivedType : derivedNodeTypes) {
199 if (template.getType().equals(derivedType.getName()) || template.getType().equals(controlLoopElementType)) {
206 private List<ToscaNodeType> getDerivedNodeTypes(ToscaServiceTemplate serviceTemplate) {
207 String type = "org.onap.policy.clamp.controlloop.ControlLoopElement";
208 List<ToscaNodeType> nodeTypes = new ArrayList<>();
209 for (ToscaNodeType nodeType : serviceTemplate.getNodeTypes().values()) {
210 if (nodeType.getDerivedFrom().equals(type)) {
211 nodeTypes.add(nodeType);