Skip to content

Commit

Permalink
More unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lukashinsch committed Jul 5, 2015
1 parent 682a82a commit ddc85f7
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,12 @@ private void doRequestAndAddToCache(final HttpServletRequest request, final Map<
ContentBufferingResponse response = new ContentBufferingResponse();
try {
dispatcherServlet.service(new UrlRewritingRequestWrapper(request, urlDecode(url)), response);
String controllerResponse = response.getResponseContent();
verifyNoErrorResponse(response.getStatus(), url);
cache.put(url, controllerResponse);
} catch (Exception e) {
throw new RuntimeException("error caching request " + url, e);
}
String controllerResponse = response.getResponseContent();
verifyNoErrorResponse(response.getStatus(), url);
cache.put(url, controllerResponse);
}

private void verifyNoErrorResponse(int responseStatus, String url) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
import eu.hinsch.spring.angular.cache.AngularRestCachePreloadConfiguration.CachedUrl;
import org.apache.commons.io.IOUtils;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.junit.rules.ExpectedException;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
Expand All @@ -20,6 +21,8 @@
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import static java.util.Collections.singletonList;
import static org.hamcrest.Matchers.containsString;
Expand All @@ -34,6 +37,9 @@ public class AngularRestCachePreloadTransformerTest {

private static final String REST_RESPONSE = "['A', 'B']";

@Rule
public ExpectedException expectedException = ExpectedException.none();

@Mock
private AngularRestCachePreloadConfiguration config;
@Mock
Expand All @@ -51,35 +57,71 @@ public class AngularRestCachePreloadTransformerTest {
private AngularRestCachePreloadTransformer transformer;

@Before
public void setup() {
public void setup() throws Exception {
MockitoAnnotations.initMocks(this);
when(chain.transform(request, resource)).thenReturn(resource);
when(config.getAngularModule()).thenReturn("myModule");
when(config.getPlaceholder()).thenReturn("{cachePreloadScript}");
when(resource.getInputStream()).thenReturn(new ByteArrayInputStream("--{cachePreloadScript}--".getBytes()));
}

@Test
public void shouldAddRestResponseToAngularCache() throws IOException, ServletException {
public void shouldAddRestResponseToAngularCache() throws Exception {
// given
when(chain.transform(request, resource)).thenReturn(resource);
when(config.getCachedUrls()).thenReturn(singletonList(new CachedUrl("/test/url")));
when(config.getAngularModule()).thenReturn("myModule");
when(config.getPlaceholder()).thenReturn("{cachePreloadScript}");
when(resource.getInputStream()).thenReturn(new ByteArrayInputStream("--{cachePreloadScript}--".getBytes()));
doAnswer(invocation -> {
ServletResponse response = (ServletResponse) invocation.getArguments()[1];
response.getOutputStream().write(REST_RESPONSE.getBytes());
return null;
}).when(dispatcherServlet).service(any(ServletRequest.class), any(ServletResponse.class));
mockResponse(REST_RESPONSE, 200);

// when
Resource transformedResource = transformer.transform(request, resource, chain);

// then
ArgumentCaptor<HttpServletResponse> responseCaptor = ArgumentCaptor.forClass(HttpServletResponse.class);
ArgumentCaptor<HttpServletRequest> requestCaptor = ArgumentCaptor.forClass(HttpServletRequest.class);
verify(dispatcherServlet).service(requestCaptor.capture(), responseCaptor.capture());

String content = IOUtils.toString(transformedResource.getInputStream());
String content = getContent(transformedResource);
assertThat(content, containsString("httpCache.put('/test/url', '" + REST_RESPONSE + "');"));
}

@Test
public void shouldHandleDynamicParameters() throws Exception {
// given
Map<String,String> parameters = new HashMap<>();
parameters.put("parameter", "1 + 1");
when(config.getCachedUrls()).thenReturn(singletonList(new CachedUrl("/test/url/{parameter}", parameters)));
mockResponse(REST_RESPONSE, 200);

// when
Resource transformedResource = transformer.transform(request, resource, chain);

// then
String content = getContent(transformedResource);
assertThat(content, containsString("httpCache.put('/test/url/2', '" + REST_RESPONSE + "');"));
}

@Test
public void shouldThrowExceptionOnErrorResponse() throws Exception {
// given
when(config.getCachedUrls()).thenReturn(singletonList(new CachedUrl("/test/url")));
mockResponse("", 400);
expectedException.expect(RuntimeException.class);
expectedException.expectMessage("Error caching request /test/url, response status was 400");

// when
transformer.transform(request, resource, chain);

// then -> exception
}

private void mockResponse(String restResponse, int status) throws ServletException, IOException {
doAnswer(invocation -> {
HttpServletResponse response = (HttpServletResponse) invocation.getArguments()[1];
response.getOutputStream().write(restResponse.getBytes());
response.setStatus(status);
return null;
}).when(dispatcherServlet).service(any(ServletRequest.class), any(ServletResponse.class));
}


private String getContent(Resource transformedResource) throws IOException {
return IOUtils.toString(transformedResource.getInputStream());
}


}

0 comments on commit ddc85f7

Please sign in to comment.