Skip to content

Commit

Permalink
Expanded unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
trajano committed Dec 21, 2017
1 parent 6f9d728 commit 36f271a
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.trajano.ms.common.test;

import static net.trajano.commons.testing.UtilityClassTestUtil.assertUtilityClassWellDefined;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
Expand All @@ -15,6 +16,10 @@
import javax.ws.rs.core.UriInfo;
import javax.xml.bind.JAXB;

import net.trajano.commons.testing.UtilityClassTestUtil;
import net.trajano.ms.core.ErrorCodes;
import net.trajano.ms.core.Qualifiers;
import net.trajano.ms.spi.CacheNames;
import org.junit.Test;
import org.slf4j.MDC;
import org.springframework.beans.factory.BeanExpressionException;
Expand All @@ -24,6 +29,16 @@

public class ErrorResponseTest {

@Test
public void assertConstClass() throws Exception {

assertUtilityClassWellDefined(CacheNames.class);
assertUtilityClassWellDefined(ErrorCodes.class);
assertUtilityClassWellDefined(MDCKeys.class);
assertUtilityClassWellDefined(Qualifiers.class);

}

@Test
public void chainedError() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,8 @@ public void register(final Class<? extends Application> applicationClass,

}

public void setFailureHandler(JaxRsFailureHandler failureHandler) {

this.failureHandler = failureHandler;
}
}
6 changes: 6 additions & 0 deletions ms-engine/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,12 @@
<version>${vertx.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.trajano.commons</groupId>
<artifactId>commons-testing</artifactId>
<version>2.1.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,26 +164,6 @@ public SpringJaxRsHandler(final ConfigurableApplicationContext baseApplicationCo
providerFactory = deployment.getProviderFactory();
}

/**
* The base URI set in {@link ApplicationPath} annotation.
*
* @return the base URI
*/
public URI baseUri() {

return baseUri;
}

/**
* The route path to the base URI. Basically base URI + "/*".
*
* @return The route path to the base URI.
*/
public String baseUriRoute() {

return baseUri().toASCIIString() + "/*";
}

@Override
public void close() {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package net.trajano.ms.engine.test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.*;

import java.util.stream.Collectors;

import net.trajano.ms.engine.jaxrs.JaxRsFailureHandler;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down Expand Up @@ -35,13 +39,29 @@ public class SpringJaxRsHandlerTest {
@Rule
public RunTestOnContext rule = new RunTestOnContext();

@Test
public void test400(final TestContext testContext) throws Exception {
private Router router;

final Router router = Router.router(rule.vertx());
private SpringJaxRsHandler handler;

@Before
public void setup() {

router = Router.router(rule.vertx());
final JaxRsRouter jaxRsRouter = new JaxRsRouter();
final SpringJaxRsHandler handler = new SpringJaxRsHandler(MyApp.class);
handler = new SpringJaxRsHandler(MyApp.class);
jaxRsRouter.setFailureHandler(new JaxRsFailureHandler());
jaxRsRouter.register(MyApp.class, router, handler, handler);
}

@After
public void teardown() {

handler.close();

}

@Test
public void test400(final TestContext testContext) throws Exception {

final HttpServerRequest serverRequest = mock(HttpServerRequest.class);
when(serverRequest.absoluteURI()).thenReturn("http://test.trajano.net/api/hello/400");
Expand All @@ -67,7 +87,7 @@ public void test400(final TestContext testContext) throws Exception {
when(serverRequest.response()).thenReturn(response);

router.accept(serverRequest);
async.await();
async.awaitSuccess();

verify(response, times(1)).setStatusCode(400);
}
Expand Down Expand Up @@ -184,6 +204,38 @@ public void test500(final TestContext testContext) throws Exception {
assertTrue(errorMessage.contains("server_error"));
}

@Test
public void testFailure(final TestContext testContext) throws Exception {

final HttpServerRequest serverRequest = mock(HttpServerRequest.class);
when(serverRequest.absoluteURI()).thenThrow(new RuntimeException("boom"));
when(serverRequest.path()).thenReturn("/api/hello/400");
when(serverRequest.uri()).thenReturn("/api/hello/400");
when(serverRequest.isEnded()).thenReturn(true);
when(serverRequest.method()).thenReturn(HttpMethod.GET);

final HttpServerResponse response = mock(HttpServerResponse.class);
when(response.putHeader(anyString(), anyString())).thenReturn(response);
when(response.putHeader(any(AsciiString.class), anyString())).thenReturn(response);
when(response.headers()).thenReturn(new VertxHttpHeaders());

final Async async = testContext.async();
when(response.setStatusCode(Matchers.any(Integer.class))).then(invocation -> {

try {
return response;
} finally {
async.complete();
}
});
when(serverRequest.response()).thenReturn(response);

router.accept(serverRequest);
async.await();

verify(response, times(1)).setStatusCode(500);
}

@Test
public void testHandler(final TestContext testContext) throws Exception {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package net.trajano.ms.engine.test;

import net.trajano.ms.engine.internal.BufferUtil;
import net.trajano.ms.engine.internal.Conversions;
import org.junit.Test;

import static net.trajano.commons.testing.UtilityClassTestUtil.assertUtilityClassWellDefined;

public class UtilityClassTest {

@Test
public void testUtils() throws Exception {

assertUtilityClassWellDefined(BufferUtil.class);
assertUtilityClassWellDefined(Conversions.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ public void init() {
}

public void setClient(Client client) {

this.client = client;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public void testOidcResource() {
when(webTarget.path(anyString())).thenReturn(webTarget);
final Invocation.Builder builder = mock(Invocation.Builder.class);
when(webTarget.request(anyString())).thenReturn(builder);
when(builder.header(anyString(),anyString())).thenReturn(builder);
when(builder.header(anyString(), anyString())).thenReturn(builder);
when(builder.get(String.class)).thenReturn("http://callback.trajano.net");
resource.setClient(client);
final Response auth = resource.auth("abc", "issuer", "jdk");
Expand Down

0 comments on commit 36f271a

Please sign in to comment.