diff --git a/jaeger-core/src/test/java/com/uber/jaeger/mocks/MockAgentResource.java b/jaeger-core/src/test/java/com/uber/jaeger/mocks/MockAgentResource.java index 0e49c38..5f389d8 100644 --- a/jaeger-core/src/test/java/com/uber/jaeger/mocks/MockAgentResource.java +++ b/jaeger-core/src/test/java/com/uber/jaeger/mocks/MockAgentResource.java @@ -20,6 +20,7 @@ import javax.ws.rs.QueryParam; import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.MediaType; +import java.util.List; @Path("") public class MockAgentResource { @@ -44,4 +45,24 @@ public String getBaggageRestrictions(@QueryParam("service") String serviceName) } throw new WebApplicationException(); } + + @GET + @Path("credits") + @Produces(MediaType.APPLICATION_JSON) + public String getCredits(@QueryParam("uuid") int clientId, + @QueryParam("service") String serviceName, + @QueryParam("operations") List operations) { + if (serviceName.equals("clairvoyant")) { + StringBuffer buffer = new StringBuffer("{ \"balances\": ["); + for (int i = 0; i < operations.size(); i++) { + if (i > 0) { + buffer.append(", "); + } + buffer.append("{ \"operation\": \"" + operations.get(i) + "\", \"balance\": 1 }"); + } + buffer.append("]}"); + return buffer.toString(); + } + throw new WebApplicationException(); + } } diff --git a/jaeger-core/src/test/java/com/uber/jaeger/throttler/HttpThrottlerProxyTest.java b/jaeger-core/src/test/java/com/uber/jaeger/throttler/HttpThrottlerProxyTest.java new file mode 100644 index 0000000..0b482a7 --- /dev/null +++ b/jaeger-core/src/test/java/com/uber/jaeger/throttler/HttpThrottlerProxyTest.java @@ -0,0 +1,67 @@ +package com.uber.jaeger.throttler; + +import com.uber.jaeger.mocks.MockAgentResource; +import com.uber.jaeger.throttler.http.CreditResponse; +import org.glassfish.jersey.server.ResourceConfig; +import org.glassfish.jersey.test.JerseyTest; +import org.glassfish.jersey.test.TestProperties; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +import javax.ws.rs.core.Application; +import java.net.URI; +import java.util.ArrayList; +import java.util.List; +import java.util.Properties; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +public class HttpThrottlerProxyTest extends JerseyTest { + private static Properties originalProps; + private static final int CLIENT_ID = 1234; + private static final String OPERATION_NAME = "test-operation"; + + private HttpThrottlerProxy proxy; + + @BeforeClass + public static void beforeClass() { + originalProps = new Properties(System.getProperties()); + if (System.getProperty(TestProperties.CONTAINER_PORT) == null) { + System.setProperty(TestProperties.CONTAINER_PORT, "0"); + } + } + + @AfterClass + public static void afterClass() { + System.setProperties(originalProps); + } + + @Override + protected Application configure() { + + return new ResourceConfig(MockAgentResource.class); + } + + @Test + public void testGetCredits() throws Exception { + URI uri = target().getUri(); + proxy = new HttpThrottlerProxy(uri.getHost() + ":" + uri.getPort()); + final List operations = new ArrayList(); + operations.add(OPERATION_NAME); + CreditResponse response = proxy.getCredits(CLIENT_ID, "clairvoyant", operations); + assertNotNull(response); + assertEquals(1, response.getBalances().size()); + assertEquals(OPERATION_NAME, response.getBalances().get(0).getOperation()); + assertEquals(1, response.getBalances().get(0).getBalance(), 0.0001); + + try { + proxy.getCredits(CLIENT_ID, "invalid-service", operations); + } catch (ThrottlerException e) { + return; + } + + assert(false); + } +}