Skip to content

Commit

Permalink
Cont. migration to Junit5 style
Browse files Browse the repository at this point in the history
  • Loading branch information
mokies committed Oct 20, 2017
1 parent 9b46c7c commit d4172d8
Show file tree
Hide file tree
Showing 26 changed files with 132 additions and 153 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@

import static org.assertj.core.api.Assertions.assertThat;

public class RateLimitUtilsTest {
class RateLimitUtilsTest {

@Test
public void shouldReturnFirst() {
void shouldReturnFirst() {
Object one = new Object();
Object two = new Object();

assertThat(RateLimitUtils.coalesce(one, two)).isEqualTo(one);
}

@Test
public void shouldReturnSecond() {
void shouldReturnSecond() {
Object one = null;
Object two = new Object();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,31 @@

import static org.assertj.core.api.Assertions.assertThat;

public class ConcurrentLimitRuleTest {
class ConcurrentLimitRuleTest {

@Test
public void shouldHaveTimeout1Seconds() {
void shouldHaveTimeout1Seconds() {
ConcurrentLimitRule limitRule = ConcurrentLimitRule.of(10, TimeUnit.SECONDS, 1);

assertThat(limitRule.getTimeoutMillis()).isEqualTo(1000);
}

@Test
public void shouldHaveTimeout60Seconds() {
void shouldHaveTimeout60Seconds() {
ConcurrentLimitRule limitRule = ConcurrentLimitRule.of(10, TimeUnit.MINUTES, 1);

assertThat(limitRule.getTimeoutMillis()).isEqualTo(60000);
}

@Test
public void shouldHaveConcurrentLimit5() {
void shouldHaveConcurrentLimit5() {
ConcurrentLimitRule limitRule = ConcurrentLimitRule.of(5, TimeUnit.MINUTES, 5);

assertThat(limitRule.getConcurrentLimit()).isEqualTo(5);
}

@Test
public void shouldHaveNameOfBoom() {
void shouldHaveNameOfBoom() {
ConcurrentLimitRule limitRule = ConcurrentLimitRule.of(1, TimeUnit.SECONDS, 5).withName("boom");

assertThat(limitRule.getName()).isEqualTo("boom");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,38 @@

import static org.assertj.core.api.Assertions.assertThat;

public class RequestLimitRuleTest {
class RequestLimitRuleTest {

@Test
public void shouldHaveDuration1Seconds() {
void shouldHaveDuration1Seconds() {
RequestLimitRule requestLimitRule = RequestLimitRule.of(1, TimeUnit.SECONDS, 5);

assertThat(requestLimitRule.getDurationSeconds()).isEqualTo(1);
}

@Test
public void shouldHaveDuration60Seconds() {
void shouldHaveDuration60Seconds() {
RequestLimitRule requestLimitRule = RequestLimitRule.of(1, TimeUnit.MINUTES, 5);

assertThat(requestLimitRule.getDurationSeconds()).isEqualTo(60);
}

@Test
public void shouldHaveLimit5() {
void shouldHaveLimit5() {
RequestLimitRule requestLimitRule = RequestLimitRule.of(1, TimeUnit.MINUTES, 5);

assertThat(requestLimitRule.getLimit()).isEqualTo(5);
}

@Test
public void shouldHavePrecisionOf10() {
void shouldHavePrecisionOf10() {
RequestLimitRule requestLimitRule = RequestLimitRule.of(1, TimeUnit.SECONDS, 5).withPrecision(10);

assertThat(requestLimitRule.getPrecision()).isPresent().hasValue(10);
}

@Test
public void shouldHaveNameOfBoom() {
void shouldHaveNameOfBoom() {
RequestLimitRule requestLimitRule = RequestLimitRule.of(1, TimeUnit.SECONDS, 5).withName("boom");

assertThat(requestLimitRule.getName()).isEqualTo("boom");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@

import static org.assertj.core.api.Assertions.assertThat;

public class SystemTimeSupplierTest {
class SystemTimeSupplierTest {

@Test
public void shouldGetSystemCurrentTime() {
void shouldGetSystemCurrentTime() {
Long time = new SystemTimeSupplier().get();
assertThat(time).isCloseTo(System.currentTimeMillis() / 1000L, Offset.offset(2L));
}

@Test
public void shouldGetAsyncSystemCurrentTime() throws Exception {
void shouldGetAsyncSystemCurrentTime() throws Exception {
Long time = new SystemTimeSupplier().getAsync().toCompletableFuture().get();
assertThat(time).isCloseTo(System.currentTimeMillis() / 1000L, Offset.offset(2L));
}

@Test
public void shouldGetReactiveSystemCurrentTime() throws Exception {
void shouldGetReactiveSystemCurrentTime() throws Exception {
Long time = new SystemTimeSupplier().getReactive().block();
assertThat(time).isCloseTo(System.currentTimeMillis() / 1000L, Offset.offset(2L));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package es.moki.ratelimij.dropwizard.filter;


import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.junit.jupiter.api.Test;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.container.ResourceInfo;
Expand All @@ -15,29 +12,26 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class AnyKeyTest {
class AnyKeyTest {

@Mock
private HttpServletRequest request;
private HttpServletRequest request = mock(HttpServletRequest.class);

@Mock
private ResourceInfo resource;
private ResourceInfo resource = mock(ResourceInfo.class);

@Mock
private SecurityContext securityContext;
private SecurityContext securityContext = mock(SecurityContext.class);

@Before
public void beforeEach() throws Exception {
@BeforeEach
void beforeEach() throws Exception {
doReturn(Object.class).when(resource).getResourceClass();
when(resource.getResourceMethod()).thenReturn(Object.class.getMethod("wait"));
}

@DisplayName("ANY key should start with 'rlj' prefix")
@Test
public void shouldStartWithPrefix() {
void shouldStartWithPrefix() {
when(request.getRemoteAddr()).thenReturn("293.0.120.7");

Optional<String> keyName = Key.ANY.create(request, resource, securityContext);
Expand All @@ -47,7 +41,7 @@ public void shouldStartWithPrefix() {

@DisplayName("ANY key should include Class and Method names in key")
@Test
public void shouldIncludeResourceInKey() {
void shouldIncludeResourceInKey() {
when(request.getRemoteAddr()).thenReturn("293.0.120.7");

Optional<String> keyName = Key.ANY.create(request, resource, securityContext);
Expand All @@ -57,7 +51,7 @@ public void shouldIncludeResourceInKey() {

@DisplayName("ANY key should include user id if available")
@Test
public void shouldIncludeUserId() {
void shouldIncludeUserId() {
when(securityContext.getUserPrincipal()).thenReturn(() -> "elliot");

Optional<String> keyName = Key.ANY.create(request, resource, securityContext);
Expand All @@ -67,7 +61,7 @@ public void shouldIncludeUserId() {

@DisplayName("ANY key should include X-Forwarded-For if available")
@Test
public void shouldIncludeXForwardedForIfUserNull() {
void shouldIncludeXForwardedForIfUserNull() {
when(request.getHeader("X-Forwarded-For")).thenReturn("293.0.113.7, 211.1.16.2");

Optional<String> keyName = Key.ANY.create(request, resource, securityContext);
Expand All @@ -77,7 +71,7 @@ public void shouldIncludeXForwardedForIfUserNull() {

@DisplayName("ANY key should include remote IP if available and user and X-Forwarded-For not found")
@Test
public void shouldIncludeRemoteIpIfUserAndXForwarded4Null() {
void shouldIncludeRemoteIpIfUserAndXForwarded4Null() {
when(request.getRemoteAddr()).thenReturn("293.0.120.7");

Optional<String> keyName = Key.ANY.create(request, resource, securityContext);
Expand All @@ -87,7 +81,7 @@ public void shouldIncludeRemoteIpIfUserAndXForwarded4Null() {

@DisplayName("ANY key should return absent if no key available")
@Test
public void shouldBeAbsent() {
void shouldBeAbsent() {
when(request.getRemoteAddr()).thenReturn(null);

Optional<String> keyName = Key.ANY.create(request, resource, securityContext);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,39 +1,34 @@
package es.moki.ratelimij.dropwizard.filter;


import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.junit.jupiter.api.Test;

import javax.ws.rs.container.ResourceInfo;
import javax.ws.rs.core.SecurityContext;
import java.util.Optional;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class AuthenticatedKeyTest {
class AuthenticatedKeyTest {

@Mock
private ResourceInfo resource;
private ResourceInfo resource = mock(ResourceInfo.class);

@Mock
private SecurityContext securityContext;
private SecurityContext securityContext = mock(SecurityContext.class);

@Before
public void beforeEach() throws Exception {
@BeforeEach
void beforeEach() throws Exception {
doReturn(Object.class).when(resource).getResourceClass();
when(resource.getResourceMethod()).thenReturn(Object.class.getMethod("wait"));
}

@DisplayName("AUTHENTICATED key should start with 'rlj' prefix")
@Test
public void shouldStartWithPrefix() {
void shouldStartWithPrefix() {
when(securityContext.getUserPrincipal()).thenReturn(() -> "elliot");

Optional<String> keyName = Key.AUTHENTICATED.create(null, resource, securityContext);
Expand All @@ -43,7 +38,7 @@ public void shouldStartWithPrefix() {

@DisplayName("AUTHENTICATED key should include user id if available")
@Test
public void shouldIncludeUserId() {
void shouldIncludeUserId() {
when(securityContext.getUserPrincipal()).thenReturn(() -> "elliot");

Optional<String> keyName = Key.AUTHENTICATED.create(null, resource, securityContext);
Expand All @@ -53,7 +48,7 @@ public void shouldIncludeUserId() {

@DisplayName("AUTHENTICATED key should return absent if no key available")
@Test
public void shouldBeAbsent() {
void shouldBeAbsent() {
Optional<String> keyName = Key.AUTHENTICATED.create(null, resource, securityContext);

assertThat(keyName).isNotPresent();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,39 +1,33 @@
package es.moki.ratelimij.dropwizard.filter;


import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.junit.jupiter.api.Test;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.container.ResourceInfo;
import java.util.Optional;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class IpKeyTest {
class IpKeyTest {

@Mock
private HttpServletRequest request;
private HttpServletRequest request = mock(HttpServletRequest.class);

@Mock
private ResourceInfo resource;
private ResourceInfo resource = mock(ResourceInfo.class);

@Before
public void beforeEach() throws Exception {
@BeforeEach
void beforeEach() throws Exception {
doReturn(Object.class).when(resource).getResourceClass();
when(resource.getResourceMethod()).thenReturn(Object.class.getMethod("wait"));
}

@DisplayName("IP key should start with 'rlj' prefix")
@Test
public void shouldStartWithPrefix() {
void shouldStartWithPrefix() {
when(request.getRemoteAddr()).thenReturn("293.0.120.7");

Optional<String> keyName = Key.IP.create(request, resource, null);
Expand All @@ -43,7 +37,7 @@ public void shouldStartWithPrefix() {

@DisplayName("IP key should include Class and Method names in key")
@Test
public void shouldIncludeResourceInKey() {
void shouldIncludeResourceInKey() {
when(request.getRemoteAddr()).thenReturn("293.0.120.7");

Optional<String> keyName = Key.IP.create(request, resource, null);
Expand All @@ -53,7 +47,7 @@ public void shouldIncludeResourceInKey() {

@DisplayName("IP key should include X-Forwarded-For if available")
@Test
public void shouldIncludeXForwardedForIfUserNull() {
void shouldIncludeXForwardedForIfUserNull() {
when(request.getHeader("X-Forwarded-For")).thenReturn("293.0.113.7, 211.1.16.2");

Optional<String> keyName = Key.IP.create(request, resource, null);
Expand All @@ -63,7 +57,7 @@ public void shouldIncludeXForwardedForIfUserNull() {

@DisplayName("IP key should include remote IP if available and user and X-Forwarded-For not found")
@Test
public void shouldIncludeRemoteIpIfUserAndXForwarded4Null() {
void shouldIncludeRemoteIpIfUserAndXForwarded4Null() {
when(request.getRemoteAddr()).thenReturn("293.0.120.7");

Optional<String> keyName = Key.IP.create(request, resource, null);
Expand All @@ -73,7 +67,7 @@ public void shouldIncludeRemoteIpIfUserAndXForwarded4Null() {

@DisplayName("IP key should return absent if no key available")
@Test
public void shouldBeAbsent() {
void shouldBeAbsent() {
when(request.getRemoteAddr()).thenReturn(null);

Optional<String> keyName = Key.IP.create(request, resource, null);
Expand Down
Loading

0 comments on commit d4172d8

Please sign in to comment.