Search in sources :

Example 46 with ComplexResourceKey

use of com.linkedin.restli.common.ComplexResourceKey in project incubator-gobblin by apache.

the class RestClientRequestSender method sendRequest.

@Override
public void sendRequest(PermitRequest request, Callback<Response<PermitAllocation>> callback) {
    PermitsGetRequestBuilder getBuilder = new PermitsRequestBuilders().get();
    Request<PermitAllocation> fullRequest = getBuilder.id(new ComplexResourceKey<>(request, new EmptyRecord())).build();
    getRestClient().sendRequest(fullRequest, decorateCallback(request, callback));
}
Also used : EmptyRecord(com.linkedin.restli.common.EmptyRecord) ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey) PermitsGetRequestBuilder(org.apache.gobblin.restli.throttling.PermitsGetRequestBuilder) PermitsRequestBuilders(org.apache.gobblin.restli.throttling.PermitsRequestBuilders) PermitAllocation(org.apache.gobblin.restli.throttling.PermitAllocation)

Example 47 with ComplexResourceKey

use of com.linkedin.restli.common.ComplexResourceKey in project incubator-gobblin by apache.

the class LimiterServerResourceTest method testLimitedRequests.

@Test
public void testLimitedRequests() {
    ThrottlingPolicyFactory factory = new ThrottlingPolicyFactory();
    SharedLimiterKey res1key = new SharedLimiterKey("res1");
    SharedLimiterKey res2key = new SharedLimiterKey("res2");
    Map<String, String> configMap = ImmutableMap.<String, String>builder().put(BrokerConfigurationKeyGenerator.generateKey(factory, res1key, null, ThrottlingPolicyFactory.POLICY_KEY), CountBasedPolicy.FACTORY_ALIAS).put(BrokerConfigurationKeyGenerator.generateKey(factory, res1key, null, CountBasedPolicy.COUNT_KEY), "100").put(BrokerConfigurationKeyGenerator.generateKey(factory, res2key, null, ThrottlingPolicyFactory.POLICY_KEY), CountBasedPolicy.FACTORY_ALIAS).put(BrokerConfigurationKeyGenerator.generateKey(factory, res2key, null, CountBasedPolicy.COUNT_KEY), "50").build();
    ThrottlingGuiceServletConfig guiceServletConfig = new ThrottlingGuiceServletConfig();
    guiceServletConfig.initialize(ConfigFactory.parseMap(configMap));
    Injector injector = guiceServletConfig.getInjector();
    LimiterServerResource limiterServer = injector.getInstance(LimiterServerResource.class);
    PermitRequest res1request = new PermitRequest();
    res1request.setPermits(20);
    res1request.setResource(res1key.getResourceLimitedPath());
    PermitRequest res2request = new PermitRequest();
    res2request.setPermits(20);
    res2request.setResource(res2key.getResourceLimitedPath());
    PermitRequest res3request = new PermitRequest();
    res3request.setPermits(100000);
    res3request.setResource("res3");
    Assert.assertEquals(limiterServer.getSync(new ComplexResourceKey<>(res1request, new EmptyRecord())).getPermits(), new Long(20));
    Assert.assertEquals(limiterServer.getSync(new ComplexResourceKey<>(res1request, new EmptyRecord())).getPermits(), new Long(20));
    Assert.assertEquals(limiterServer.getSync(new ComplexResourceKey<>(res1request, new EmptyRecord())).getPermits(), new Long(20));
    Assert.assertEquals(limiterServer.getSync(new ComplexResourceKey<>(res1request, new EmptyRecord())).getPermits(), new Long(20));
    Assert.assertEquals(limiterServer.getSync(new ComplexResourceKey<>(res1request, new EmptyRecord())).getPermits(), new Long(20));
    try {
        // out of permits
        limiterServer.getSync(new ComplexResourceKey<>(res1request, new EmptyRecord())).getPermits();
        Assert.fail();
    } catch (RestLiServiceException exc) {
        Assert.assertEquals(exc.getStatus(), HttpStatus.S_403_FORBIDDEN);
    }
    Assert.assertEquals(limiterServer.getSync(new ComplexResourceKey<>(res2request, new EmptyRecord())).getPermits(), new Long(20));
    Assert.assertEquals(limiterServer.getSync(new ComplexResourceKey<>(res2request, new EmptyRecord())).getPermits(), new Long(20));
    // out of permits
    try {
        // out of permits
        limiterServer.getSync(new ComplexResourceKey<>(res2request, new EmptyRecord())).getPermits();
        Assert.fail();
    } catch (RestLiServiceException exc) {
        Assert.assertEquals(exc.getStatus(), HttpStatus.S_403_FORBIDDEN);
    }
    // No limit
    Assert.assertTrue(limiterServer.getSync(new ComplexResourceKey<>(res3request, new EmptyRecord())).getPermits() >= res3request.getPermits());
}
Also used : EmptyRecord(com.linkedin.restli.common.EmptyRecord) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) Injector(com.google.inject.Injector) ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey) SharedLimiterKey(org.apache.gobblin.util.limiter.broker.SharedLimiterKey) Test(org.testng.annotations.Test)

Example 48 with ComplexResourceKey

use of com.linkedin.restli.common.ComplexResourceKey in project rest.li by linkedin.

the class TestParseqBasedFluentClientApi method testComplexKey_batchDelete.

@Test
public void testComplexKey_batchDelete() throws Exception {
    String messageText = "m1";
    Message message = new Message();
    message.setMessage(messageText);
    ComplexKeys complexKeyClient = new ComplexKeysFluentClient(_parSeqRestliClient, _parSeqUnitTestHelper.getEngine());
    ComplexResourceKey<TwoPartKey, TwoPartKey> createResponse = complexKeyClient.create(message).toCompletableFuture().get(5000, TimeUnit.MILLISECONDS);
    String messageText2 = "m2";
    message.setMessage(messageText2);
    createResponse = complexKeyClient.create(message).toCompletableFuture().get(5000, TimeUnit.MILLISECONDS);
    ComplexResourceKey<TwoPartKey, TwoPartKey> key1 = getComplexKey(messageText, messageText);
    ComplexResourceKey<TwoPartKey, TwoPartKey> key2 = getComplexKey(messageText2, messageText2);
    ArrayList<ComplexResourceKey<TwoPartKey, TwoPartKey>> ids = new ArrayList<>();
    ids.add(key1);
    ids.add(key2);
    Map<ComplexResourceKey<TwoPartKey, TwoPartKey>, UpdateStatus> deleteResponse = complexKeyClient.batchDelete(new HashSet<>(ids)).toCompletableFuture().get(5000, TimeUnit.MILLISECONDS);
    Assert.assertEquals(deleteResponse.size(), ids.size());
    Assert.assertEquals(deleteResponse.get(key1).getStatus().intValue(), 204);
    Assert.assertEquals(deleteResponse.get(key2).getStatus().intValue(), 204);
    Map<ComplexResourceKey<TwoPartKey, TwoPartKey>, EntityResponse<Message>> getResponse = complexKeyClient.batchGet(new HashSet<>(ids)).toCompletableFuture().get(5000, TimeUnit.MILLISECONDS);
    Assert.assertEquals(getResponse.get(key1).getError().getStatus().intValue(), 404);
    Assert.assertEquals(getResponse.get(key2).getError().getStatus().intValue(), 404);
}
Also used : TwoPartKey(com.linkedin.restli.examples.greetings.api.TwoPartKey) UpdateStatus(com.linkedin.restli.common.UpdateStatus) Message(com.linkedin.restli.examples.greetings.api.Message) ArrayList(java.util.ArrayList) EntityResponse(com.linkedin.restli.common.EntityResponse) IdEntityResponse(com.linkedin.restli.common.IdEntityResponse) ComplexKeys(com.linkedin.restli.examples.greetings.client.ComplexKeys) ComplexKeysFluentClient(com.linkedin.restli.examples.greetings.client.ComplexKeysFluentClient) ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey) Test(org.testng.annotations.Test)

Example 49 with ComplexResourceKey

use of com.linkedin.restli.common.ComplexResourceKey in project rest.li by linkedin.

the class TestParseqBasedFluentClientApi method testComplexKey_batchUpdate.

@Test
public void testComplexKey_batchUpdate() throws Exception {
    ComplexKeys complexKeyClient = new ComplexKeysFluentClient(_parSeqRestliClient, _parSeqUnitTestHelper.getEngine());
    final String messageText = StringTestKeys.SIMPLEKEY + " " + StringTestKeys.SIMPLEKEY2;
    final Message message = new Message();
    message.setId(StringTestKeys.SIMPLEKEY + " " + StringTestKeys.SIMPLEKEY2);
    message.setMessage(messageText);
    message.setTone(Tone.INSULTING);
    final String messageText2 = StringTestKeys.URL + " " + StringTestKeys.URL2;
    final Message message2 = new Message();
    message2.setId(StringTestKeys.URL + " " + StringTestKeys.URL2);
    message2.setMessage(messageText2);
    message2.setTone(Tone.INSULTING);
    final Map<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message> inputs = new HashMap<>();
    ComplexResourceKey<TwoPartKey, TwoPartKey> key1 = getComplexKey(StringTestKeys.SIMPLEKEY, StringTestKeys.SIMPLEKEY2);
    ComplexResourceKey<TwoPartKey, TwoPartKey> key2 = getComplexKey(StringTestKeys.URL, StringTestKeys.URL2);
    inputs.put(key1, message);
    inputs.put(key2, message2);
    complexKeyClient.batchUpdate(inputs).toCompletableFuture().get(5000, TimeUnit.MILLISECONDS);
    Map<ComplexResourceKey<TwoPartKey, TwoPartKey>, EntityResponse<Message>> result = complexKeyClient.batchGet(new HashSet<>(Arrays.asList(key1, key2))).toCompletableFuture().get(5000, TimeUnit.MILLISECONDS);
    Assert.assertNotNull(result.get(key1));
    Assert.assertNotNull(result.get(key2));
    Assert.assertEquals(result.get(key1).getEntity().getTone(), Tone.INSULTING);
    Assert.assertEquals(result.get(key2).getEntity().getTone(), Tone.INSULTING);
}
Also used : TwoPartKey(com.linkedin.restli.examples.greetings.api.TwoPartKey) Message(com.linkedin.restli.examples.greetings.api.Message) HashMap(java.util.HashMap) EntityResponse(com.linkedin.restli.common.EntityResponse) IdEntityResponse(com.linkedin.restli.common.IdEntityResponse) ComplexKeys(com.linkedin.restli.examples.greetings.client.ComplexKeys) ComplexKeysFluentClient(com.linkedin.restli.examples.greetings.client.ComplexKeysFluentClient) ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey) Test(org.testng.annotations.Test)

Example 50 with ComplexResourceKey

use of com.linkedin.restli.common.ComplexResourceKey in project rest.li by linkedin.

the class TestParseqBasedFluentClientApi method testComplexKey_batchGet.

@Test
public void testComplexKey_batchGet() throws Exception {
    List<ComplexResourceKey<TwoPartKey, TwoPartKey>> ids = getBatchComplexKeys();
    ComplexKeys complexKeyClient = new ComplexKeysFluentClient(_parSeqRestliClient, _parSeqUnitTestHelper.getEngine());
    Map<ComplexResourceKey<TwoPartKey, TwoPartKey>, EntityResponse<Message>> resultMap = complexKeyClient.batchGet(new HashSet<>(ids)).toCompletableFuture().get(5000, TimeUnit.MILLISECONDS);
    Assert.assertEquals(resultMap.size(), 3);
    Assert.assertNotNull(resultMap.get(ids.get(0)).getEntity());
    Assert.assertNotNull(resultMap.get(ids.get(1)).getEntity());
    Assert.assertNotNull(resultMap.get(ids.get(2)).getError());
}
Also used : EntityResponse(com.linkedin.restli.common.EntityResponse) IdEntityResponse(com.linkedin.restli.common.IdEntityResponse) ComplexKeys(com.linkedin.restli.examples.greetings.client.ComplexKeys) ComplexKeysFluentClient(com.linkedin.restli.examples.greetings.client.ComplexKeysFluentClient) ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey) Test(org.testng.annotations.Test)

Aggregations

ComplexResourceKey (com.linkedin.restli.common.ComplexResourceKey)89 Test (org.testng.annotations.Test)43 TwoPartKey (com.linkedin.restli.examples.greetings.api.TwoPartKey)19 TestRecord (com.linkedin.restli.client.test.TestRecord)18 CompoundKey (com.linkedin.restli.common.CompoundKey)18 HashMap (java.util.HashMap)17 EmptyRecord (com.linkedin.restli.common.EmptyRecord)16 DataMap (com.linkedin.data.DataMap)15 Message (com.linkedin.restli.examples.greetings.api.Message)14 ResourceModel (com.linkedin.restli.internal.server.model.ResourceModel)13 Key (com.linkedin.restli.server.Key)13 RecordTemplate (com.linkedin.data.template.RecordTemplate)11 BatchKVResponse (com.linkedin.restli.client.response.BatchKVResponse)11 EntityResponse (com.linkedin.restli.common.EntityResponse)11 ResourceMethodDescriptor (com.linkedin.restli.internal.server.model.ResourceMethodDescriptor)11 RestLiTestHelper.buildResourceModel (com.linkedin.restli.server.test.RestLiTestHelper.buildResourceModel)11 DiscoveredItemKey (com.linkedin.restli.server.twitter.TwitterTestDataModels.DiscoveredItemKey)11 DiscoveredItemKeyParams (com.linkedin.restli.server.twitter.TwitterTestDataModels.DiscoveredItemKeyParams)11 HashSet (java.util.HashSet)11 AfterTest (org.testng.annotations.AfterTest)11