aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/test/java/org/onap/policy/api/main/rest/TestNodeTemplateController.java
blob: 993fbee16a1ae45240a6ccd6ae8e956914653b8e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*-
 * ============LICENSE_START=======================================================
 * ONAP Policy API
 * ================================================================================
 * Copyright (C) 2022 Nordix Foundation. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.api.main.rest;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;

import java.io.IOException;
import java.util.List;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.Response;
import org.junit.After;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.onap.policy.api.main.PolicyApiApplication;
import org.onap.policy.api.main.rest.utils.CommonTestRestController;
import org.onap.policy.api.main.service.ToscaServiceTemplateService;
import org.onap.policy.common.utils.security.SelfSignedKeyStore;
import org.onap.policy.models.errors.concepts.ErrorResponse;
import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.springframework.test.context.junit4.SpringRunner;

/**
 * Class to perform unit test of {@link NodeTemplateController}.
 *
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = PolicyApiApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles("test")
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
public class TestNodeTemplateController extends CommonTestRestController {

    private static final String TOSCA_NODE_TEMPLATE_RESOURCE =
        "nodetemplates/nodetemplates.metadatasets.input.tosca.json";
    private static final String TOSCA_INVALID_NODE_TYPE =
        "nodetemplates/nodetemplates.metadatasets.invalid.nodetype.json";
    private static final String TOSCA_INVALID_TEMPLATE =
        "nodetemplates/nodetemplates.metadatasets.no.nodetemplate.json";
    private static final String TOSCA_UPDATE_NODE_TEMPLATES = "nodetemplates/nodetemplates.metadatasets.update.json";

    private static final String NODE_TEMPLATES = "nodetemplates";
    private static final String SPECIFIC_NODE_TEMPLATE = "nodetemplates/apexMetadata_adaptive/versions/1.0.0";
    private static final String INVALID_NODE_TEMPLATE_ID = "nodetemplates/invalid_template/versions/1.0.0";

    private static final List<String> nodeTemplateKeys =
        List.of("apexMetadata_grpc", "apexMetadata_adaptive", "apexMetadata_decisionMaker");

    protected static final String APP_JSON = "application/json";

    private static SelfSignedKeyStore keystore;

    @LocalServerPort
    private int apiPort;

    @Autowired
    private ToscaServiceTemplateService toscaServiceTemplateService;

    /**
     * Initializes parameters and set up test environment.
     *
     * @throws IOException on I/O exceptions
     * @throws InterruptedException if interrupted
     */
    @BeforeClass
    public static void setupParameters() throws IOException, InterruptedException {
        keystore = new SelfSignedKeyStore();
    }

    /**
     * Clean up the database.
     *
     */
    @After
    public void clearDb() {
        for (String name : nodeTemplateKeys) {
            try {
                toscaServiceTemplateService.deleteToscaNodeTemplate(name, "1.0.0");
            } catch (Exception e) {
                //do nothing
            }
        }
    }

    @DynamicPropertySource
    static void registerPgProperties(DynamicPropertyRegistry registry) {
        registry.add("server.ssl.enabled", () -> "true");
        registry.add("server.ssl.key-store", () -> keystore.getKeystoreName());
        registry.add("server.ssl.key-store-password", () -> SelfSignedKeyStore.KEYSTORE_PASSWORD);
        registry.add("server.ssl.key-store-type", () -> "PKCS12");
        registry.add("server.ssl.key-alias", () -> "policy@policy.onap.org");
        registry.add("server.ssl.key-password", () -> SelfSignedKeyStore.PRIVATE_KEY_PASSWORD);
    }


    @Test
    public void testCreateToscaNodeTemplates() throws Exception {
        Response rawResponse = createResource(NODE_TEMPLATES, TOSCA_NODE_TEMPLATE_RESOURCE, apiPort);
        assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
        ToscaServiceTemplate response = rawResponse.readEntity(ToscaServiceTemplate.class);
        assertNotNull(response);
        assertFalse(response.getToscaTopologyTemplate().getNodeTemplates().isEmpty());

        // Send a node type with a invalid value to trigger an error
        rawResponse = createResource(NODE_TEMPLATES, TOSCA_INVALID_NODE_TYPE, apiPort);
        assertEquals(Response.Status.NOT_ACCEPTABLE.getStatusCode(), rawResponse.getStatus());
        ErrorResponse response2 = rawResponse.readEntity(ErrorResponse.class);
        assertThat(response2.getErrorMessage())
            .containsPattern("^NODE_TYPE .* for toscaNodeTemplate .* does not exist$");

        // Send invalid tosca template with no node templates
        rawResponse = createResource(NODE_TEMPLATES, TOSCA_INVALID_TEMPLATE, apiPort);
        assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawResponse.getStatus());
        response2 = rawResponse.readEntity(ErrorResponse.class);
        assertEquals("node templates not present on the service template", response2.getErrorMessage());
    }


    @Test
    public void testReadNodeTemplates() throws Exception {
        Response rawResponse = readResource(NODE_TEMPLATES, APP_JSON, apiPort);
        assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
        List<?> nodeTemplates = rawResponse.readEntity(List.class);
        assertNotNull(nodeTemplates);
        assertEquals(0, nodeTemplates.size());

        createResource(NODE_TEMPLATES, TOSCA_NODE_TEMPLATE_RESOURCE, apiPort);
        rawResponse = readResource(NODE_TEMPLATES, APP_JSON, apiPort);
        assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
        nodeTemplates = rawResponse.readEntity(List.class);
        assertNotNull(nodeTemplates);
        assertEquals(3, nodeTemplates.size());

        rawResponse = readResource(SPECIFIC_NODE_TEMPLATE, APP_JSON, apiPort);
        assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
        List<ToscaNodeTemplate> retrievedTemplate =
            rawResponse.readEntity(new GenericType<List<ToscaNodeTemplate>>() {});
        assertNotNull(nodeTemplates);
        assertEquals(1, retrievedTemplate.size());
        String retrievedTemplateName = retrievedTemplate.get(0).getName();
        assertEquals("apexMetadata_adaptive", retrievedTemplateName);
    }

    @Test
    public void testUpdateNodeTemplates() throws Exception {
        createResource(NODE_TEMPLATES, TOSCA_NODE_TEMPLATE_RESOURCE, apiPort);
        Response rawResponse = updateResource(NODE_TEMPLATES, TOSCA_UPDATE_NODE_TEMPLATES, APP_JSON, apiPort);
        assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
        ToscaServiceTemplate response = rawResponse.readEntity(ToscaServiceTemplate.class);
        assertNotNull(response);
        assertFalse(response.getToscaTopologyTemplate().getNodeTemplates().isEmpty());
        String updatedValue = "" + response.getToscaTopologyTemplate().getNodeTemplates().get("apexMetadata_grpc")
            .getMetadata().get("state");
        assertEquals("passive", updatedValue);

        rawResponse = updateResource(NODE_TEMPLATES, TOSCA_INVALID_NODE_TYPE, APP_JSON, apiPort);
        assertEquals(Response.Status.NOT_ACCEPTABLE.getStatusCode(), rawResponse.getStatus());
        ErrorResponse response2 = rawResponse.readEntity(ErrorResponse.class);
        assertThat(response2.getErrorMessage())
            .containsPattern("^NODE_TYPE .* for toscaNodeTemplate .* does not exist$");

        // Send invalid tosca template with no node templates
        rawResponse = updateResource(NODE_TEMPLATES, TOSCA_INVALID_TEMPLATE, APP_JSON, apiPort);
        assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawResponse.getStatus());
        ErrorResponse response3 = rawResponse.readEntity(ErrorResponse.class);
        assertEquals("node templates not present on the service template", response3.getErrorMessage());
    }

    @Test
    public void testDeleteNodeTemplates() throws Exception {
        createResource(NODE_TEMPLATES, TOSCA_NODE_TEMPLATE_RESOURCE, apiPort);
        Response rawResponse = deleteResource(SPECIFIC_NODE_TEMPLATE, APP_JSON, apiPort);
        assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
        ToscaServiceTemplate response = rawResponse.readEntity(ToscaServiceTemplate.class);
        assertNotNull(response);
        assertFalse(response.getToscaTopologyTemplate().getNodeTemplates().isEmpty());

        rawResponse = readResource(NODE_TEMPLATES, APP_JSON, apiPort);
        assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
        List<?> nodeTemplates = rawResponse.readEntity(List.class);
        assertNotNull(nodeTemplates);
        assertEquals(2, nodeTemplates.size());

        // Send invalid id
        rawResponse = deleteResource(INVALID_NODE_TEMPLATE_ID, APP_JSON, apiPort);
        assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawResponse.getStatus());
        ErrorResponse response3 = rawResponse.readEntity(ErrorResponse.class);
        assertThat(response3.getErrorMessage()).containsPattern("^node template .* not found$");
    }

}