diff options
author | Sonsino, Ofir (os0695) <os0695@intl.att.com> | 2018-07-10 14:20:54 +0300 |
---|---|---|
committer | Sonsino, Ofir (os0695) <os0695@intl.att.com> | 2018-07-10 14:20:54 +0300 |
commit | c72d565bb58226b20625b2bce5f0019046bee649 (patch) | |
tree | 8658e49595705b02e47ddc14afa20d6bb7123547 /vid-app-common/src/test/java/org/onap/vid/testUtils | |
parent | ef8a6b47847012fd59ea20da21d8d3d7c4a301ed (diff) |
Merge 1806 code of vid-common
Change-Id: I75d52abed4a24dfe3827d79edc4a2938726aa87a
Issue-ID: VID-208
Signed-off-by: Sonsino, Ofir (os0695) <os0695@intl.att.com>
Diffstat (limited to 'vid-app-common/src/test/java/org/onap/vid/testUtils')
-rw-r--r-- | vid-app-common/src/test/java/org/onap/vid/testUtils/TestUtils.java | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/vid-app-common/src/test/java/org/onap/vid/testUtils/TestUtils.java b/vid-app-common/src/test/java/org/onap/vid/testUtils/TestUtils.java index 73a79cd2..175f87f2 100644 --- a/vid-app-common/src/test/java/org/onap/vid/testUtils/TestUtils.java +++ b/vid-app-common/src/test/java/org/onap/vid/testUtils/TestUtils.java @@ -1,12 +1,30 @@ package org.onap.vid.testUtils; +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.common.collect.ImmutableList; import org.json.JSONArray; import org.json.JSONObject; import org.junit.Assert; +import org.mockito.MockSettings; +import org.mockito.Mockito; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; +import org.onap.vid.asdc.beans.Service; +import javax.ws.rs.core.GenericType; +import javax.ws.rs.core.Response; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.Serializable; import java.util.Iterator; +import java.util.List; import static fj.parser.Parser.fail; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.*; /** * Created by Oren on 6/7/17. @@ -64,5 +82,81 @@ public class TestUtils { } } + public static <T> T readJsonResourceFileAsObject(String pathInResource, Class<T> valueType) throws IOException { + return readJsonResourceFileAsObject(pathInResource, valueType, false); + } + + public static <T> T readJsonResourceFileAsObject(String pathInResource, Class<T> valueType, boolean ignoreUnknownProperties) + throws IOException { + ObjectMapper objectMapper = new ObjectMapper(); + objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, ignoreUnknownProperties); + return objectMapper.readValue( + TestUtils.class.getResource(pathInResource), + valueType); + } + + + public static class JavaxRsClientMocks { + private final javax.ws.rs.client.Client fakeClient; + private final javax.ws.rs.client.Invocation.Builder fakeBuilder; + private final Response fakeResponse; + + public javax.ws.rs.client.Client getFakeClient() { + return fakeClient; + } + + public javax.ws.rs.client.Invocation.Builder getFakeBuilder() { + return fakeBuilder; + } + + public Response getFakeResponse() { + return fakeResponse; + } + + public JavaxRsClientMocks() { + final MockSettings mockSettings = withSettings().defaultAnswer(new TriesToReturnMockByType()); + fakeClient = mock(javax.ws.rs.client.Client.class, mockSettings); + fakeBuilder = mock(javax.ws.rs.client.Invocation.Builder.class, mockSettings); + fakeResponse = mock(Response.class, mockSettings); + final javax.ws.rs.client.WebTarget fakeWebTarget = mock(javax.ws.rs.client.WebTarget.class, mockSettings); + + TriesToReturnMockByType.setAvailableMocks( + fakeClient, + fakeWebTarget, + fakeBuilder, + fakeResponse + ); + + Mockito.when(fakeBuilder.get(any(Class.class))).thenReturn(null); + Mockito.when(fakeBuilder.get(eq(InputStream.class))).thenReturn(new ByteArrayInputStream(new byte[]{})); + Mockito.when(fakeBuilder.get(any(GenericType.class))).thenReturn(null); + + Mockito.when(fakeResponse.getStatus()).thenReturn(200); + Mockito.when(fakeResponse.readEntity(Service.class)).thenReturn(null); + } + } + + /* + inspired out from newer Mockito version + returns a mock from given list if it's a matching return-type + */ + private static class TriesToReturnMockByType implements Answer<Object>, Serializable { + private final Answer<Object> defaultReturn = RETURNS_DEFAULTS; + private static List<Object> availableMocks = ImmutableList.of(); + + static void setAvailableMocks(Object... mocks) { + availableMocks = ImmutableList.copyOf(mocks); + } + + public Object answer(InvocationOnMock invocation) throws Throwable { + Class<?> methodReturnType = invocation.getMethod().getReturnType(); + + return availableMocks.stream() + .filter(mock -> methodReturnType.isAssignableFrom(mock.getClass())) + //.peek(m -> System.out.println("found a mock: " + m.getClass().getName())) + .findFirst() + .orElse(defaultReturn.answer(invocation)); + } + } } |