Skip to content

Commit

Permalink
Migrate unit tests to JUnit 5 (Part 1) (#471)
Browse files Browse the repository at this point in the history
This PR migrates part of unit tests to JUnit 5.

#### Changes:
- Migrate some tests to JUnit 5
- Use nested tests in `ProjectServiceV1Test` to reduce the number of Central Dogma instances
- Upgrade Mockito to 3.2.4 and revert an earlier change in `ZooKeeperCommandExecutorTest`
- Use `assertThat().hasSize()` where applicable
  • Loading branch information
KarboniteKream authored and trustin committed Jan 23, 2020
1 parent fe81f2a commit 346a975
Show file tree
Hide file tree
Showing 70 changed files with 646 additions and 658 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ configure(projectsWithFlags('java')) {
testCompile 'org.hamcrest:hamcrest-library'
testCompile 'org.assertj:assertj-core'
testCompile 'org.mockito:mockito-core'
testCompile 'org.mockito:mockito-junit-jupiter'
testCompile 'org.junit.jupiter:junit-jupiter-api'
testCompile 'org.junit.jupiter:junit-jupiter-params'
testRuntime 'org.junit.jupiter:junit-jupiter-engine'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2018 LINE Corporation
* Copyright 2020 LINE Corporation
*
* LINE Corporation licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
Expand Down Expand Up @@ -28,12 +28,9 @@
import java.util.Set;

import org.apache.thrift.async.AsyncMethodCallback;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
Expand Down Expand Up @@ -72,24 +69,22 @@
import com.linecorp.centraldogma.internal.thrift.WatchFileResult;
import com.linecorp.centraldogma.internal.thrift.WatchRepositoryResult;

public class LegacyCentralDogmaTest {
private static final String TIMESTAMP = "2016-01-02T03:04:05Z";
class LegacyCentralDogmaTest {

@Rule
public MockitoRule rule = MockitoJUnit.rule();
private static final String TIMESTAMP = "2016-01-02T03:04:05Z";

@Mock
private CentralDogmaService.AsyncIface iface;

private CentralDogma client;

@Before
public void setup() {
@BeforeEach
void setUp() {
client = new LegacyCentralDogma(CommonPools.workerGroup(), iface);
}

@Test
public void createProject() throws Exception {
void createProject() throws Exception {
doAnswer(invocation -> {
final AsyncMethodCallback<Void> callback = invocation.getArgument(1);
callback.onComplete(null);
Expand All @@ -100,7 +95,7 @@ public void createProject() throws Exception {
}

@Test
public void removeProject() throws Exception {
void removeProject() throws Exception {
doAnswer(invocation -> {
final AsyncMethodCallback<Void> callback = invocation.getArgument(1);
callback.onComplete(null);
Expand All @@ -111,7 +106,7 @@ public void removeProject() throws Exception {
}

@Test
public void purgeProject() throws Exception {
void purgeProject() throws Exception {
doAnswer(invocation -> {
final AsyncMethodCallback<Void> callback = invocation.getArgument(1);
callback.onComplete(null);
Expand All @@ -122,7 +117,7 @@ public void purgeProject() throws Exception {
}

@Test
public void unremoveProject() throws Exception {
void unremoveProject() throws Exception {
doAnswer(invocation -> {
final AsyncMethodCallback<Void> callback = invocation.getArgument(1);
callback.onComplete(null);
Expand All @@ -133,7 +128,7 @@ public void unremoveProject() throws Exception {
}

@Test
public void listProjects() throws Exception {
void listProjects() throws Exception {
doAnswer(invocation -> {
final AsyncMethodCallback<List<Project>> callback = invocation.getArgument(0);
callback.onComplete(ImmutableList.of(new Project("project")));
Expand All @@ -144,7 +139,7 @@ public void listProjects() throws Exception {
}

@Test
public void listRemovedProjects() throws Exception {
void listRemovedProjects() throws Exception {
doAnswer(invocation -> {
final AsyncMethodCallback<Set<String>> callback = invocation.getArgument(0);
callback.onComplete(ImmutableSet.of("project"));
Expand All @@ -155,7 +150,7 @@ public void listRemovedProjects() throws Exception {
}

@Test
public void createRepository() throws Exception {
void createRepository() throws Exception {
doAnswer(invocation -> {
final AsyncMethodCallback<Void> callback = invocation.getArgument(2);
callback.onComplete(null);
Expand All @@ -166,7 +161,7 @@ public void createRepository() throws Exception {
}

@Test
public void removeRepository() throws Exception {
void removeRepository() throws Exception {
doAnswer(invocation -> {
final AsyncMethodCallback<Void> callback = invocation.getArgument(2);
callback.onComplete(null);
Expand All @@ -177,7 +172,7 @@ public void removeRepository() throws Exception {
}

@Test
public void purgeRepository() throws Exception {
void purgeRepository() throws Exception {
doAnswer(invocation -> {
final AsyncMethodCallback<Void> callback = invocation.getArgument(2);
callback.onComplete(null);
Expand All @@ -188,7 +183,7 @@ public void purgeRepository() throws Exception {
}

@Test
public void unremoveRepository() throws Exception {
void unremoveRepository() throws Exception {
doAnswer(invocation -> {
final AsyncMethodCallback<Void> callback = invocation.getArgument(2);
callback.onComplete(null);
Expand All @@ -199,7 +194,7 @@ public void unremoveRepository() throws Exception {
}

@Test
public void listRepositories() throws Exception {
void listRepositories() throws Exception {
doAnswer(invocation -> {
final AsyncMethodCallback<List<Repository>> callback = invocation.getArgument(1);
final Repository repository = new Repository("repo").setHead(
Expand All @@ -216,7 +211,7 @@ public void listRepositories() throws Exception {
}

@Test
public void listRemovedRepositories() throws Exception {
void listRemovedRepositories() throws Exception {
doAnswer(invocation -> {
final AsyncMethodCallback<Set<String>> callback = invocation.getArgument(1);
callback.onComplete(ImmutableSet.of("repo"));
Expand All @@ -227,7 +222,7 @@ public void listRemovedRepositories() throws Exception {
}

@Test
public void normalizeRevision() throws Exception {
void normalizeRevision() throws Exception {
doAnswer(invocation -> {
final AsyncMethodCallback<TRevision> callback = invocation.getArgument(3);
callback.onComplete(new TRevision(3));
Expand All @@ -239,7 +234,7 @@ public void normalizeRevision() throws Exception {
}

@Test
public void listFiles() throws Exception {
void listFiles() throws Exception {
doAnswer(invocation -> {
final AsyncMethodCallback<List<TEntry>> callback = invocation.getArgument(4);
final TEntry entry = new TEntry("/a.txt", TEntryType.TEXT);
Expand All @@ -253,13 +248,7 @@ public void listFiles() throws Exception {
}

@Test
public void getFiles() throws Exception {
doAnswer(invocation -> {
final AsyncMethodCallback<com.linecorp.centraldogma.internal.thrift.Revision> callback =
invocation.getArgument(3);
callback.onComplete(new com.linecorp.centraldogma.internal.thrift.Revision(1, 0));
return null;
}).when(iface).normalizeRevision(any(), any(), any(), any());
void getFiles() throws Exception {
doAnswer(invocation -> {
final AsyncMethodCallback<List<TEntry>> callback = invocation.getArgument(4);
final TEntry entry = new TEntry("/b.txt", TEntryType.TEXT);
Expand All @@ -273,7 +262,7 @@ public void getFiles() throws Exception {
}

@Test
public void getHistory() throws Exception {
void getHistory() throws Exception {
doAnswer(invocation -> {
final AsyncMethodCallback<List<TCommit>> callback = invocation.getArgument(5);
callback.onComplete(ImmutableList.of(new TCommit(
Expand All @@ -294,7 +283,7 @@ public void getHistory() throws Exception {
}

@Test
public void getDiffs() throws Exception {
void getDiffs() throws Exception {
doAnswer(invocation -> {
final AsyncMethodCallback<List<TChange>> callback = invocation.getArgument(5);
final TChange change = new TChange("/a.txt", ChangeType.UPSERT_TEXT);
Expand All @@ -308,7 +297,7 @@ public void getDiffs() throws Exception {
}

@Test
public void getPreviewDiffs() throws Exception {
void getPreviewDiffs() throws Exception {
doAnswer(invocation -> {
final AsyncMethodCallback<List<TChange>> callback = invocation.getArgument(4);
final TChange change = new TChange("/a.txt", ChangeType.UPSERT_TEXT);
Expand All @@ -323,7 +312,7 @@ public void getPreviewDiffs() throws Exception {
}

@Test
public void push() throws Exception {
void push() throws Exception {
doAnswer(invocation -> {
final AsyncMethodCallback<TCommit> callback = invocation.getArgument(7);
callback.onComplete(new TCommit(
Expand All @@ -345,13 +334,7 @@ public void push() throws Exception {
}

@Test
public void getFile() throws Exception {
doAnswer(invocation -> {
final AsyncMethodCallback<com.linecorp.centraldogma.internal.thrift.Revision> callback =
invocation.getArgument(3);
callback.onComplete(new com.linecorp.centraldogma.internal.thrift.Revision(1, 0));
return null;
}).when(iface).normalizeRevision(any(), any(), any(), any());
void getFile() throws Exception {
doAnswer(invocation -> {
final AsyncMethodCallback<GetFileResult> callback = invocation.getArgument(4);
callback.onComplete(new GetFileResult(TEntryType.TEXT, "content"));
Expand All @@ -363,13 +346,7 @@ public void getFile() throws Exception {
}

@Test
public void getFile_path() throws Exception {
doAnswer(invocation -> {
final AsyncMethodCallback<com.linecorp.centraldogma.internal.thrift.Revision> callback =
invocation.getArgument(3);
callback.onComplete(new com.linecorp.centraldogma.internal.thrift.Revision(1, 0));
return null;
}).when(iface).normalizeRevision(any(), any(), any(), any());
void getFile_path() throws Exception {
doAnswer(invocation -> {
final AsyncMethodCallback<GetFileResult> callback = invocation.getArgument(4);
callback.onComplete(new GetFileResult(TEntryType.TEXT, "content"));
Expand All @@ -381,7 +358,7 @@ public void getFile_path() throws Exception {
}

@Test
public void mergeFiles() throws Exception {
void mergeFiles() throws Exception {
doAnswer(invocation -> {
final AsyncMethodCallback<MergedEntry> callback = invocation.getArgument(4);
callback.onComplete(new MergedEntry(new TRevision(1), TEntryType.JSON, "{\"foo\": \"bar\"}",
Expand All @@ -399,7 +376,7 @@ public void mergeFiles() throws Exception {
}

@Test
public void diffFile() throws Exception {
void diffFile() throws Exception {
doAnswer(invocation -> {
final AsyncMethodCallback<DiffFileResult> callback = invocation.getArgument(5);
callback.onComplete(new DiffFileResult(ChangeType.UPSERT_TEXT, "some_text"));
Expand All @@ -412,7 +389,7 @@ public void diffFile() throws Exception {
}

@Test
public void watchRepository() throws Exception {
void watchRepository() throws Exception {
doAnswer(invocation -> {
final AsyncMethodCallback<WatchRepositoryResult> callback = invocation.getArgument(5);
callback.onComplete(new WatchRepositoryResult().setRevision(new TRevision(42)));
Expand All @@ -424,7 +401,7 @@ public void watchRepository() throws Exception {
}

@Test
public void watchRepositoryTimedOut() throws Exception {
void watchRepositoryTimedOut() throws Exception {
doAnswer(invocation -> {
AsyncMethodCallback<WatchRepositoryResult> callback = invocation.getArgument(5);
callback.onComplete(new WatchRepositoryResult());
Expand All @@ -436,7 +413,7 @@ public void watchRepositoryTimedOut() throws Exception {
}

@Test
public void watchFile() throws Exception {
void watchFile() throws Exception {
doAnswer(invocation -> {
AsyncMethodCallback<WatchFileResult> callback = invocation.getArgument(5);
callback.onComplete(new WatchFileResult().setRevision(new TRevision(42))
Expand All @@ -450,7 +427,7 @@ public void watchFile() throws Exception {
}

@Test
public void watchFileTimedOut() throws Exception {
void watchFileTimedOut() throws Exception {
doAnswer(invocation -> {
AsyncMethodCallback<WatchFileResult> callback = invocation.getArgument(5);
callback.onComplete(new WatchFileResult());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2018 LINE Corporation
* Copyright 2020 LINE Corporation
*
* LINE Corporation licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
Expand All @@ -22,12 +22,9 @@

import java.util.List;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;

import com.google.common.collect.ImmutableList;

Expand All @@ -40,42 +37,40 @@
import com.linecorp.centraldogma.internal.api.v1.WatchTimeout;
import com.linecorp.centraldogma.internal.thrift.CentralDogmaService;

public class LegacyCentralDogmaTimeoutSchedulerTest {
@Rule
public MockitoRule rule = MockitoJUnit.rule();
class LegacyCentralDogmaTimeoutSchedulerTest {

@Mock
private RpcClient client;

private LegacyCentralDogmaTimeoutScheduler decorator;

@Before
public void setup() {
@BeforeEach
void setUp() {
decorator = new LegacyCentralDogmaTimeoutScheduler(client);
}

@Test
public void execute() throws Exception {
void execute() throws Exception {
check("listProjects", 1000L, 1L, 1L);
}

@Test
public void execute_watchFile() throws Exception {
void execute_watchFile() throws Exception {
check("watchFile", 1000L, 1L, 1001L);
}

@Test
public void execute_watchRepository() throws Exception {
void execute_watchRepository() throws Exception {
check("watchRepository", 1000L, 1L, 1001L);
}

@Test
public void execute_watch_timeoutOverflow() throws Exception {
void execute_watch_timeoutOverflow() throws Exception {
check("watchRepository", Long.MAX_VALUE - 10, 100L, WatchTimeout.MAX_MILLIS);
}

@Test
public void execute_noTimeout() throws Exception {
void execute_noTimeout() throws Exception {
check("watchFile", 1000L, 0, 0);
}

Expand Down

0 comments on commit 346a975

Please sign in to comment.