2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 - 2019 Nokia. 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.
18 * ============LICENSE_END=========================================================
21 package org.onap.vid.aai.util;
23 import static javax.ws.rs.core.Response.Status.NO_CONTENT;
24 import static javax.ws.rs.core.Response.Status.OK;
25 import static org.mockito.Mockito.verify;
26 import static org.mockito.Mockito.when;
28 import java.io.UnsupportedEncodingException;
29 import java.util.Arrays;
30 import java.util.Collection;
31 import java.util.Optional;
32 import java.util.UUID;
33 import javax.servlet.http.HttpServletRequest;
34 import javax.ws.rs.client.Client;
35 import javax.ws.rs.client.Invocation;
36 import javax.ws.rs.client.WebTarget;
37 import javax.ws.rs.core.Response;
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 import org.junit.runners.Parameterized;
42 import org.mockito.Mock;
43 import org.mockito.Mockito;
44 import org.mockito.MockitoAnnotations;
45 import org.onap.vid.aai.exceptions.InvalidPropertyException;
46 import org.onap.vid.utils.Logging;
47 import org.testng.Assert;
49 @RunWith(Parameterized.class)
50 public class ParametrizedAAIRestInterfaceTest {
52 private static final String PATH = "path";
53 private static final String HTTP_LOCALHOST = "http://localhost/";
55 private Client client;
57 private WebTarget webTarget;
59 private Invocation.Builder builder;
61 private ServletRequestHelper servletRequestHelper;
63 private HttpsAuthClient httpsAuthClient;
65 private HttpServletRequest httpServletRequest;
67 private Response response;
69 private SystemPropertyHelper systemPropertyHelper;
71 private Logging loggingService;
73 private AAIRestInterface testSubject;
74 private Response.Status status;
76 @Parameterized.Parameters
77 public static Collection<Object> data() {
78 return Arrays.asList(OK, NO_CONTENT);
82 public void setUp() throws Exception {
83 MockitoAnnotations.initMocks(this);
84 mockSystemProperties();
85 testSubject = createTestSubject();
86 when(client.target(HTTP_LOCALHOST+PATH)).thenReturn(webTarget);
87 when(webTarget.request()).thenReturn(builder);
88 when(builder.accept(Mockito.anyString())).thenReturn(builder);
89 when(builder.header(Mockito.anyString(), Mockito.anyString())).thenReturn(builder);
90 when(servletRequestHelper.extractOrGenerateRequestId()).thenReturn(UUID.randomUUID().toString());
93 public ParametrizedAAIRestInterfaceTest(Response.Status status) {
97 private AAIRestInterface createTestSubject() {
98 return new AAIRestInterface(Optional.of(client), httpsAuthClient, servletRequestHelper, systemPropertyHelper, loggingService);
102 public void testRestDeleteWithValidResponse() {
105 when(builder.delete()).thenReturn(response);
106 when(response.getStatusInfo()).thenReturn(status);
107 boolean finalResponse = testSubject.Delete("", "", PATH);
110 verify(builder).delete();
111 Assert.assertTrue(finalResponse);
114 private void mockSystemProperties() throws UnsupportedEncodingException, InvalidPropertyException {
115 when(systemPropertyHelper.getAAIServerUrl()).thenReturn(Optional.of(HTTP_LOCALHOST));
116 when(systemPropertyHelper.getAAIUseClientCert()).thenReturn(Optional.of("cert"));
117 when(systemPropertyHelper.getAAIVIDPasswd()).thenReturn(Optional.of("passwd"));
118 when(systemPropertyHelper.getAAIVIDUsername()).thenReturn(Optional.of("user"));
119 when(systemPropertyHelper.getEncodedCredentials()).thenReturn("someCredentials");
120 when(systemPropertyHelper.getFullServicePath(Mockito.anyString())).thenReturn("http://localhost/path");