2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2022 Nordix Foundation. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.api.main.rest;
25 import static org.assertj.core.api.Assertions.assertThat;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertFalse;
28 import static org.junit.Assert.assertNotNull;
30 import java.io.IOException;
31 import java.util.List;
32 import javax.ws.rs.core.GenericType;
33 import javax.ws.rs.core.Response;
34 import org.junit.After;
35 import org.junit.BeforeClass;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.onap.policy.api.main.PolicyApiApplication;
39 import org.onap.policy.api.main.rest.provider.NodeTemplateProvider;
40 import org.onap.policy.api.main.rest.utils.CommonTestRestController;
41 import org.onap.policy.common.utils.security.SelfSignedKeyStore;
42 import org.onap.policy.models.base.PfModelException;
43 import org.onap.policy.models.base.PfModelRuntimeException;
44 import org.onap.policy.models.errors.concepts.ErrorResponse;
45 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
46 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
47 import org.springframework.beans.factory.annotation.Autowired;
48 import org.springframework.boot.test.context.SpringBootTest;
49 import org.springframework.boot.web.server.LocalServerPort;
50 import org.springframework.test.annotation.DirtiesContext;
51 import org.springframework.test.context.ActiveProfiles;
52 import org.springframework.test.context.DynamicPropertyRegistry;
53 import org.springframework.test.context.DynamicPropertySource;
54 import org.springframework.test.context.junit4.SpringRunner;
57 * Class to perform unit test of {@link NodeTemplateController}.
60 @RunWith(SpringRunner.class)
61 @SpringBootTest(classes = PolicyApiApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
62 @ActiveProfiles("test")
63 @DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
64 public class TestNodeTemplateController extends CommonTestRestController {
66 private static final String TOSCA_NODE_TEMPLATE_RESOURCE =
67 "nodetemplates/nodetemplates.metadatasets.input.tosca.json";
68 private static final String TOSCA_INVALID_NODE_TYPE =
69 "nodetemplates/nodetemplates.metadatasets.invalid.nodetype.json";
70 private static final String TOSCA_INVALID_TEMPLATE =
71 "nodetemplates/nodetemplates.metadatasets.no.nodetemplate.json";
72 private static final String TOSCA_UPDATE_NODE_TEMPLATES = "nodetemplates/nodetemplates.metadatasets.update.json";
74 private static final String NODE_TEMPLATES = "nodetemplates";
75 private static final String SPECIFIC_NODE_TEMPLATE = "nodetemplates/apexMetadata_adaptive/versions/1.0.0";
76 private static final String INVALID_NODE_TEMPLATE_ID = "nodetemplates/invalid_template/versions/1.0.0";
78 private static final List<String> nodeTemplateKeys =
79 List.of("apexMetadata_grpc", "apexMetadata_adaptive", "apexMetadata_decisionMaker");
81 protected static final String APP_JSON = "application/json";
83 private static SelfSignedKeyStore keystore;
89 private NodeTemplateProvider provider;
92 * Initializes parameters and set up test environment.
94 * @throws IOException on I/O exceptions
95 * @throws InterruptedException if interrupted
98 public static void setupParameters() throws IOException, InterruptedException {
99 keystore = new SelfSignedKeyStore();
103 * Clean up the database.
107 public void clearDb() {
108 for (String name : nodeTemplateKeys) {
110 provider.deleteToscaNodeTemplate(name, "1.0.0");
111 } catch (PfModelException | PfModelRuntimeException e) {
117 @DynamicPropertySource
118 static void registerPgProperties(DynamicPropertyRegistry registry) {
119 registry.add("server.ssl.enabled", () -> "true");
120 registry.add("server.ssl.key-store", () -> keystore.getKeystoreName());
121 registry.add("server.ssl.key-store-password", () -> SelfSignedKeyStore.KEYSTORE_PASSWORD);
122 registry.add("server.ssl.key-store-type", () -> "PKCS12");
123 registry.add("server.ssl.key-alias", () -> "policy@policy.onap.org");
124 registry.add("server.ssl.key-password", () -> SelfSignedKeyStore.PRIVATE_KEY_PASSWORD);
129 public void testCreateToscaNodeTemplates() throws Exception {
130 Response rawResponse = createResource(NODE_TEMPLATES, TOSCA_NODE_TEMPLATE_RESOURCE, apiPort);
131 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
132 ToscaServiceTemplate response = rawResponse.readEntity(ToscaServiceTemplate.class);
133 assertNotNull(response);
134 assertFalse(response.getToscaTopologyTemplate().getNodeTemplates().isEmpty());
136 // Send a node type with a invalid value to trigger an error
137 rawResponse = createResource(NODE_TEMPLATES, TOSCA_INVALID_NODE_TYPE, apiPort);
138 assertEquals(Response.Status.NOT_ACCEPTABLE.getStatusCode(), rawResponse.getStatus());
139 ErrorResponse response2 = rawResponse.readEntity(ErrorResponse.class);
140 assertThat(response2.getErrorMessage())
141 .containsPattern("^NODE_TYPE .* for toscaNodeTemplate .* does not exist$");
143 // Send invalid tosca template with no node templates
144 rawResponse = createResource(NODE_TEMPLATES, TOSCA_INVALID_TEMPLATE, apiPort);
145 assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawResponse.getStatus());
146 response2 = rawResponse.readEntity(ErrorResponse.class);
147 assertEquals("node templates not present on the service template", response2.getErrorMessage());
152 public void testReadNodeTemplates() throws Exception {
153 Response rawResponse = readResource(NODE_TEMPLATES, APP_JSON, apiPort);
154 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
155 List<?> nodeTemplates = rawResponse.readEntity(List.class);
156 assertNotNull(nodeTemplates);
157 assertEquals(0, nodeTemplates.size());
159 createResource(NODE_TEMPLATES, TOSCA_NODE_TEMPLATE_RESOURCE, apiPort);
160 rawResponse = readResource(NODE_TEMPLATES, APP_JSON, apiPort);
161 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
162 nodeTemplates = rawResponse.readEntity(List.class);
163 assertNotNull(nodeTemplates);
164 assertEquals(3, nodeTemplates.size());
166 rawResponse = readResource(SPECIFIC_NODE_TEMPLATE, APP_JSON, apiPort);
167 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
168 List<ToscaNodeTemplate> retrievedTemplate =
169 rawResponse.readEntity(new GenericType<List<ToscaNodeTemplate>>() {});
170 assertNotNull(nodeTemplates);
171 assertEquals(1, retrievedTemplate.size());
172 String retrievedTemplateName = retrievedTemplate.get(0).getName();
173 assertEquals("apexMetadata_adaptive", retrievedTemplateName);
177 public void testUpdateNodeTemplates() throws Exception {
178 createResource(NODE_TEMPLATES, TOSCA_NODE_TEMPLATE_RESOURCE, apiPort);
179 Response rawResponse = updateResource(NODE_TEMPLATES, TOSCA_UPDATE_NODE_TEMPLATES, APP_JSON, apiPort);
180 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
181 ToscaServiceTemplate response = rawResponse.readEntity(ToscaServiceTemplate.class);
182 assertNotNull(response);
183 assertFalse(response.getToscaTopologyTemplate().getNodeTemplates().isEmpty());
184 String updatedValue = "" + response.getToscaTopologyTemplate().getNodeTemplates().get("apexMetadata_grpc")
185 .getMetadata().get("state");
186 assertEquals("passive", updatedValue);
188 rawResponse = updateResource(NODE_TEMPLATES, TOSCA_INVALID_NODE_TYPE, APP_JSON, apiPort);
189 assertEquals(Response.Status.NOT_ACCEPTABLE.getStatusCode(), rawResponse.getStatus());
190 ErrorResponse response2 = rawResponse.readEntity(ErrorResponse.class);
191 assertThat(response2.getErrorMessage())
192 .containsPattern("^NODE_TYPE .* for toscaNodeTemplate .* does not exist$");
194 // Send invalid tosca template with no node templates
195 rawResponse = updateResource(NODE_TEMPLATES, TOSCA_INVALID_TEMPLATE, APP_JSON, apiPort);
196 assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawResponse.getStatus());
197 ErrorResponse response3 = rawResponse.readEntity(ErrorResponse.class);
198 assertEquals("node templates not present on the service template", response3.getErrorMessage());
202 public void testDeleteNodeTemplates() throws Exception {
203 createResource(NODE_TEMPLATES, TOSCA_NODE_TEMPLATE_RESOURCE, apiPort);
204 Response rawResponse = deleteResource(SPECIFIC_NODE_TEMPLATE, APP_JSON, apiPort);
205 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
206 ToscaServiceTemplate response = rawResponse.readEntity(ToscaServiceTemplate.class);
207 assertNotNull(response);
208 assertFalse(response.getToscaTopologyTemplate().getNodeTemplates().isEmpty());
210 rawResponse = readResource(NODE_TEMPLATES, APP_JSON, apiPort);
211 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
212 List<?> nodeTemplates = rawResponse.readEntity(List.class);
213 assertNotNull(nodeTemplates);
214 assertEquals(2, nodeTemplates.size());
217 rawResponse = deleteResource(INVALID_NODE_TEMPLATE_ID, APP_JSON, apiPort);
218 assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawResponse.getStatus());
219 ErrorResponse response3 = rawResponse.readEntity(ErrorResponse.class);
220 assertThat(response3.getErrorMessage()).containsPattern("^node template .* not found$");