2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2022-2023 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 jakarta.ws.rs.core.GenericType;
31 import jakarta.ws.rs.core.Response;
32 import java.io.IOException;
33 import java.util.List;
34 import org.junit.jupiter.api.AfterEach;
35 import org.junit.jupiter.api.BeforeAll;
36 import org.junit.jupiter.api.Test;
37 import org.onap.policy.api.main.PolicyApiApplication;
38 import org.onap.policy.api.main.rest.utils.CommonTestRestController;
39 import org.onap.policy.api.main.service.ToscaServiceTemplateService;
40 import org.onap.policy.common.utils.security.SelfSignedKeyStore;
41 import org.onap.policy.models.errors.concepts.ErrorResponse;
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
44 import org.springframework.beans.factory.annotation.Autowired;
45 import org.springframework.boot.test.context.SpringBootTest;
46 import org.springframework.boot.test.web.server.LocalServerPort;
47 import org.springframework.test.annotation.DirtiesContext;
48 import org.springframework.test.context.ActiveProfiles;
49 import org.springframework.test.context.DynamicPropertyRegistry;
50 import org.springframework.test.context.DynamicPropertySource;
53 * Class to perform unit test of {@link NodeTemplateController}.
56 @SpringBootTest(classes = PolicyApiApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
57 @ActiveProfiles({ "test", "default" })
58 @DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
59 public class TestNodeTemplateController extends CommonTestRestController {
61 private static final String TOSCA_NODE_TEMPLATE_RESOURCE =
62 "nodetemplates/nodetemplates.metadatasets.input.tosca.json";
63 private static final String TOSCA_INVALID_NODE_TYPE =
64 "nodetemplates/nodetemplates.metadatasets.invalid.nodetype.json";
65 private static final String TOSCA_INVALID_TEMPLATE =
66 "nodetemplates/nodetemplates.metadatasets.no.nodetemplate.json";
67 private static final String TOSCA_UPDATE_NODE_TEMPLATES = "nodetemplates/nodetemplates.metadatasets.update.json";
69 private static final String NODE_TEMPLATES = "nodetemplates";
70 private static final String SPECIFIC_NODE_TEMPLATE = "nodetemplates/apexMetadata_adaptive/versions/1.0.0";
71 private static final String INVALID_NODE_TEMPLATE_ID = "nodetemplates/invalid_template/versions/1.0.0";
73 private static final List<String> nodeTemplateKeys =
74 List.of("apexMetadata_grpc", "apexMetadata_adaptive", "apexMetadata_decisionMaker");
76 protected static final String APP_JSON = "application/json";
78 private static SelfSignedKeyStore keystore;
84 private ToscaServiceTemplateService toscaServiceTemplateService;
87 * Initializes parameters and set up test environment.
89 * @throws IOException on I/O exceptions
90 * @throws InterruptedException if interrupted
93 public static void setupParameters() throws IOException, InterruptedException {
94 keystore = new SelfSignedKeyStore();
98 * Clean up the database.
102 public void clearDb() {
103 for (String name : nodeTemplateKeys) {
105 toscaServiceTemplateService.deleteToscaNodeTemplate(name, "1.0.0");
106 } catch (Exception e) {
112 @DynamicPropertySource
113 static void registerPgProperties(DynamicPropertyRegistry registry) {
114 registry.add("server.ssl.enabled", () -> "true");
115 registry.add("server.ssl.key-store", () -> keystore.getKeystoreName());
116 registry.add("server.ssl.key-store-password", () -> SelfSignedKeyStore.KEYSTORE_PASSWORD);
117 registry.add("server.ssl.key-store-type", () -> "PKCS12");
118 registry.add("server.ssl.key-alias", () -> "policy@policy.onap.org");
119 registry.add("server.ssl.key-password", () -> SelfSignedKeyStore.PRIVATE_KEY_PASSWORD);
124 void testCreateToscaNodeTemplates() throws Exception {
125 Response rawResponse = createResource(NODE_TEMPLATES, TOSCA_NODE_TEMPLATE_RESOURCE, apiPort);
126 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
127 ToscaServiceTemplate response = rawResponse.readEntity(ToscaServiceTemplate.class);
128 assertNotNull(response);
129 assertFalse(response.getToscaTopologyTemplate().getNodeTemplates().isEmpty());
131 // Send a node type with a invalid value to trigger an error
132 rawResponse = createResource(NODE_TEMPLATES, TOSCA_INVALID_NODE_TYPE, apiPort);
133 assertEquals(Response.Status.NOT_ACCEPTABLE.getStatusCode(), rawResponse.getStatus());
134 ErrorResponse response2 = rawResponse.readEntity(ErrorResponse.class);
135 assertThat(response2.getErrorMessage())
136 .containsPattern("^NODE_TYPE .* for toscaNodeTemplate .* does not exist$");
138 // Send invalid tosca template with no node templates
139 rawResponse = createResource(NODE_TEMPLATES, TOSCA_INVALID_TEMPLATE, apiPort);
140 assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawResponse.getStatus());
141 response2 = rawResponse.readEntity(ErrorResponse.class);
142 assertEquals("node templates not present on the service template", response2.getErrorMessage());
147 void testReadNodeTemplates() throws Exception {
148 Response rawResponse = readResource(NODE_TEMPLATES, APP_JSON, apiPort);
149 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
150 List<?> nodeTemplates = rawResponse.readEntity(List.class);
151 assertNotNull(nodeTemplates);
152 assertEquals(0, nodeTemplates.size());
154 createResource(NODE_TEMPLATES, TOSCA_NODE_TEMPLATE_RESOURCE, apiPort);
155 rawResponse = readResource(NODE_TEMPLATES, APP_JSON, apiPort);
156 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
157 nodeTemplates = rawResponse.readEntity(List.class);
158 assertNotNull(nodeTemplates);
159 assertEquals(3, nodeTemplates.size());
161 rawResponse = readResource(SPECIFIC_NODE_TEMPLATE, APP_JSON, apiPort);
162 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
163 List<ToscaNodeTemplate> retrievedTemplate =
164 rawResponse.readEntity(new GenericType<List<ToscaNodeTemplate>>() {});
165 assertNotNull(nodeTemplates);
166 assertEquals(1, retrievedTemplate.size());
167 String retrievedTemplateName = retrievedTemplate.get(0).getName();
168 assertEquals("apexMetadata_adaptive", retrievedTemplateName);
172 void testUpdateNodeTemplates() throws Exception {
173 createResource(NODE_TEMPLATES, TOSCA_NODE_TEMPLATE_RESOURCE, apiPort);
174 Response rawResponse = updateResource(NODE_TEMPLATES, TOSCA_UPDATE_NODE_TEMPLATES, APP_JSON, apiPort);
175 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
176 ToscaServiceTemplate response = rawResponse.readEntity(ToscaServiceTemplate.class);
177 assertNotNull(response);
178 assertFalse(response.getToscaTopologyTemplate().getNodeTemplates().isEmpty());
179 String updatedValue = "" + response.getToscaTopologyTemplate().getNodeTemplates().get("apexMetadata_grpc")
180 .getMetadata().get("state");
181 assertEquals("passive", updatedValue);
183 rawResponse = updateResource(NODE_TEMPLATES, TOSCA_INVALID_NODE_TYPE, APP_JSON, apiPort);
184 assertEquals(Response.Status.NOT_ACCEPTABLE.getStatusCode(), rawResponse.getStatus());
185 ErrorResponse response2 = rawResponse.readEntity(ErrorResponse.class);
186 assertThat(response2.getErrorMessage())
187 .containsPattern("^NODE_TYPE .* for toscaNodeTemplate .* does not exist$");
189 // Send invalid tosca template with no node templates
190 rawResponse = updateResource(NODE_TEMPLATES, TOSCA_INVALID_TEMPLATE, APP_JSON, apiPort);
191 assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawResponse.getStatus());
192 ErrorResponse response3 = rawResponse.readEntity(ErrorResponse.class);
193 assertEquals("node templates not present on the service template", response3.getErrorMessage());
197 void testDeleteNodeTemplates() throws Exception {
198 createResource(NODE_TEMPLATES, TOSCA_NODE_TEMPLATE_RESOURCE, apiPort);
199 Response rawResponse = deleteResource(SPECIFIC_NODE_TEMPLATE, APP_JSON, apiPort);
200 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
201 ToscaServiceTemplate response = rawResponse.readEntity(ToscaServiceTemplate.class);
202 assertNotNull(response);
203 assertFalse(response.getToscaTopologyTemplate().getNodeTemplates().isEmpty());
205 rawResponse = readResource(NODE_TEMPLATES, APP_JSON, apiPort);
206 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
207 List<?> nodeTemplates = rawResponse.readEntity(List.class);
208 assertNotNull(nodeTemplates);
209 assertEquals(2, nodeTemplates.size());
212 rawResponse = deleteResource(INVALID_NODE_TEMPLATE_ID, APP_JSON, apiPort);
213 assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawResponse.getStatus());
214 ErrorResponse response3 = rawResponse.readEntity(ErrorResponse.class);
215 assertThat(response3.getErrorMessage()).containsPattern("^node template .* not found$");