aboutsummaryrefslogtreecommitdiffstats
path: root/src/test/java/org/onap/aai/babel/service/TestGenerateArtifactsServiceImpl.java
blob: 5ceca17e039ae47ac37953ce4357b9351afa60d7 (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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/**
 * ============LICENSE_START=======================================================
 * org.onap.aai
 * ================================================================================
 * Copyright (c) 2017-2019 AT&T Intellectual Property. All rights reserved.
 * Copyright (c) 2017-2019 European Software Marketing Ltd.
 * ================================================================================
 * 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.
 * ============LICENSE_END=========================================================
 */

package org.onap.aai.babel.service;

import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

import com.google.gson.Gson;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.security.cert.X509Certificate;
import java.util.Collections;
import java.util.List;
import java.util.Map.Entry;
import java.util.Optional;
import javax.inject.Inject;
import javax.security.auth.x500.X500Principal;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MultivaluedHashMap;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.onap.aai.auth.AAIAuthException;
import org.onap.aai.auth.AAIMicroServiceAuth;
import org.onap.aai.babel.service.data.BabelRequest;
import org.onap.aai.babel.testdata.CsarTest;
import org.onap.aai.babel.util.ArtifactTestUtils;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;

/**
 * Direct invocation of the generate artifacts service implementation.
 *
 */
@SpringJUnitConfig(locations = {"classpath:/babel-beans.xml"})
public class TestGenerateArtifactsServiceImpl {

    static {
        System.setProperty("CONFIG_HOME", "src/test/resources");
    }

    @Inject
    private AAIMicroServiceAuth auth;

    @BeforeAll
    public static void setup() {
        new ArtifactTestUtils().setGeneratorSystemProperties();
    }

    /**
     * Test with a valid request (and valid CSAR content) by calling the Service implementation directly using a mocked
     * HTTPS request.
     *
     * @throws URISyntaxException
     *             if the URI cannot be created
     * @throws IOException
     *             if the resource cannot be loaded
     */
    @Test
    public void testGenerateArtifacts() throws URISyntaxException, IOException {
        Response response = processJsonRequest(CsarTest.VNF_VENDOR_CSAR, auth);
        assertThat(response.toString(), response.getStatus(), is(Response.Status.OK.getStatusCode()));
        assertThat(response.getEntity(), is(getResponseJson("response.json")));
    }

    /**
     * Test with a valid request that has no Transaction ID header value.
     *
     * @throws URISyntaxException
     *             if the URI cannot be created
     * @throws IOException
     *             if the resource cannot be loaded
     */
    @Test
    public void testGenerateArtifactsWithoutRequestId() throws URISyntaxException, IOException {
        Response response = invokeService(CsarTest.VNF_VENDOR_CSAR.getJsonRequest(), Optional.empty(), auth);
        assertThat(response.toString(), response.getStatus(), is(Response.Status.OK.getStatusCode()));
        assertThat(response.getEntity(), is(getResponseJson("response.json")));
    }
    
    /**
     * Test with a valid request without Minor Artifact version.
     *
     * @throws URISyntaxException
     *             if the URI cannot be created
     * @throws IOException
     *             if the resource cannot be loaded
     */
    @Test
    public void testGenerateArtifactsWithoutMinorArtifactVersion() throws URISyntaxException, IOException {
        Response response = invokeService(CsarTest.VNF_VENDOR_CSAR.getJsonRequestWithArtifactVersion("1"),
        		Optional.of("transaction-id"), auth);
        assertThat(response.toString(), response.getStatus(), is(Response.Status.OK.getStatusCode()));
        assertThat(response.getEntity(), is(getResponseJson("response.json")));
    }
    
    /**
     * Test with a valid request without Minor Artifact version.
     *
     * @throws URISyntaxException
     *             if the URI cannot be created
     * @throws IOException
     *             if the resource cannot be loaded
     */
    @Test
    public void testGenerateArtifactsWithInvalidArtifactVersion() throws URISyntaxException, IOException {
        Response response = invokeService(CsarTest.VNF_VENDOR_CSAR.getJsonRequestWithArtifactVersion("a"),
        		Optional.of("transaction-id"), auth);
        assertThat(response.toString(), response.getStatus(), is(Response.Status.OK.getStatusCode()));
        assertThat(response.getEntity(), is(getResponseJson("response.json")));
    }
    
    
    /**
     * Test with a valid request with Artifact version less than 1.
     *
     * @throws URISyntaxException
     *             if the URI cannot be created
     * @throws IOException
     *             if the resource cannot be loaded
     */
    @Test
    public void testGenerateArtifactsWithArtifactVerLessThan1() throws URISyntaxException, IOException {
        Response response = invokeService(CsarTest.VNF_VENDOR_CSAR.getJsonRequestWithArtifactVersion("0.1"),
        		Optional.of("transaction-id"), auth);
        assertThat(response.toString(), response.getStatus(), is(Response.Status.OK.getStatusCode()));
        assertThat(response.getEntity(), is(getResponseJson("responseWithVersionLessThan1.json")));
    }


    /**
     * Test with a valid request, using a CSAR file that has no VNF configuration present.
     *
     * @throws URISyntaxException
     *             if the URI cannot be created
     * @throws IOException
     *             if the resource cannot be loaded
     */
    @Test
    public void testGenerateArtifactsWithoutVnfConfiguration() throws IOException, URISyntaxException {
        Response response = processJsonRequest(CsarTest.NO_VNF_CONFIG_CSAR, auth);
        assertThat(response.getStatus(), is(Response.Status.OK.getStatusCode()));
        assertThat(response.getEntity(), is(getResponseJson("validNoVnfConfigurationResponse.json")));
    }

    /**
     * Test for a valid request with invalid CSAR file content.
     *
     * @throws URISyntaxException
     *             if the URI cannot be created
     * @throws IOException
     *             if the resource cannot be loaded
     */
    @Test
    public void testGenerateArtifactsInvalidCsar() throws IOException, URISyntaxException {
        Response response = processJsonRequest(CsarTest.MULTIPLE_VNF_CSAR, auth);
        assertThat(response.getStatus(), is(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()));
        assertThat(response.getEntity().toString(), containsString("VNF catalog"));
    }

    @Test
    public void testUninitializedService() throws IOException, URISyntaxException, AAIAuthException {
        AAIMicroServiceAuth uninitializedAuth = Mockito.mock(AAIMicroServiceAuth.class);
        Mockito.when(uninitializedAuth.validateRequest(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any()))
                .thenThrow(new AAIAuthException("test"));
        Response response = processJsonRequest(CsarTest.NO_VNF_CONFIG_CSAR, uninitializedAuth);
        assertThat(response.getStatus(), is(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()));
        assertThat(response.getEntity().toString(), containsString("check the Babel service logs"));
    }

    @Test
    public void testUnauthorizedRequest() throws IOException, URISyntaxException, AAIAuthException {
        AAIMicroServiceAuth uninitializedAuth = Mockito.mock(AAIMicroServiceAuth.class);
        Mockito.when(uninitializedAuth.validateRequest(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any()))
                .thenReturn(false);
        Response response = processJsonRequest(CsarTest.NO_VNF_CONFIG_CSAR, uninitializedAuth);
        assertThat(response.getStatus(), is(Response.Status.UNAUTHORIZED.getStatusCode()));
        assertThat(response.getEntity().toString(), containsString("User not authorized"));
    }

    @Test
    public void testInvalidCsarFile() throws URISyntaxException, IOException {
        BabelRequest request = new BabelRequest();
        request.setArtifactName("hello");
        request.setArtifactVersion("1.0");
        request.setCsar("xxxx");
        Response response = invokeService(new Gson().toJson(request));
        assertThat(response.getStatus(), is(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()));
        assertThat(response.getEntity(), is("Error converting CSAR artifact to XML model."));
    }

    @Test
    public void testInvalidJsonFile() throws URISyntaxException, IOException {
        Response response = invokeService("{\"csar:\"xxxx\"");
        assertThat(response.getStatus(), is(Response.Status.BAD_REQUEST.getStatusCode()));
        assertThat(response.getEntity(), is("Malformed request."));
    }

    @Test
    public void testMissingArtifactName() throws Exception {
        BabelRequest request = new BabelRequest();
        request.setArtifactVersion("1.0");
        request.setCsar("");
        Response response = invokeService(new Gson().toJson(request));
        assertThat(response.getStatus(), is(Response.Status.BAD_REQUEST.getStatusCode()));
        assertThat(response.getEntity(), is("No artifact name attribute found in the request body."));
    }

    @Test
    public void testMissingArtifactVersion() throws Exception {
        BabelRequest request = new BabelRequest();
        request.setArtifactName("hello");
        request.setCsar("");
        Response response = invokeService(new Gson().toJson(request));
        assertThat(response.getStatus(), is(Response.Status.BAD_REQUEST.getStatusCode()));
        assertThat(response.getEntity(), is("No artifact version attribute found in the request body."));
    }

    @Test
    public void testMissingCsarFile() throws Exception {
        BabelRequest request = new BabelRequest();
        request.setArtifactName("test-name");
        request.setArtifactVersion("1.0");
        Response response = invokeService(new Gson().toJson(request));
        assertThat(response.getStatus(), is(Response.Status.BAD_REQUEST.getStatusCode()));
        assertThat(response.getEntity(), is("No csar attribute found in the request body."));
    }

    /**
     * Create a (mocked) HTTPS request and invoke the Babel generate artifacts API.
     *
     * @param csar
     *            test CSAR file
     * @param auth
     *            the auth module
     * @return the Response from the HTTP API
     * @throws URISyntaxException
     *             if the URI cannot be created
     * @throws IOException
     *             if the resource cannot be loaded
     */
    private Response processJsonRequest(CsarTest csar, AAIMicroServiceAuth auth)
            throws URISyntaxException, IOException {
        return invokeService(csar.getJsonRequest(), Optional.of("transaction-id"), auth);
    }

    /**
     * Create a (mocked) HTTPS request and invoke the Babel generate artifacts API.
     *
     * @param jsonString
     *            the JSON request
     * @return the Response from the HTTP API
     * @throws URISyntaxException
     *             if the URI cannot be created
     */
    private Response invokeService(String jsonRequest) throws URISyntaxException {
        return invokeService(jsonRequest, Optional.of("transaction-id"), auth);
    }

    /**
     * Create a (mocked) HTTPS request and invoke the Babel generate artifacts API.
     *
     * @param jsonString
     *            the JSON request
     * @param transactionId
     *            optional X-TransactionId value for the HTTP request
     * @param auth
     *            the auth module
     * @return the Response from the HTTP API
     * @throws URISyntaxException
     *             if the URI cannot be created
     */
    private Response invokeService(String jsonString, Optional<String> transactionId, AAIMicroServiceAuth auth)
            throws URISyntaxException {
        UriInfo mockUriInfo = Mockito.mock(UriInfo.class);
        Mockito.when(mockUriInfo.getRequestUri()).thenReturn(new URI("/validate")); // NOSONAR (mocked)
        Mockito.when(mockUriInfo.getPath(false)).thenReturn("validate"); // URI prefix is stripped by AJSC routing
        Mockito.when(mockUriInfo.getPathParameters()).thenReturn(new MultivaluedHashMap<String, String>());

        // Create mocked request headers map
        MultivaluedHashMap<String, String> headersMap = new MultivaluedHashMap<>();
        if (transactionId.isPresent()) {
            headersMap.put("X-TransactionId", createSingletonList(transactionId.get()));
        }
        headersMap.put("X-FromAppId", createSingletonList("app-id"));
        headersMap.put("Host", createSingletonList("hostname"));

        HttpHeaders headers = Mockito.mock(HttpHeaders.class);
        for (Entry<String, List<String>> entry : headersMap.entrySet()) {
            Mockito.when(headers.getRequestHeader(entry.getKey())).thenReturn(entry.getValue());
            Mockito.when(headers.getHeaderString(entry.getKey())).thenReturn(entry.getValue().get(0));
        }
        Mockito.when(headers.getRequestHeaders()).thenReturn(headersMap);

        MockHttpServletRequest servletRequest = new MockHttpServletRequest();
        servletRequest.setSecure(true);
        servletRequest.setScheme("https");
        servletRequest.setServerPort(9501);
        servletRequest.setServerName("localhost");
        servletRequest.setRequestURI("/services/validation-service/v1/app/validate");

        X509Certificate mockCertificate = Mockito.mock(X509Certificate.class);
        Mockito.when(mockCertificate.getSubjectX500Principal())
                .thenReturn(new X500Principal("CN=test, OU=qa, O=Test Ltd, L=London, ST=London, C=GB"));

        servletRequest.setAttribute("javax.servlet.request.X509Certificate", new X509Certificate[] {mockCertificate});
        servletRequest.setAttribute("javax.servlet.request.cipher_suite", "");

        GenerateArtifactsServiceImpl service = new GenerateArtifactsServiceImpl(auth);
        return service.generateArtifacts(mockUriInfo, headers, servletRequest, jsonString);
    }

    private String getResponseJson(String jsonResponse) throws IOException, URISyntaxException {
        return new ArtifactTestUtils().getResponseJson(jsonResponse);
    }

    private List<String> createSingletonList(String listItem) {
        return Collections.<String>singletonList(listItem);
    }

}