Migrate RemoteConfigControllerTest to JUnit 5
This commit is contained in:
parent
831023e41d
commit
208a09b3ae
|
@ -5,13 +5,35 @@
|
||||||
|
|
||||||
package org.whispersystems.textsecuregcm.tests.controllers;
|
package org.whispersystems.textsecuregcm.tests.controllers;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.reset;
|
||||||
|
import static org.mockito.Mockito.times;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
import com.google.common.collect.ImmutableSet;
|
import com.google.common.collect.ImmutableSet;
|
||||||
import io.dropwizard.auth.PolymorphicAuthValueFactoryProvider;
|
import io.dropwizard.auth.PolymorphicAuthValueFactoryProvider;
|
||||||
import io.dropwizard.testing.junit.ResourceTestRule;
|
import io.dropwizard.testing.junit5.DropwizardExtensionsSupport;
|
||||||
|
import io.dropwizard.testing.junit5.ResourceExtension;
|
||||||
|
import java.security.MessageDigest;
|
||||||
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Random;
|
||||||
|
import java.util.Set;
|
||||||
|
import javax.ws.rs.client.Entity;
|
||||||
|
import javax.ws.rs.core.MediaType;
|
||||||
|
import javax.ws.rs.core.Response;
|
||||||
import org.glassfish.jersey.test.grizzly.GrizzlyWebTestContainerFactory;
|
import org.glassfish.jersey.test.grizzly.GrizzlyWebTestContainerFactory;
|
||||||
import org.junit.Before;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.Rule;
|
import org.junit.jupiter.api.AfterEach;
|
||||||
import org.junit.Test;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
import org.mockito.ArgumentCaptor;
|
import org.mockito.ArgumentCaptor;
|
||||||
import org.whispersystems.textsecuregcm.auth.DisabledPermittedAccount;
|
import org.whispersystems.textsecuregcm.auth.DisabledPermittedAccount;
|
||||||
import org.whispersystems.textsecuregcm.controllers.RemoteConfigController;
|
import org.whispersystems.textsecuregcm.controllers.RemoteConfigController;
|
||||||
|
@ -23,43 +45,23 @@ import org.whispersystems.textsecuregcm.storage.RemoteConfig;
|
||||||
import org.whispersystems.textsecuregcm.storage.RemoteConfigsManager;
|
import org.whispersystems.textsecuregcm.storage.RemoteConfigsManager;
|
||||||
import org.whispersystems.textsecuregcm.tests.util.AuthHelper;
|
import org.whispersystems.textsecuregcm.tests.util.AuthHelper;
|
||||||
|
|
||||||
import javax.ws.rs.client.Entity;
|
@ExtendWith(DropwizardExtensionsSupport.class)
|
||||||
import javax.ws.rs.core.MediaType;
|
class RemoteConfigControllerTest {
|
||||||
import javax.ws.rs.core.Response;
|
|
||||||
import java.security.MessageDigest;
|
|
||||||
import java.security.NoSuchAlgorithmException;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.LinkedList;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Random;
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
private static final RemoteConfigsManager remoteConfigsManager = mock(RemoteConfigsManager.class);
|
||||||
import static org.mockito.Mockito.mock;
|
private static final List<String> remoteConfigsAuth = List.of("foo", "bar");
|
||||||
import static org.mockito.Mockito.times;
|
|
||||||
import static org.mockito.Mockito.verify;
|
|
||||||
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
|
||||||
import static org.mockito.Mockito.when;
|
|
||||||
|
|
||||||
public class RemoteConfigControllerTest {
|
private static final ResourceExtension resources = ResourceExtension.builder()
|
||||||
|
.addProvider(AuthHelper.getAuthFilter())
|
||||||
private final RemoteConfigsManager remoteConfigsManager = mock(RemoteConfigsManager.class);
|
.addProvider(new PolymorphicAuthValueFactoryProvider.Binder<>(ImmutableSet.of(Account.class, DisabledPermittedAccount.class)))
|
||||||
private final List<String> remoteConfigsAuth = List.of("foo", "bar");
|
.setTestContainerFactory(new GrizzlyWebTestContainerFactory())
|
||||||
|
.addProvider(new DeviceLimitExceededExceptionMapper())
|
||||||
@Rule
|
.addResource(new RemoteConfigController(remoteConfigsManager, remoteConfigsAuth, Map.of("maxGroupSize", "42")))
|
||||||
public final ResourceTestRule resources = ResourceTestRule.builder()
|
.build();
|
||||||
.addProvider(AuthHelper.getAuthFilter())
|
|
||||||
.addProvider(new PolymorphicAuthValueFactoryProvider.Binder<>(ImmutableSet.of(Account.class, DisabledPermittedAccount.class)))
|
|
||||||
.setTestContainerFactory(new GrizzlyWebTestContainerFactory())
|
|
||||||
.addProvider(new DeviceLimitExceededExceptionMapper())
|
|
||||||
.addResource(new RemoteConfigController(remoteConfigsManager, remoteConfigsAuth, Map.of("maxGroupSize", "42")))
|
|
||||||
.build();
|
|
||||||
|
|
||||||
|
|
||||||
@Before
|
@BeforeEach
|
||||||
public void setup() {
|
void setup() {
|
||||||
when(remoteConfigsManager.getAll()).thenReturn(new LinkedList<>() {{
|
when(remoteConfigsManager.getAll()).thenReturn(new LinkedList<>() {{
|
||||||
add(new RemoteConfig("android.stickers", 25, Set.of(AuthHelper.DISABLED_UUID, AuthHelper.INVALID_UUID), null, null, null));
|
add(new RemoteConfig("android.stickers", 25, Set.of(AuthHelper.DISABLED_UUID, AuthHelper.INVALID_UUID), null, null, null));
|
||||||
add(new RemoteConfig("ios.stickers", 50, Set.of(), null, null, null));
|
add(new RemoteConfig("ios.stickers", 50, Set.of(), null, null, null));
|
||||||
|
@ -74,8 +76,13 @@ public class RemoteConfigControllerTest {
|
||||||
}});
|
}});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@AfterEach
|
||||||
|
void teardown() {
|
||||||
|
reset(remoteConfigsManager);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testRetrieveConfig() {
|
void testRetrieveConfig() {
|
||||||
UserRemoteConfigList configuration = resources.getJerseyTest()
|
UserRemoteConfigList configuration = resources.getJerseyTest()
|
||||||
.target("/v1/config/")
|
.target("/v1/config/")
|
||||||
.request()
|
.request()
|
||||||
|
@ -109,7 +116,7 @@ public class RemoteConfigControllerTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testRetrieveConfigNotSpecial() {
|
void testRetrieveConfigNotSpecial() {
|
||||||
UserRemoteConfigList configuration = resources.getJerseyTest()
|
UserRemoteConfigList configuration = resources.getJerseyTest()
|
||||||
.target("/v1/config/")
|
.target("/v1/config/")
|
||||||
.request()
|
.request()
|
||||||
|
@ -143,7 +150,7 @@ public class RemoteConfigControllerTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testHashKeyLinkedConfigs() {
|
void testHashKeyLinkedConfigs() {
|
||||||
boolean allUnlinkedConfigsMatched = true;
|
boolean allUnlinkedConfigsMatched = true;
|
||||||
for (AuthHelper.TestAccount testAccount : AuthHelper.TEST_ACCOUNTS) {
|
for (AuthHelper.TestAccount testAccount : AuthHelper.TEST_ACCOUNTS) {
|
||||||
UserRemoteConfigList configuration = resources.getJerseyTest().target("/v1/config/").request().header("Authorization", testAccount.getAuthHeader()).get(UserRemoteConfigList.class);
|
UserRemoteConfigList configuration = resources.getJerseyTest().target("/v1/config/").request().header("Authorization", testAccount.getAuthHeader()).get(UserRemoteConfigList.class);
|
||||||
|
@ -166,7 +173,7 @@ public class RemoteConfigControllerTest {
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testRetrieveConfigUnauthorized() {
|
void testRetrieveConfigUnauthorized() {
|
||||||
Response response = resources.getJerseyTest()
|
Response response = resources.getJerseyTest()
|
||||||
.target("/v1/config/")
|
.target("/v1/config/")
|
||||||
.request()
|
.request()
|
||||||
|
@ -180,7 +187,7 @@ public class RemoteConfigControllerTest {
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSetConfig() {
|
void testSetConfig() {
|
||||||
Response response = resources.getJerseyTest()
|
Response response = resources.getJerseyTest()
|
||||||
.target("/v1/config")
|
.target("/v1/config")
|
||||||
.request()
|
.request()
|
||||||
|
@ -199,7 +206,7 @@ public class RemoteConfigControllerTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSetConfigValued() {
|
void testSetConfigValued() {
|
||||||
Response response = resources.getJerseyTest()
|
Response response = resources.getJerseyTest()
|
||||||
.target("/v1/config")
|
.target("/v1/config")
|
||||||
.request()
|
.request()
|
||||||
|
@ -218,7 +225,7 @@ public class RemoteConfigControllerTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSetConfigWithHashKey() {
|
void testSetConfigWithHashKey() {
|
||||||
Response response1 = resources.getJerseyTest()
|
Response response1 = resources.getJerseyTest()
|
||||||
.target("/v1/config")
|
.target("/v1/config")
|
||||||
.request()
|
.request()
|
||||||
|
@ -254,7 +261,7 @@ public class RemoteConfigControllerTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSetConfigUnauthorized() {
|
void testSetConfigUnauthorized() {
|
||||||
Response response = resources.getJerseyTest()
|
Response response = resources.getJerseyTest()
|
||||||
.target("/v1/config")
|
.target("/v1/config")
|
||||||
.request()
|
.request()
|
||||||
|
@ -267,7 +274,7 @@ public class RemoteConfigControllerTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSetConfigMissingUnauthorized() {
|
void testSetConfigMissingUnauthorized() {
|
||||||
Response response = resources.getJerseyTest()
|
Response response = resources.getJerseyTest()
|
||||||
.target("/v1/config")
|
.target("/v1/config")
|
||||||
.request()
|
.request()
|
||||||
|
@ -279,7 +286,7 @@ public class RemoteConfigControllerTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSetConfigBadName() {
|
void testSetConfigBadName() {
|
||||||
Response response = resources.getJerseyTest()
|
Response response = resources.getJerseyTest()
|
||||||
.target("/v1/config")
|
.target("/v1/config")
|
||||||
.request()
|
.request()
|
||||||
|
@ -292,7 +299,7 @@ public class RemoteConfigControllerTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSetConfigEmptyName() {
|
void testSetConfigEmptyName() {
|
||||||
Response response = resources.getJerseyTest()
|
Response response = resources.getJerseyTest()
|
||||||
.target("/v1/config")
|
.target("/v1/config")
|
||||||
.request()
|
.request()
|
||||||
|
@ -305,7 +312,7 @@ public class RemoteConfigControllerTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSetGlobalConfig() {
|
void testSetGlobalConfig() {
|
||||||
Response response = resources.getJerseyTest()
|
Response response = resources.getJerseyTest()
|
||||||
.target("/v1/config")
|
.target("/v1/config")
|
||||||
.request()
|
.request()
|
||||||
|
@ -316,7 +323,7 @@ public class RemoteConfigControllerTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testDelete() {
|
void testDelete() {
|
||||||
Response response = resources.getJerseyTest()
|
Response response = resources.getJerseyTest()
|
||||||
.target("/v1/config/android.stickers")
|
.target("/v1/config/android.stickers")
|
||||||
.request()
|
.request()
|
||||||
|
@ -330,7 +337,7 @@ public class RemoteConfigControllerTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testDeleteUnauthorized() {
|
void testDeleteUnauthorized() {
|
||||||
Response response = resources.getJerseyTest()
|
Response response = resources.getJerseyTest()
|
||||||
.target("/v1/config/android.stickers")
|
.target("/v1/config/android.stickers")
|
||||||
.request()
|
.request()
|
||||||
|
@ -343,7 +350,7 @@ public class RemoteConfigControllerTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testDeleteGlobalConfig() {
|
void testDeleteGlobalConfig() {
|
||||||
Response response = resources.getJerseyTest()
|
Response response = resources.getJerseyTest()
|
||||||
.target("/v1/config/global.maxGroupSize")
|
.target("/v1/config/global.maxGroupSize")
|
||||||
.request()
|
.request()
|
||||||
|
@ -354,7 +361,7 @@ public class RemoteConfigControllerTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testMath() throws NoSuchAlgorithmException {
|
void testMath() throws NoSuchAlgorithmException {
|
||||||
List<RemoteConfig> remoteConfigList = remoteConfigsManager.getAll();
|
List<RemoteConfig> remoteConfigList = remoteConfigsManager.getAll();
|
||||||
Map<String, Integer> enabledMap = new HashMap<>();
|
Map<String, Integer> enabledMap = new HashMap<>();
|
||||||
MessageDigest digest = MessageDigest.getInstance("SHA1");
|
MessageDigest digest = MessageDigest.getInstance("SHA1");
|
||||||
|
|
Loading…
Reference in New Issue