make Logging a service and inject it to SyncRestClient
[vid.git] / vid-app-common / src / test / java / org / onap / vid / aai / util / ParametrizedAAIRestInterfaceTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.vid.aai.util;
22
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;
27
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;
48
49 @RunWith(Parameterized.class)
50 public class ParametrizedAAIRestInterfaceTest {
51
52     private static final String PATH = "path";
53     private static final String HTTP_LOCALHOST = "http://localhost/";
54     @Mock
55     private Client client;
56     @Mock
57     private WebTarget webTarget;
58     @Mock
59     private Invocation.Builder builder;
60     @Mock
61     private ServletRequestHelper servletRequestHelper;
62     @Mock
63     private HttpsAuthClient httpsAuthClient;
64     @Mock
65     private HttpServletRequest httpServletRequest;
66     @Mock
67     private Response response;
68     @Mock
69     private SystemPropertyHelper systemPropertyHelper;
70     @Mock
71     private Logging loggingService;
72
73     private AAIRestInterface testSubject;
74     private Response.Status status;
75
76     @Parameterized.Parameters
77     public static Collection<Object> data() {
78         return Arrays.asList(OK, NO_CONTENT);
79     }
80
81     @Before
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());
91     }
92
93     public ParametrizedAAIRestInterfaceTest(Response.Status status) {
94         this.status = status;
95     }
96
97     private AAIRestInterface createTestSubject() {
98         return new AAIRestInterface(Optional.of(client), httpsAuthClient, servletRequestHelper, systemPropertyHelper, loggingService);
99     }
100
101     @Test
102     public void testRestDeleteWithValidResponse() {
103
104         // when
105         when(builder.delete()).thenReturn(response);
106         when(response.getStatusInfo()).thenReturn(status);
107         boolean finalResponse = testSubject.Delete("", "", PATH);
108
109         // then
110         verify(builder).delete();
111         Assert.assertTrue(finalResponse);
112     }
113
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");
121     }
122
123 }