blob: 854cdbbd4d5d2221ed81ffab76faa1d0f781c725 [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.testUtils;
24
25import com.fasterxml.jackson.databind.DeserializationFeature;
26import com.fasterxml.jackson.databind.ObjectMapper;
27import com.google.common.collect.ImmutableList;
28import org.json.JSONArray;
29import org.json.JSONObject;
30import org.junit.Assert;
31import org.mockito.MockSettings;
32import org.mockito.Mockito;
33import org.mockito.invocation.InvocationOnMock;
34import org.mockito.stubbing.Answer;
35
36import javax.ws.rs.core.GenericType;
37import javax.ws.rs.core.Response;
38import java.io.ByteArrayInputStream;
39import java.io.IOException;
40import java.io.InputStream;
41import java.io.Serializable;
42import java.util.Iterator;
43import java.util.List;
44
Aharoni, Pavel (pa0916)ca3cb012018-10-22 15:29:57 +030045import static org.mockito.Matchers.any;
46import static org.mockito.Matchers.eq;
47import static org.mockito.Mockito.*;
48
49public class TestUtils {
50
51 public static void assertJsonStringEqualsIgnoreNulls(String expected, String actual) {
52 if (expected == null || expected == JSONObject.NULL) {return;}
53
54 JSONObject expectedJSON = new JSONObject(expected);
55 JSONObject actualJSON = new JSONObject(actual);
56 Iterator<?> keys = expectedJSON.keys();
57
58 while( keys.hasNext() ) {
59 String key = (String)keys.next();
60 Object expectedValue = expectedJSON.get(key);
61 if (expectedValue == JSONObject.NULL){
62 continue;
63 }
64
65 Object actualValue = actualJSON.get(key);
66
67 if (expectedValue instanceof JSONObject) {
68 String expectedVal = expectedValue.toString();
69 String actualVal = actualValue.toString();
70 assertJsonStringEqualsIgnoreNulls(expectedVal, actualVal);
71 }
72 else if (expectedValue instanceof JSONArray) {
73 if (actualValue instanceof JSONArray) {
74 JSONArray expectedJSONArray = (JSONArray)expectedValue;
75 JSONArray actualJSONArray = (JSONArray)actualValue;
76 for (int i = 0; i < expectedJSONArray.length(); i++) {
77 String expectedItem = expectedJSONArray.get(i).toString();
78 String actualItem = actualJSONArray.get(i).toString();
79 if (expectedValue instanceof JSONObject)
80 assertJsonStringEqualsIgnoreNulls(expectedItem, actualItem);
81 }
82 }
83 else {
Aharoni, Pavel (pa0916)d2946182018-12-17 11:21:02 +020084 Assert.fail("expected: " + expectedValue + " got:" + actualValue);
Aharoni, Pavel (pa0916)ca3cb012018-10-22 15:29:57 +030085 }
86 }
87 else {
88 Assert.assertEquals(expectedValue, actualValue);
89 }
90 }
91 }
92
93 public static <T> T readJsonResourceFileAsObject(String pathInResource, Class<T> valueType) throws IOException {
94 return readJsonResourceFileAsObject(pathInResource, valueType, false);
95 }
96
97 public static <T> T readJsonResourceFileAsObject(String pathInResource, Class<T> valueType, boolean ignoreUnknownProperties)
98 throws IOException {
99 ObjectMapper objectMapper = new ObjectMapper();
100 objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, ignoreUnknownProperties);
101 return objectMapper.readValue(
102 TestUtils.class.getResource(pathInResource),
103 valueType);
104 }
105
106
107 public static class JavaxRsClientMocks {
108 private final javax.ws.rs.client.Client fakeClient;
109 private final javax.ws.rs.client.Invocation.Builder fakeBuilder;
110 private final Response fakeResponse;
111
112 public javax.ws.rs.client.Client getFakeClient() {
113 return fakeClient;
114 }
115
116 public javax.ws.rs.client.Invocation.Builder getFakeBuilder() {
117 return fakeBuilder;
118 }
119
120 public Response getFakeResponse() {
121 return fakeResponse;
122 }
123
124 public JavaxRsClientMocks() {
125 final MockSettings mockSettings = withSettings().defaultAnswer(new TriesToReturnMockByType());
126
127 fakeClient = mock(javax.ws.rs.client.Client.class, mockSettings);
128 fakeBuilder = mock(javax.ws.rs.client.Invocation.Builder.class, mockSettings);
129 fakeResponse = mock(Response.class, mockSettings);
130 final javax.ws.rs.client.WebTarget fakeWebTarget = mock(javax.ws.rs.client.WebTarget.class, mockSettings);
131
132 TriesToReturnMockByType.setAvailableMocks(
133 fakeClient,
134 fakeWebTarget,
135 fakeBuilder,
136 fakeResponse
137 );
138
139 Mockito.when(fakeBuilder.get(any(Class.class))).thenReturn(null);
140 Mockito.when(fakeBuilder.get(eq(InputStream.class))).thenReturn(new ByteArrayInputStream(new byte[]{}));
141 Mockito.when(fakeBuilder.get(any(GenericType.class))).thenReturn(null);
142
143 Mockito.when(fakeResponse.getStatus()).thenReturn(200);
144 Mockito.when(fakeResponse.getStatusInfo()).thenReturn(Response.Status.OK);
145 }
146 }
147
148 /*
149 inspired out from newer Mockito version
150 returns a mock from given list if it's a matching return-type
151 */
152 private static class TriesToReturnMockByType implements Answer<Object>, Serializable {
153 private final Answer<Object> defaultReturn = RETURNS_DEFAULTS;
154 private static List<Object> availableMocks = ImmutableList.of();
155
156 static void setAvailableMocks(Object... mocks) {
157 availableMocks = ImmutableList.copyOf(mocks);
158 }
159
160 public Object answer(InvocationOnMock invocation) throws Throwable {
161 Class<?> methodReturnType = invocation.getMethod().getReturnType();
162
163 return availableMocks.stream()
164 .filter(mock -> methodReturnType.isAssignableFrom(mock.getClass()))
165 //.peek(m -> System.out.println("found a mock: " + m.getClass().getName()))
166 .findFirst()
167 .orElse(defaultReturn.answer(invocation));
168 }
169 }
170}