Skip to content

Commit

Permalink
increase coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
abuttaro committed Feb 11, 2018
1 parent d424064 commit 8d5b171
Show file tree
Hide file tree
Showing 15 changed files with 286 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public DefaultContentNegotiator(String mediaType, ContentNegotiator... negotiato
this(getContentNegotiator(mediaType, negotiators));
}

private static ContentNegotiator getContentNegotiator(String mediaType, ContentNegotiator[] negotiators) {
static ContentNegotiator getContentNegotiator(String mediaType, ContentNegotiator... negotiators) {
for (ContentNegotiator negotiator : negotiators) {
if (negotiator instanceof ContentTypeNegotiation) {
if (((ContentTypeNegotiation) negotiator).getContentType().equals(mediaType)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ public abstract class BasicResourceControllerRequest implements ResourceControll
private final String baseRequestUrl;
private final String requestUrl;

protected BasicResourceControllerRequest(HttpMethod method, Resource<?, ?> resource, List<?> ids, Resource<?, ?> relationship, ResourceRelationship<?, ?, ?, ?> resourceRelationship, ResourceData<?> body
protected BasicResourceControllerRequest(HttpMethod method, Resource<?, ?> resource, List<?> ids, Resource<?, ?> relationship,
ResourceRelationship<?, ?, ?, ?> resourceRelationship, ResourceData<?> body
, String contentType, String baseRequestUrl, String requestUrl) {
this.resource = resource;
this.ids = ids;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.github.restup.controller.settings;

import com.github.restup.controller.ExceptionHandler;
import com.github.restup.controller.content.negotiation.ContentNegotiator;
import com.github.restup.controller.interceptor.RequestInterceptor;
import com.github.restup.controller.request.parser.RequestParser;
import com.github.restup.registry.ResourceRegistry;

public class BasicControllerSettings implements ControllerSettings {

private final ResourceRegistry registry;
private final ContentNegotiator[] contentNegotiators;
private final RequestInterceptor requestInterceptor;
private final RequestParser requestParser;
private final ExceptionHandler exceptionHandler;
private final String defaultMediaType;

protected BasicControllerSettings(ResourceRegistry registry, ContentNegotiator[] contentNegotiators,
RequestInterceptor requestInterceptor, RequestParser requestParsers,
ExceptionHandler exceptionHandler, String defaultMediaType) {
super();
this.registry = registry;
this.contentNegotiators = contentNegotiators;
this.requestInterceptor = requestInterceptor;
this.requestParser = requestParsers;
this.exceptionHandler = exceptionHandler;
this.defaultMediaType = defaultMediaType;
}

@Override
public String getDefaultMediaType() {
return defaultMediaType;
}

@Override
public ResourceRegistry getRegistry() {
return registry;
}

@Override
public ContentNegotiator[] getContentNegotiators() {
return contentNegotiators;
}

@Override
public RequestInterceptor getRequestInterceptor() {
return requestInterceptor;
}

@Override
public RequestParser getRequestParser() {
return requestParser;
}

@Override
public ExceptionHandler getExceptionHandler() {
return exceptionHandler;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,56 +30,26 @@
import com.github.restup.service.registry.DiscoveryService;
import com.google.gson.Gson;

public class ControllerSettings {

private final ResourceRegistry registry;
private final ContentNegotiator[] contentNegotiators;
private final RequestInterceptor requestInterceptor;
private final RequestParser requestParser;
private final ExceptionHandler exceptionHandler;
private final String defaultMediaType;

protected ControllerSettings(ResourceRegistry registry, ContentNegotiator[] contentNegotiators,
RequestInterceptor requestInterceptor, RequestParser requestParsers,
ExceptionHandler exceptionHandler, String defaultMediaType) {
super();
this.registry = registry;
this.contentNegotiators = contentNegotiators;
this.requestInterceptor = requestInterceptor;
this.requestParser = requestParsers;
this.exceptionHandler = exceptionHandler;
this.defaultMediaType = defaultMediaType;
}
public interface ControllerSettings {


public static Builder builder() {
static Builder builder() {
return new Builder();
}

public String getDefaultMediaType() {
return defaultMediaType;
}
String getDefaultMediaType();

public ResourceRegistry getRegistry() {
return registry;
}
ResourceRegistry getRegistry();

public ContentNegotiator[] getContentNegotiators() {
return contentNegotiators;
}
ContentNegotiator[] getContentNegotiators();

public RequestInterceptor getRequestInterceptor() {
return requestInterceptor;
}
RequestInterceptor getRequestInterceptor();

public RequestParser getRequestParser() {
return requestParser;
}
RequestParser getRequestParser();

public ExceptionHandler getExceptionHandler() {
return exceptionHandler;
}
ExceptionHandler getExceptionHandler();

public static class Builder {
static class Builder {

private ResourceRegistry registry;
private ContentNegotiator[] contentNegotiator;
Expand Down Expand Up @@ -258,7 +228,7 @@ public ControllerSettings build() {
.id(String.class, "name")
)
);
return new ControllerSettings(registry, contentNegotiators, interceptor, requestParser, exceptionHandler, defaultMediaType);
return new BasicControllerSettings(registry, contentNegotiators, interceptor, requestParser, exceptionHandler, defaultMediaType);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,8 @@ public static RequestParser parser(ObjectMapper mapper, String defaultMediaType)
.build();
}

private JacksonConfiguration() {

}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.github.restup.jackson.parser;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand All @@ -9,9 +12,6 @@
import com.github.restup.errors.ErrorCode;
import com.github.restup.path.ResourcePath;
import com.github.restup.registry.Resource;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class JacksonRequestBodyParser extends AbstractRequestBodyParser<JsonNode> {

Expand All @@ -21,6 +21,10 @@ public JacksonRequestBodyParser(ObjectMapper mapper) {
this.mapper = mapper;
}

public JacksonRequestBodyParser() {
this(new ObjectMapper());
}

@Override
protected boolean isArray(JsonNode jsonNode) {
return jsonNode.isArray();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@


import org.junit.Test;
import com.github.restup.controller.model.ParsedResourceControllerRequest;
import com.github.restup.controller.model.result.NegotiatedResult;
import com.github.restup.test.assertions.Assertions;

public class PojoTest {
Expand All @@ -12,8 +10,9 @@ public class PojoTest {
public void testPojos() {
String regex = "Basic((?!Test$).)*$";
Assertions.pojo()
.addMatching(1, ParsedResourceControllerRequest.class, "BasicParsed")
.addMatching(3, NegotiatedResult.class, "((?!Negotiated).)*Result$")
.addMatching(1, "com.github.restup.controller.model", "BasicParsed")
.addMatching(3, "com.github.restup.controller.model.result", "((?!Negotiated).)*Result$")
.addMatching(1, "com.github.restup.controller.settings", regex)
.validate();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.github.restup.controller.content.negotiation;

import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import com.github.restup.controller.model.ParsedResourceControllerRequest;
import com.github.restup.controller.model.ResourceControllerRequest;
import com.github.restup.controller.model.ResourceControllerResponse;
import com.github.restup.controller.model.result.JsonApiResult;
import com.github.restup.test.assertions.Assertions;

@RunWith(MockitoJUnitRunner.class)
public class DefaultContentNegotiatorTest {

@Mock
ContentNegotiator contentNegotiator;

@Test
public void testAccept() {
DefaultContentNegotiator defaultContentNegotiator = new DefaultContentNegotiator(contentNegotiator);
ResourceControllerRequest request = mock(ResourceControllerRequest.class);
assertTrue(defaultContentNegotiator.accept(request));
verifyZeroInteractions(contentNegotiator, request);
}

@Test
public void testFormatResponse() {
DefaultContentNegotiator defaultContentNegotiator = new DefaultContentNegotiator(contentNegotiator);
ParsedResourceControllerRequest<?> request = mock(ParsedResourceControllerRequest.class);
ResourceControllerResponse response = mock(ResourceControllerResponse.class);
JsonApiResult result = mock(JsonApiResult.class);
defaultContentNegotiator.formatResponse(request, response, result);
verify(contentNegotiator).formatResponse(request, response, result);
verifyZeroInteractions(contentNegotiator, request);
}

@Test
public void testGetContentNegotiator() {
Assertions.assertThrows(() -> DefaultContentNegotiator.getContentNegotiator("text/foo", contentNegotiator), IllegalArgumentException.class);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.github.restup.controller.content.negotiation;

import static org.junit.Assert.assertSame;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.Collections;
import org.junit.Test;
import com.github.restup.controller.model.MediaType;
import com.github.restup.controller.model.ParsedResourceControllerRequest;
import com.github.restup.controller.model.ResourceControllerResponse;
import com.github.restup.controller.model.result.HalJsonResult;
import com.github.restup.registry.Resource;

public class HalJsonContentNegotiatorTest {


@Test
public void testFormatResponse() {
String foo = "foo";
ResourceControllerResponse response = mock(ResourceControllerResponse.class);

ParsedResourceControllerRequest<?> request = mock(ParsedResourceControllerRequest.class);
when(request.getRequestedQueries()).thenReturn(Collections.emptyList());
when(request.getResource()).thenReturn(mock(Resource.class));
HalJsonResult result = (HalJsonResult) new HalJsonContentNegotiator().formatResponse(request, response, foo);
assertSame(foo, result.getResult());
verify(response).setHeader(ContentTypeNegotiation.CONTENT_TYPE, MediaType.APPLICATION_JSON_HAL.getContentType());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.github.restup.controller.content.negotiation;

import static org.junit.Assert.assertSame;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.Collections;
import org.junit.Test;
import com.github.restup.controller.linking.LinkBuilderFactory;
import com.github.restup.controller.model.MediaType;
import com.github.restup.controller.model.ParsedResourceControllerRequest;
import com.github.restup.controller.model.ResourceControllerResponse;
import com.github.restup.controller.model.result.JsonApiResult;
import com.github.restup.registry.Resource;

public class JsonApiContentNegotiatorTest {


@Test
public void testFormatResponse() {
String foo = "foo";
ResourceControllerResponse response = mock(ResourceControllerResponse.class);

ParsedResourceControllerRequest<?> request = mock(ParsedResourceControllerRequest.class);
when(request.getRequestedQueries()).thenReturn(Collections.emptyList());
when(request.getResource()).thenReturn(mock(Resource.class));
JsonApiResult result = (JsonApiResult) new JsonApiContentNegotiator(mock(LinkBuilderFactory.class)).formatResponse(request, response, foo);
assertSame(foo, result.getResult());
verify(response).setHeader(ContentTypeNegotiation.CONTENT_TYPE, MediaType.APPLICATION_JSON_API.getContentType());
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.github.restup.controller.content.negotiation;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;

import com.github.restup.controller.model.ParsedResourceControllerRequest;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import com.github.restup.controller.model.ParsedResourceControllerRequest;

@RunWith(MockitoJUnitRunner.class)
public class NoOpContentNegotiatorTest {
Expand All @@ -20,4 +20,10 @@ public void testAccept() {
assertTrue(new NoOpContentNegotiator().accept(details));
assertFalse(new NoOpContentNegotiator().accept(null));
}

@Test
public void testFormatResponse() {
String foo = "foo";
assertSame(foo, new NoOpContentNegotiator().formatResponse(null, null, foo));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.github.restup.controller.model;

import static org.junit.Assert.assertNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import org.junit.Test;

public class BasicResourceControllerRequestTest {

@Test
public void testGetHeaders() {
BasicResourceControllerRequest request = mock(BasicResourceControllerRequest.class);
when(request.getHeaders("foo")).thenCallRealMethod();
assertNull(request.getHeaders("foo"));
}

}
Loading

0 comments on commit 8d5b171

Please sign in to comment.