Aharoni, Pavel (pa0916) | ca3cb01 | 2018-10-22 15:29:57 +0300 | [diff] [blame] | 1 | /*-
|
| 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 |
|
| 23 | package org.onap.osam.aai.util;
|
| 24 |
|
| 25 | import org.junit.Before;
|
| 26 | import org.junit.Test;
|
| 27 | import org.junit.runner.RunWith;
|
| 28 | import org.junit.runners.Parameterized;
|
| 29 | import org.mockito.Mock;
|
| 30 | import org.mockito.Mockito;
|
| 31 | import org.mockito.MockitoAnnotations;
|
| 32 | import org.onap.osam.aai.exceptions.InvalidPropertyException;
|
| 33 | import org.testng.Assert;
|
| 34 |
|
| 35 | import javax.servlet.http.HttpServletRequest;
|
| 36 | import javax.ws.rs.client.Client;
|
| 37 | import javax.ws.rs.client.Invocation;
|
| 38 | import javax.ws.rs.client.WebTarget;
|
| 39 | import javax.ws.rs.core.Response;
|
| 40 | import java.io.UnsupportedEncodingException;
|
| 41 | import java.util.Arrays;
|
| 42 | import java.util.Collection;
|
| 43 | import java.util.Optional;
|
| 44 | import java.util.UUID;
|
| 45 |
|
| 46 | import static javax.ws.rs.core.Response.Status.NO_CONTENT;
|
| 47 | import static javax.ws.rs.core.Response.Status.OK;
|
| 48 | import static org.mockito.Mockito.verify;
|
| 49 | import static org.mockito.Mockito.when;
|
| 50 |
|
| 51 | @RunWith(Parameterized.class)
|
| 52 | public 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 | }
|