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 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.utils.CommonTestRestController;
40 import org.onap.policy.api.main.service.ToscaServiceTemplateService;
41 import org.onap.policy.common.utils.security.SelfSignedKeyStore;
42 import org.onap.policy.models.errors.concepts.ErrorResponse;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
44 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
45 import org.springframework.beans.factory.annotation.Autowired;
46 import org.springframework.boot.test.context.SpringBootTest;
47 import org.springframework.boot.test.web.server.LocalServerPort;
48 import org.springframework.test.annotation.DirtiesContext;
49 import org.springframework.test.context.ActiveProfiles;
50 import org.springframework.test.context.DynamicPropertyRegistry;
51 import org.springframework.test.context.DynamicPropertySource;
52 import org.springframework.test.context.junit4.SpringRunner;
55 * Class to perform unit test of {@link NodeTemplateController}.
58 @RunWith(SpringRunner.class)
59 @SpringBootTest(classes = PolicyApiApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
60 @ActiveProfiles({ "test", "default" })
61 @DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
62 public class TestNodeTemplateController extends CommonTestRestController {
64 private static final String TOSCA_NODE_TEMPLATE_RESOURCE =
65 "nodetemplates/nodetemplates.metadatasets.input.tosca.json";
66 private static final String TOSCA_INVALID_NODE_TYPE =
67 "nodetemplates/nodetemplates.metadatasets.invalid.nodetype.json";
68 private static final String TOSCA_INVALID_TEMPLATE =
69 "nodetemplates/nodetemplates.metadatasets.no.nodetemplate.json";
70 private static final String TOSCA_UPDATE_NODE_TEMPLATES = "nodetemplates/nodetemplates.metadatasets.update.json";
72 private static final String NODE_TEMPLATES = "nodetemplates";
73 private static final String SPECIFIC_NODE_TEMPLATE = "nodetemplates/apexMetadata_adaptive/versions/1.0.0";
74 private static final String INVALID_NODE_TEMPLATE_ID = "nodetemplates/invalid_template/versions/1.0.0";
76 private static final List<String> nodeTemplateKeys =
77 List.of("apexMetadata_grpc", "apexMetadata_adaptive", "apexMetadata_decisionMaker");
79 protected static final String APP_JSON = "application/json";
81 private static SelfSignedKeyStore keystore;
87 private ToscaServiceTemplateService toscaServiceTemplateService;
90 * Initializes parameters and set up test environment.
92 * @throws IOException on I/O exceptions
93 * @throws InterruptedException if interrupted
96 public static void setupParameters() throws IOException, InterruptedException {
97 keystore = new SelfSignedKeyStore();
101 * Clean up the database.
105 public void clearDb() {
106 for (String name : nodeTemplateKeys) {
108 toscaServiceTemplateService.deleteToscaNodeTemplate(name, "1.0.0");
109 } catch (Exception e) {
115 @DynamicPropertySource
116 static void registerPgProperties(DynamicPropertyRegistry registry) {
117 registry.add("server.ssl.enabled", () -> "true");
118 registry.add("server.ssl.key-store", () -> keystore.getKeystoreName());
119 registry.add("server.ssl.key-store-password", () -> SelfSignedKeyStore.KEYSTORE_PASSWORD);
120 registry.add("server.ssl.key-store-type", () -> "PKCS12");
121 registry.add("server.ssl.key-alias", () -> "policy@policy.onap.org");
122 registry.add("server.ssl.key-password", () -> SelfSignedKeyStore.PRIVATE_KEY_PASSWORD);
127 public void testCreateToscaNodeTemplates() throws Exception {
128 Response rawResponse = createResource(NODE_TEMPLATES, TOSCA_NODE_TEMPLATE_RESOURCE, apiPort);
129 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
130 ToscaServiceTemplate response = rawResponse.readEntity(ToscaServiceTemplate.class);
131 assertNotNull(response);
132 assertFalse(response.getToscaTopologyTemplate().getNodeTemplates().isEmpty());
134 // Send a node type with a invalid value to trigger an error
135 rawResponse = createResource(NODE_TEMPLATES, TOSCA_INVALID_NODE_TYPE, apiPort);
136 assertEquals(Response.Status.NOT_ACCEPTABLE.getStatusCode(), rawResponse.getStatus());
137 ErrorResponse response2 = rawResponse.readEntity(ErrorResponse.class);
138 assertThat(response2.getErrorMessage())
139 .containsPattern("^NODE_TYPE .* for toscaNodeTemplate .* does not exist$");
141 // Send invalid tosca template with no node templates
142 rawResponse = createResource(NODE_TEMPLATES, TOSCA_INVALID_TEMPLATE, apiPort);
143 assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawResponse.getStatus());
144 response2 = rawResponse.readEntity(ErrorResponse.class);
145 assertEquals("node templates not present on the service template", response2.getErrorMessage());
150 public void testReadNodeTemplates() throws Exception {
151 Response rawResponse = readResource(NODE_TEMPLATES, APP_JSON, apiPort);
152 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
153 List<?> nodeTemplates = rawResponse.readEntity(List.class);
154 assertNotNull(nodeTemplates);
155 assertEquals(0, nodeTemplates.size());
157 createResource(NODE_TEMPLATES, TOSCA_NODE_TEMPLATE_RESOURCE, apiPort);
158 rawResponse = readResource(NODE_TEMPLATES, APP_JSON, apiPort);
159 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
160 nodeTemplates = rawResponse.readEntity(List.class);
161 assertNotNull(nodeTemplates);
162 assertEquals(3, nodeTemplates.size());
164 rawResponse = readResource(SPECIFIC_NODE_TEMPLATE, APP_JSON, apiPort);
165 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
166 List<ToscaNodeTemplate> retrievedTemplate =
167 rawResponse.readEntity(new GenericType<List<ToscaNodeTemplate>>() {});
168 assertNotNull(nodeTemplates);
169 assertEquals(1, retrievedTemplate.size());
170 String retrievedTemplateName = retrievedTemplate.get(0).getName();
171 assertEquals("apexMetadata_adaptive", retrievedTemplateName);
175 public void testUpdateNodeTemplates() throws Exception {
176 createResource(NODE_TEMPLATES, TOSCA_NODE_TEMPLATE_RESOURCE, apiPort);
177 Response rawResponse = updateResource(NODE_TEMPLATES, TOSCA_UPDATE_NODE_TEMPLATES, APP_JSON, apiPort);
178 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
179 ToscaServiceTemplate response = rawResponse.readEntity(ToscaServiceTemplate.class);
180 assertNotNull(response);
181 assertFalse(response.getToscaTopologyTemplate().getNodeTemplates().isEmpty());
182 String updatedValue = "" + response.getToscaTopologyTemplate().getNodeTemplates().get("apexMetadata_grpc")
183 .getMetadata().get("state");
184 assertEquals("passive", updatedValue);
186 rawResponse = updateResource(NODE_TEMPLATES, TOSCA_INVALID_NODE_TYPE, APP_JSON, apiPort);
187 assertEquals(Response.Status.NOT_ACCEPTABLE.getStatusCode(), rawResponse.getStatus());
188 ErrorResponse response2 = rawResponse.readEntity(ErrorResponse.class);
189 assertThat(response2.getErrorMessage())
190 .containsPattern("^NODE_TYPE .* for toscaNodeTemplate .* does not exist$");
192 // Send invalid tosca template with no node templates
193 rawResponse = updateResource(NODE_TEMPLATES, TOSCA_INVALID_TEMPLATE, APP_JSON, apiPort);
194 assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawResponse.getStatus());
195 ErrorResponse response3 = rawResponse.readEntity(ErrorResponse.class);
196 assertEquals("node templates not present on the service template", response3.getErrorMessage());
200 public void testDeleteNodeTemplates() throws Exception {
201 createResource(NODE_TEMPLATES, TOSCA_NODE_TEMPLATE_RESOURCE, apiPort);
202 Response rawResponse = deleteResource(SPECIFIC_NODE_TEMPLATE, APP_JSON, apiPort);
203 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
204 ToscaServiceTemplate response = rawResponse.readEntity(ToscaServiceTemplate.class);
205 assertNotNull(response);
206 assertFalse(response.getToscaTopologyTemplate().getNodeTemplates().isEmpty());
208 rawResponse = readResource(NODE_TEMPLATES, APP_JSON, apiPort);
209 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
210 List<?> nodeTemplates = rawResponse.readEntity(List.class);
211 assertNotNull(nodeTemplates);
212 assertEquals(2, nodeTemplates.size());
215 rawResponse = deleteResource(INVALID_NODE_TEMPLATE_ID, APP_JSON, apiPort);
216 assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawResponse.getStatus());
217 ErrorResponse response3 = rawResponse.readEntity(ErrorResponse.class);
218 assertThat(response3.getErrorMessage()).containsPattern("^node template .* not found$");