Commit dd6f5eb7 authored by Greg Messner's avatar Greg Messner
Browse files

Mods to use concrete FakeResponse instead of a Mockito Spy FakeResponse (#370).

parent 40ec8d29
package org.gitlab4j.api;
import java.io.ByteArrayInputStream;
import java.lang.annotation.Annotation;
import java.net.URI;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import javax.ws.rs.core.EntityTag;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.Link;
import javax.ws.rs.core.Link.Builder;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.NewCookie;
import javax.ws.rs.core.Response;
/**
* This class can be used as a spy for Mockito to test the individual APIs
* This class can be used as a concrete mock to test the individual APIs
* getXxxxx() methods without the need to have a GitLab server available.
*
* Supports getXxxxx() methods that return a List of items, single items,
* Optional items, and Pagers of items.
*/
public abstract class FakeResponse extends Response {
public class FakeResponse extends Response {
private List<?> responseList;
private Object responseItem;
......@@ -51,8 +63,10 @@ public abstract class FakeResponse extends Response {
this.perPage = perPage;
}
// The below methods allow this abstract class to act as a fake Resource
// instance.
/******************************************************************************
* The following methods allow this class to act as a fake Resource instance.
******************************************************************************/
@SuppressWarnings("unchecked")
@Override
......@@ -93,4 +107,115 @@ public abstract class FakeResponse extends Response {
public int getStatus() {
return (200);
}
/**************************************************************************************************
* The remaining methods are stubbed so we can create an instance of this class. They are simply
* stubs, but needed to do this because the Mockito Spy annotation does not work without JAXB
* on Java 11+ and did not wish to pull in the JAXB module even for testing.
**************************************************************************************************/
@Override
public StatusType getStatusInfo() {
return null;
}
@Override
public <T> T readEntity(Class<T> entityType, Annotation[] annotations) {
return null;
}
@Override
public <T> T readEntity(GenericType<T> entityType, Annotation[] annotations) {
return null;
}
@Override
public boolean hasEntity() {
return false;
}
@Override
public boolean bufferEntity() {
return false;
}
@Override
public void close() {
}
@Override
public MediaType getMediaType() {
return null;
}
@Override
public Locale getLanguage() {
return null;
}
@Override
public int getLength() {
return 0;
}
@Override
public Set<String> getAllowedMethods() {
return null;
}
@Override
public Map<String, NewCookie> getCookies() {
return null;
}
@Override
public EntityTag getEntityTag() {
return null;
}
@Override
public Date getDate() {
return null;
}
@Override
public Date getLastModified() {
return null;
}
@Override
public URI getLocation() {
return null;
}
@Override
public Set<Link> getLinks() {
return null;
}
@Override
public boolean hasLink(String relation) {
return false;
}
@Override
public Link getLink(String relation) {
return null;
}
@Override
public Builder getLinkBuilder(String relation) {
return null;
}
@Override
public MultivaluedMap<String, Object> getMetadata() {
return null;
}
@Override
public MultivaluedMap<String, String> getStringHeaders() {
return null;
}
}
\ No newline at end of file
......@@ -20,15 +20,14 @@ import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.Spy;
public class TestCommitDiscussionsApi implements Constants {
private static final String COMMIT_SHA = "abcdef1234567890";
@Mock private GitLabApi gitLabApi;
@Mock private GitLabApiClient gitLabApiClient;
@Spy private FakeResponse response;
@Captor private ArgumentCaptor<MultivaluedMap<String, String>> attributeCaptor;
private FakeResponse response = new FakeResponse();
@Before
public void setUp() throws Exception {
......
......@@ -20,14 +20,13 @@ import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.Spy;
public class TestEpicDiscussionsApi implements Constants {
@Mock private GitLabApi gitLabApi;
@Mock private GitLabApiClient gitLabApiClient;
@Spy private FakeResponse response;
@Captor private ArgumentCaptor<MultivaluedMap<String, String>> attributeCaptor;
private FakeResponse response = new FakeResponse();
@Before
public void setUp() throws Exception {
......
......@@ -20,14 +20,13 @@ import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.Spy;
public class TestIssueDiscussionsApi implements Constants {
@Mock private GitLabApi gitLabApi;
@Mock private GitLabApiClient gitLabApiClient;
@Spy private FakeResponse response;
@Captor private ArgumentCaptor<MultivaluedMap<String, String>> attributeCaptor;
private FakeResponse response = new FakeResponse();
@Before
public void setUp() throws Exception {
......
package org.gitlab4j.api;
import static org.hamcrest.Matchers.hasEntry;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;
import java.util.Collections;
import javax.ws.rs.core.MultivaluedMap;
import org.gitlab4j.api.models.MergeRequest;
import org.junit.Before;
import org.junit.Test;
......@@ -8,38 +19,23 @@ import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.Mockito;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import java.util.Collections;
import static org.hamcrest.Matchers.hasEntry;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;
public class TestMergeRequestApi {
@Mock private GitLabApi gitLabApi;
@Mock private GitLabApiClient gitLabApiClient;
@Mock private Response response;
@Captor private ArgumentCaptor<MultivaluedMap<String, String>> attributeCaptor;
private FakeResponse response = new FakeResponse();
@SuppressWarnings("deprecation")
@Before
public void setUp() throws Exception {
initMocks(this);
response.init(MergeRequest.class, "merge-request.json", null);
when(gitLabApi.getApiClient()).thenReturn(gitLabApiClient);
when(gitLabApiClient.validateSecretToken(any())).thenReturn(true);
when(gitLabApiClient.put(attributeCaptor.capture(), Mockito.<Object>anyVararg()))
.thenReturn(response);
when(response.getStatus()).thenReturn(200);
when(response.getEntity()).thenReturn(new MergeRequest());
}
@Test
......
......@@ -20,19 +20,18 @@ import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.Spy;
public class TestMergeRequestDiscussionsApi implements Constants {
@Mock private GitLabApi gitLabApi;
@Mock private GitLabApiClient gitLabApiClient;
@Spy private FakeResponse response;
@Captor private ArgumentCaptor<MultivaluedMap<String, String>> attributeCaptor;
private FakeResponse response = new FakeResponse();
@Before
public void setUp() throws Exception {
initMocks(this);
response.init(Discussion.class, null, "merge-request-discussions.json");
response.init(Discussion.class, null, "merge-request-discussions.json");
when(gitLabApi.getApiClient()).thenReturn(gitLabApiClient);
when(gitLabApiClient.validateSecretToken(any())).thenReturn(true);
when(gitLabApiClient.get(attributeCaptor.capture(), Mockito.<Object>any())).thenReturn(response);
......
......@@ -20,14 +20,13 @@ import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.Spy;
public class TestSnippetDiscussionsApi implements Constants {
@Mock private GitLabApi gitLabApi;
@Mock private GitLabApiClient gitLabApiClient;
@Spy private FakeResponse response;
@Captor private ArgumentCaptor<MultivaluedMap<String, String>> attributeCaptor;
private FakeResponse response = new FakeResponse();
@Before
public void setUp() throws Exception {
......
package org.gitlab4j.api;
import static java.util.Comparator.comparing;
import static java.util.stream.Collectors.toList;
import static org.gitlab4j.api.JsonUtils.compareJson;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
......@@ -10,8 +11,6 @@ import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;
import static java.util.stream.Collectors.toList;
import java.util.List;
import java.util.stream.Stream;
......@@ -25,14 +24,13 @@ import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.Spy;
public class TestStreams implements Constants {
@Mock private GitLabApi gitLabApi;
@Mock private GitLabApiClient gitLabApiClient;
@Spy private FakeResponse response;
@Captor private ArgumentCaptor<MultivaluedMap<String, String>> attributeCaptor;
private FakeResponse response = new FakeResponse();
static private List<User> sortedUsers;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment