Add contacts histogram and directory controller test.
This commit is contained in:
parent
aea3f299a0
commit
6913e4dfd2
|
@ -18,7 +18,9 @@ package org.whispersystems.textsecuregcm.controllers;
|
||||||
|
|
||||||
import com.google.common.base.Optional;
|
import com.google.common.base.Optional;
|
||||||
import com.yammer.dropwizard.auth.Auth;
|
import com.yammer.dropwizard.auth.Auth;
|
||||||
|
import com.yammer.metrics.Metrics;
|
||||||
import com.yammer.metrics.annotation.Timed;
|
import com.yammer.metrics.annotation.Timed;
|
||||||
|
import com.yammer.metrics.core.Histogram;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.whispersystems.textsecuregcm.entities.ClientContact;
|
import org.whispersystems.textsecuregcm.entities.ClientContact;
|
||||||
|
@ -46,7 +48,8 @@ import java.util.List;
|
||||||
@Path("/v1/directory")
|
@Path("/v1/directory")
|
||||||
public class DirectoryController {
|
public class DirectoryController {
|
||||||
|
|
||||||
private final Logger logger = LoggerFactory.getLogger(DirectoryController.class);
|
private final Logger logger = LoggerFactory.getLogger(DirectoryController.class);
|
||||||
|
private final Histogram contactsHistogram = Metrics.newHistogram(DirectoryController.class, "contacts");
|
||||||
|
|
||||||
private final RateLimiters rateLimiters;
|
private final RateLimiters rateLimiters;
|
||||||
private final DirectoryManager directory;
|
private final DirectoryManager directory;
|
||||||
|
@ -56,7 +59,7 @@ public class DirectoryController {
|
||||||
this.rateLimiters = rateLimiters;
|
this.rateLimiters = rateLimiters;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Timed()
|
@Timed
|
||||||
@GET
|
@GET
|
||||||
@Path("/{token}")
|
@Path("/{token}")
|
||||||
@Produces(MediaType.APPLICATION_JSON)
|
@Produces(MediaType.APPLICATION_JSON)
|
||||||
|
@ -77,7 +80,7 @@ public class DirectoryController {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Timed()
|
@Timed
|
||||||
@PUT
|
@PUT
|
||||||
@Path("/tokens")
|
@Path("/tokens")
|
||||||
@Produces(MediaType.APPLICATION_JSON)
|
@Produces(MediaType.APPLICATION_JSON)
|
||||||
|
@ -86,6 +89,7 @@ public class DirectoryController {
|
||||||
throws RateLimitExceededException
|
throws RateLimitExceededException
|
||||||
{
|
{
|
||||||
rateLimiters.getContactsLimiter().validate(account.getNumber(), contacts.getContacts().size());
|
rateLimiters.getContactsLimiter().validate(account.getNumber(), contacts.getContacts().size());
|
||||||
|
contactsHistogram.update(contacts.getContacts().size());
|
||||||
|
|
||||||
try {
|
try {
|
||||||
List<byte[]> tokens = new LinkedList<>();
|
List<byte[]> tokens = new LinkedList<>();
|
||||||
|
|
|
@ -30,4 +30,11 @@ public class ClientContactTokens {
|
||||||
public List<String> getContacts() {
|
public List<String> getContacts() {
|
||||||
return contacts;
|
return contacts;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ClientContactTokens() {}
|
||||||
|
|
||||||
|
public ClientContactTokens(List<String> contacts) {
|
||||||
|
this.contacts = contacts;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,74 @@
|
||||||
|
package org.whispersystems.textsecuregcm.tests.controllers;
|
||||||
|
|
||||||
|
import com.sun.jersey.api.client.ClientResponse;
|
||||||
|
import com.yammer.dropwizard.testing.ResourceTest;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.mockito.invocation.InvocationOnMock;
|
||||||
|
import org.mockito.stubbing.Answer;
|
||||||
|
import org.whispersystems.textsecuregcm.controllers.DirectoryController;
|
||||||
|
import org.whispersystems.textsecuregcm.entities.ClientContactTokens;
|
||||||
|
import org.whispersystems.textsecuregcm.limits.RateLimiter;
|
||||||
|
import org.whispersystems.textsecuregcm.limits.RateLimiters;
|
||||||
|
import org.whispersystems.textsecuregcm.storage.DirectoryManager;
|
||||||
|
import org.whispersystems.textsecuregcm.tests.util.AuthHelper;
|
||||||
|
import org.whispersystems.textsecuregcm.util.Base64;
|
||||||
|
|
||||||
|
import javax.ws.rs.core.MediaType;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.fest.assertions.api.Assertions.assertThat;
|
||||||
|
import static org.mockito.Matchers.anyList;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
public class DirectoryControllerTest extends ResourceTest {
|
||||||
|
|
||||||
|
private RateLimiters rateLimiters = mock(RateLimiters.class );
|
||||||
|
private RateLimiter rateLimiter = mock(RateLimiter.class );
|
||||||
|
private DirectoryManager directoryManager = mock(DirectoryManager.class);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void setUpResources() throws Exception {
|
||||||
|
addProvider(AuthHelper.getAuthenticator());
|
||||||
|
|
||||||
|
when(rateLimiters.getContactsLimiter()).thenReturn(rateLimiter);
|
||||||
|
when(directoryManager.get(anyList())).thenAnswer(new Answer<List<byte[]>>() {
|
||||||
|
@Override
|
||||||
|
public List<byte[]> answer(InvocationOnMock invocationOnMock) throws Throwable {
|
||||||
|
List<byte[]> query = (List<byte[]>) invocationOnMock.getArguments()[0];
|
||||||
|
List<byte[]> response = new LinkedList<>(query);
|
||||||
|
response.remove(0);
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
addResource(new DirectoryController(rateLimiters, directoryManager));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testContactIntersection() throws Exception {
|
||||||
|
List<String> tokens = new LinkedList<String>() {{
|
||||||
|
add(Base64.encodeBytes("foo".getBytes()));
|
||||||
|
add(Base64.encodeBytes("bar".getBytes()));
|
||||||
|
add(Base64.encodeBytes("baz".getBytes()));
|
||||||
|
}};
|
||||||
|
|
||||||
|
List<String> expectedResponse = new LinkedList<>(tokens);
|
||||||
|
expectedResponse.remove(0);
|
||||||
|
|
||||||
|
ClientResponse response =
|
||||||
|
client().resource("/v1/directory/tokens/")
|
||||||
|
.entity(new ClientContactTokens(tokens))
|
||||||
|
.type(MediaType.APPLICATION_JSON_TYPE)
|
||||||
|
.header("Authorization",
|
||||||
|
AuthHelper.getAuthHeader(AuthHelper.VALID_NUMBER,
|
||||||
|
AuthHelper.VALID_PASSWORD))
|
||||||
|
.put(ClientResponse.class);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
assertThat(response.getStatus()).isEqualTo(200);
|
||||||
|
assertThat(response.getEntity(ClientContactTokens.class).getContacts()).isEqualTo(expectedResponse);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue