Skip to content

Commit

Permalink
convert tests from junit4 to junit5 (#1659)
Browse files Browse the repository at this point in the history
Migrate servicetalk-http-* tests to junit5
  • Loading branch information
danfaer authored and idelpivnitskiy committed Jul 15, 2021
1 parent fe4eff2 commit 1920d33
Show file tree
Hide file tree
Showing 23 changed files with 360 additions and 410 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package io.servicetalk.http.netty;

import io.servicetalk.concurrent.api.Single;
import io.servicetalk.concurrent.internal.TimeoutTracingInfoExtension;
import io.servicetalk.http.api.BlockingHttpClient;
import io.servicetalk.http.api.BlockingStreamingHttpClient;
import io.servicetalk.http.api.HttpClient;
Expand All @@ -36,7 +35,6 @@
import org.hamcrest.TypeSafeMatcher;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import java.net.InetSocketAddress;
import java.util.concurrent.atomic.AtomicReference;
Expand All @@ -51,7 +49,6 @@
import static io.servicetalk.transport.netty.internal.AddressUtils.serverHostAndPort;
import static org.hamcrest.MatcherAssert.assertThat;

@ExtendWith(TimeoutTracingInfoExtension.class)
class ExecutionStrategyInContextTest {

@Nullable
Expand Down
6 changes: 5 additions & 1 deletion servicetalk-http-router-predicate/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ dependencies {
testImplementation project(":servicetalk-http-netty")
testImplementation project(":servicetalk-transport-netty")
testImplementation project(":servicetalk-test-resources")
testImplementation "junit:junit:$junitVersion"
testImplementation "org.junit.jupiter:junit-jupiter-api:$junit5Version"
testImplementation "org.junit.jupiter:junit-jupiter-params:$junit5Version"
testImplementation "org.hamcrest:hamcrest-library:$hamcrestVersion"
testImplementation "org.mockito:mockito-core:$mockitoCoreVersion"
testImplementation "org.mockito:mockito-junit-jupiter:$mockitoCoreVersion"

testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:$junit5Version"
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import io.servicetalk.buffer.api.BufferAllocator;
import io.servicetalk.buffer.api.ReadOnlyBufferAllocators;
import io.servicetalk.concurrent.api.Single;
import io.servicetalk.concurrent.internal.ServiceTalkTestTimeout;
import io.servicetalk.http.api.BlockingStreamingHttpResponseFactory;
import io.servicetalk.http.api.DefaultHttpHeadersFactory;
import io.servicetalk.http.api.DefaultStreamingHttpRequestResponseFactory;
Expand All @@ -31,13 +30,12 @@
import io.servicetalk.http.api.StreamingHttpService;
import io.servicetalk.http.api.TestHttpServiceContext;

import org.junit.Before;
import org.junit.Rule;
import org.junit.rules.ExpectedException;
import org.junit.rules.Timeout;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
import org.mockito.stubbing.Answer;

import java.util.Iterator;
Expand All @@ -53,32 +51,27 @@
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
public abstract class BaseHttpPredicateRouterBuilderTest {
static final BufferAllocator allocator = ReadOnlyBufferAllocators.DEFAULT_RO_ALLOCATOR;
private static final BufferAllocator allocator = ReadOnlyBufferAllocators.DEFAULT_RO_ALLOCATOR;
static final StreamingHttpRequestResponseFactory reqRespFactory =
new DefaultStreamingHttpRequestResponseFactory(allocator, DefaultHttpHeadersFactory.INSTANCE, HTTP_1_1);

@Rule
public final MockitoRule rule = MockitoJUnit.rule().silent();
@Rule
public final ExpectedException expected = ExpectedException.none();
@Rule
public final Timeout timeout = new ServiceTalkTestTimeout();

@Mock
@Mock(lenient = true)
StreamingHttpService serviceA, serviceB, serviceC, serviceD, serviceE, fallbackService;
@Mock
@Mock(lenient = true)
HttpExecutionContext executionCtx;
@Mock
@Mock(lenient = true)
TestHttpServiceContext ctx;
@Mock
@Mock(lenient = true)
StreamingHttpRequest request;
@Mock
HttpHeaders headers;
@Mock
Single<StreamingHttpResponse> responseA, responseB, responseC, responseD, responseE, fallbackResponse;

@Before
@BeforeEach
public void setUp() {
when(executionCtx.executor()).thenReturn(immediate());
when(executionCtx.executionStrategy()).thenReturn(defaultStrategy());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,20 @@
import io.servicetalk.http.api.StreamingHttpResponse;
import io.servicetalk.http.api.StreamingHttpService;

import org.junit.Test;
import org.junit.jupiter.api.Test;

import static io.servicetalk.http.api.HttpHeaderNames.CONTENT_LENGTH;
import static io.servicetalk.http.api.HttpHeaderNames.CONTENT_TYPE;
import static io.servicetalk.http.api.HttpHeaderValues.TEXT_PLAIN_UTF_8;
import static io.servicetalk.http.api.HttpHeaderValues.ZERO;
import static io.servicetalk.http.api.HttpProtocolVersion.HTTP_1_1;
import static io.servicetalk.http.api.HttpResponseStatus.NOT_FOUND;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class DefaultFallbackServiceTest extends BaseHttpPredicateRouterBuilderTest {
class DefaultFallbackServiceTest extends BaseHttpPredicateRouterBuilderTest {

@Test
public void testDefaultFallbackService() throws Exception {
void testDefaultFallbackService() throws Exception {
final StreamingHttpService fixture = DefaultFallbackServiceStreaming.instance();

final Single<StreamingHttpResponse> responseSingle = fixture.handle(ctx, request, reqRespFactory);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@
import io.servicetalk.http.api.HttpSetCookie;
import io.servicetalk.http.api.StreamingHttpService;

import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;

import static java.util.Collections.emptyIterator;
import static org.junit.Assert.assertSame;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.mockito.Mockito.when;

public class HttpPredicateRouterBuilderCookieTest extends BaseHttpPredicateRouterBuilderTest {
class HttpPredicateRouterBuilderCookieTest extends BaseHttpPredicateRouterBuilderTest {

@Mock
HttpSetCookie cookie1, cookie2;

@Test
public void testWhenCookieIsPresent() {
void testWhenCookieIsPresent() {
final StreamingHttpService service = new HttpPredicateRouterBuilder()
.whenCookie("session").isPresent().thenRouteTo(serviceA)
.when((ctx, req) -> true).thenRouteTo(fallbackService)
Expand All @@ -45,7 +45,7 @@ public void testWhenCookieIsPresent() {
}

@Test
public void testWhenCookieIs() {
void testWhenCookieIs() {
final StreamingHttpService service = new HttpPredicateRouterBuilder()
.whenCookie("session").value(cookie1::equals).thenRouteTo(serviceA)
.when((ctx, req) -> true).thenRouteTo(fallbackService)
Expand All @@ -68,7 +68,7 @@ public void testWhenCookieIs() {
}

@Test
public void testWhenCookieValues() {
void testWhenCookieValues() {
final StreamingHttpService service = new HttpPredicateRouterBuilder()
.whenCookie("session").values(new AnyMatchPredicate<>(cookie1)).thenRouteTo(serviceA)
.when((ctx, req) -> true).thenRouteTo(fallbackService)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,22 @@

import io.servicetalk.http.api.StreamingHttpService;

import org.junit.Test;
import org.junit.jupiter.api.Test;

import java.util.regex.Pattern;

import static java.util.Collections.emptyIterator;
import static java.util.regex.Pattern.CASE_INSENSITIVE;
import static org.junit.Assert.assertSame;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@SuppressWarnings("PMD.AvoidUsingHardCodedIP")
public class HttpPredicateRouterBuilderHeaderTest extends BaseHttpPredicateRouterBuilderTest {
class HttpPredicateRouterBuilderHeaderTest extends BaseHttpPredicateRouterBuilderTest {

@Test
public void testWhenHeaderIsPresent() {
void testWhenHeaderIsPresent() {
final StreamingHttpService service = new HttpPredicateRouterBuilder()
.whenHeader("host").isPresent().thenRouteTo(serviceA)
.when((ctx, req) -> true).thenRouteTo(fallbackService)
Expand All @@ -46,7 +46,7 @@ public void testWhenHeaderIsPresent() {
}

@Test
public void testWhenHeaderFirstValue() {
void testWhenHeaderFirstValue() {
final StreamingHttpService service = new HttpPredicateRouterBuilder()
.whenHeader("host").firstValue("localhost").thenRouteTo(serviceA)
.when((ctx, req) -> true).thenRouteTo(fallbackService)
Expand All @@ -69,7 +69,7 @@ public void testWhenHeaderFirstValue() {
}

@Test
public void testWhenHeaderFirstValueMatches() {
void testWhenHeaderFirstValueMatches() {
final StreamingHttpService service = new HttpPredicateRouterBuilder()
.whenHeader("host").firstValueMatches("127\\..*").thenRouteTo(serviceA)
.when((ctx, req) -> true).thenRouteTo(fallbackService)
Expand All @@ -89,7 +89,7 @@ public void testWhenHeaderFirstValueMatches() {
}

@Test
public void testWhenHeaderFirstValueMatchesPattern() {
void testWhenHeaderFirstValueMatchesPattern() {
final StreamingHttpService service = new HttpPredicateRouterBuilder()
.whenHeader("host").firstValueMatches(Pattern.compile("127\\..*", CASE_INSENSITIVE))
.thenRouteTo(serviceA)
Expand All @@ -110,7 +110,7 @@ public void testWhenHeaderFirstValueMatchesPattern() {
}

@Test
public void testWhenHeaderValues() {
void testWhenHeaderValues() {
final StreamingHttpService service = new HttpPredicateRouterBuilder()
.whenHeader("host").values(new AnyMatchPredicate<>("localhost")).thenRouteTo(serviceA)
.when((ctx, req) -> true).thenRouteTo(fallbackService)
Expand All @@ -130,7 +130,7 @@ public void testWhenHeaderValues() {
}

@Test
public void testMultipleHeaderRoutes() {
void testMultipleHeaderRoutes() {
final StreamingHttpService service = new HttpPredicateRouterBuilder()
.whenHeader("host").firstValue("a.com").thenRouteTo(serviceA)
.whenHeader("host").firstValue("b.com").thenRouteTo(serviceB)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@

import io.servicetalk.http.api.StreamingHttpService;

import org.junit.Test;
import org.junit.jupiter.api.Test;

import java.util.regex.Pattern;

import static java.util.Collections.emptyIterator;
import static java.util.regex.Pattern.CASE_INSENSITIVE;
import static org.junit.Assert.assertSame;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.mockito.Mockito.when;

public class HttpPredicateRouterBuilderQueryTest extends BaseHttpPredicateRouterBuilderTest {
class HttpPredicateRouterBuilderQueryTest extends BaseHttpPredicateRouterBuilderTest {

@Test
public void testWhenQueryParamIsPresent() {
void testWhenQueryParamIsPresent() {
final StreamingHttpService service = new HttpPredicateRouterBuilder()
.whenQueryParam("page").isPresent().thenRouteTo(serviceA)
.when((ctx, req) -> true).thenRouteTo(fallbackService)
Expand All @@ -43,7 +43,7 @@ public void testWhenQueryParamIsPresent() {
}

@Test
public void testWhenQueryParamFirstValue() {
void testWhenQueryParamFirstValue() {
final StreamingHttpService service = new HttpPredicateRouterBuilder()
.whenQueryParam("page").firstValue("home").thenRouteTo(serviceA)
.when((ctx, req) -> true).thenRouteTo(fallbackService)
Expand All @@ -66,7 +66,7 @@ public void testWhenQueryParamFirstValue() {
}

@Test
public void testWhenQueryParamFirstValueMatches() {
void testWhenQueryParamFirstValueMatches() {
final StreamingHttpService service = new HttpPredicateRouterBuilder()
.whenQueryParam("page").firstValueMatches("sign.*").thenRouteTo(serviceA)
.when((ctx, req) -> true).thenRouteTo(fallbackService)
Expand All @@ -89,7 +89,7 @@ public void testWhenQueryParamFirstValueMatches() {
}

@Test
public void testWhenQueryParamFirstValueMatchesPattern() {
void testWhenQueryParamFirstValueMatchesPattern() {
final StreamingHttpService service = new HttpPredicateRouterBuilder()
.whenQueryParam("page").firstValueMatches(Pattern.compile("sign.*", CASE_INSENSITIVE))
.thenRouteTo(serviceA)
Expand All @@ -113,7 +113,7 @@ public void testWhenQueryParamFirstValueMatchesPattern() {
}

@Test
public void testWhenQueryParamValues() {
void testWhenQueryParamValues() {
final StreamingHttpService service = new HttpPredicateRouterBuilder()
.whenQueryParam("page").values(new AnyMatchPredicate<>("home")).thenRouteTo(serviceA)
.when((ctx, req) -> true).thenRouteTo(fallbackService)
Expand Down
Loading

0 comments on commit 1920d33

Please sign in to comment.