blob: b33aacf9bb50abd537966ea121fb38bf9d76109f [file] [log] [blame]
Aharoni, Pavel (pa0916)ca3cb012018-10-22 15:29:57 +03001/*-
2 * ============LICENSE_START=======================================================
3 * OSAM
4 * ================================================================================
5 * Copyright (C) 2018 AT&T
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
22
23package org.onap.osam.aai.util;
24
25import org.junit.Before;
26import org.junit.Test;
27import org.junit.runner.RunWith;
28import org.junit.runners.Parameterized;
29import org.mockito.Mock;
30import org.mockito.Mockito;
31import org.mockito.MockitoAnnotations;
32import org.onap.osam.aai.exceptions.InvalidPropertyException;
33import org.testng.Assert;
34
35import javax.servlet.http.HttpServletRequest;
36import javax.ws.rs.client.Client;
37import javax.ws.rs.client.Invocation;
38import javax.ws.rs.client.WebTarget;
39import javax.ws.rs.core.Response;
40import java.io.UnsupportedEncodingException;
41import java.util.Arrays;
42import java.util.Collection;
43import java.util.Optional;
44import java.util.UUID;
45
46import static javax.ws.rs.core.Response.Status.NO_CONTENT;
47import static javax.ws.rs.core.Response.Status.OK;
48import static org.mockito.Mockito.verify;
49import static org.mockito.Mockito.when;
50
51@RunWith(Parameterized.class)
52public class ParametrizedAAIRestInterfaceTest {
53
54 private static final String PATH = "path";
55 private static final String HTTP_LOCALHOST = "http://localhost/";
56 @Mock
57 private Client client;
58 @Mock
59 private WebTarget webTarget;
60 @Mock
61 private Invocation.Builder builder;
62 @Mock
63 private ServletRequestHelper servletRequestHelper;
64 @Mock
65 private HttpsAuthClient httpsAuthClient;
66 @Mock
67 private HttpServletRequest httpServletRequest;
68 @Mock
69 private Response response;
70 @Mock
71 private SystemPropertyHelper systemPropertyHelper;
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);
99 }
100
101 @Test
102 public void testRestDeleteWithValidResponse() throws Exception {
103 // given
104 String methodName = "Delete";
105
106 // when
107 when(builder.delete()).thenReturn(response);
108 when(response.getStatusInfo()).thenReturn(status);
109 boolean finalResponse = testSubject.Delete("", "", PATH);
110
111 // then
112 verify(builder).delete();
113 Assert.assertTrue(finalResponse);
114 }
115
116 private void mockSystemProperties() throws UnsupportedEncodingException, InvalidPropertyException {
117 when(systemPropertyHelper.getAAIServerUrl()).thenReturn(Optional.of(HTTP_LOCALHOST));
118 when(systemPropertyHelper.getAAIUseClientCert()).thenReturn(Optional.of("cert"));
119 when(systemPropertyHelper.getAAIVIDPasswd()).thenReturn(Optional.of("passwd"));
120 when(systemPropertyHelper.getAAIVIDUsername()).thenReturn(Optional.of("user"));
121 when(systemPropertyHelper.getEncodedCredentials()).thenReturn("someCredentials");
122 when(systemPropertyHelper.getFullServicePath(Mockito.anyString())).thenReturn("http://localhost/path");
123 }
124
125}