Skip to content

Commit

Permalink
Add APIs with assock key param fluentapi (linkedin#613)
Browse files Browse the repository at this point in the history
* Add assoc key param method to fluent api
  • Loading branch information
junchuanwang authored and Lester Haynes committed May 14, 2021
1 parent 7a323c0 commit 487a1bb
Show file tree
Hide file tree
Showing 16 changed files with 736 additions and 387 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@
} ],
"entity" : {
"path" : "/associations/{associationsId}",
"actions" : [ {
"name" : "testAction",
"returns" : "string"
} ],
"subresources" : [ {
"name" : "associationsAssociations",
"namespace" : "com.linkedin.restli.examples.greetings.client",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@
} ],
"entity" : {
"path" : "/associations/{associationsId}",
"actions" : [ {
"name" : "testAction",
"returns" : "string"
} ],
"subresources" : [ {
"name" : "associationsAssociations",
"namespace" : "com.linkedin.restli.examples.greetings.client",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@
import com.linkedin.restli.server.CollectionResult;
import com.linkedin.restli.server.CreateResponse;
import com.linkedin.restli.server.PagingContext;
import com.linkedin.restli.server.ResourceLevel;
import com.linkedin.restli.server.RestLiServiceException;
import com.linkedin.restli.server.UpdateResponse;
import com.linkedin.restli.server.annotations.Action;
import com.linkedin.restli.server.annotations.AssocKeyParam;
import com.linkedin.restli.server.annotations.BatchFinder;
import com.linkedin.restli.server.annotations.Finder;
Expand Down Expand Up @@ -165,4 +167,11 @@ public BatchFinderResult<MessageCriteria, Message, Empty> searchMessages(@AssocK

return batchFinderResult;
}

@Action(name = "testAction", resourceLevel = ResourceLevel.ENTITY)
public String testAction()
{
return "Hello!";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ public AltKeyDataProvider()
key2.append("message", "b");
key2.append("greetingId", 2L);

CompoundKey key3 = new CompoundKey();
key3.append("message", "c");
key3.append("greetingId", 3L);

Greeting greeting1 = new Greeting();
greeting1.setTone(Tone.INSULTING);
greeting1.setId(1l);
Expand All @@ -56,10 +60,20 @@ public AltKeyDataProvider()
greeting2.setId(2l);
greeting2.setMessage("b");

Greeting greeting3 = new Greeting();
greeting3.setTone(Tone.FRIENDLY);
greeting3.setId(3l);
greeting3.setMessage("c");

_db1.put(1L, greeting1);
_db1.put(2L, greeting2);
_db2.put(key1, greeting1);
_db2.put(key2, greeting2);
_db2.put(key3, greeting3);
}
private void create(CompoundKey id, Greeting entity)
{
_db2.put(id, entity);
}

public Greeting get(Long id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.linkedin.restli.server.ResourceLevel;
import com.linkedin.restli.server.UpdateResponse;
import com.linkedin.restli.server.annotations.Action;
import com.linkedin.restli.server.annotations.ActionParam;
import com.linkedin.restli.server.annotations.AlternativeKey;
import com.linkedin.restli.server.annotations.Key;
import com.linkedin.restli.server.annotations.RestLiAssociation;
Expand Down Expand Up @@ -108,6 +109,7 @@ public UpdateResponse update(CompoundKey key, PatchRequest<Greeting> patch)
return new UpdateResponse(HttpStatus.S_400_BAD_REQUEST);
}

update(key, g);
return new UpdateResponse(HttpStatus.S_204_NO_CONTENT);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
import com.linkedin.restli.examples.greetings.api.TwoPartKey;
import com.linkedin.restli.examples.greetings.client.Actions;
import com.linkedin.restli.examples.greetings.client.ActionsFluentClient;
import com.linkedin.restli.examples.greetings.client.AssociationAltKey;
import com.linkedin.restli.examples.greetings.client.AssociationAltKeyFluentClient;
import com.linkedin.restli.examples.greetings.client.Associations;
import com.linkedin.restli.examples.greetings.client.AssociationsAssociationsFluentClient;
import com.linkedin.restli.examples.greetings.client.AssociationsAssociationsSubFluentClient;
Expand Down Expand Up @@ -796,6 +798,50 @@ public void testAssociationBatchFinderUsingAssocKey() throws Exception
Assert.assertEquals( (int) insulting.getError().getStatus(), 404);
}

@Test public void testAssociateResourceSpreadKeyAPI() throws Exception
{
// Use AssocationAltKeyResource and AltKeyDataProvider

// Get
AssociationAltKey client = new AssociationAltKeyFluentClient(_parSeqRestliClient, _parSeqUnitTestHelper.getEngine());
String msgKey = "c";
Long longKey = 3L;
Greeting res = client.get(longKey, msgKey).toCompletableFuture().get(5000, TimeUnit.MILLISECONDS);
Assert.assertEquals(res.getTone(), Tone.FRIENDLY);
Assert.assertEquals(res.getMessage(), msgKey);

// Update
String newMsg = "aa";
res.setMessage(newMsg);
client.update(longKey, msgKey, res).toCompletableFuture().get(5000, TimeUnit.MILLISECONDS);
res = client.get(longKey, msgKey).toCompletableFuture().get(5000, TimeUnit.MILLISECONDS);
Assert.assertEquals(res.getMessage(), newMsg);
// PartialUpdate
Tone updatedTone = Tone.SINCERE;
res.setTone(updatedTone);
Greeting original = client.get(longKey, msgKey).toCompletableFuture().get(5000, TimeUnit.MILLISECONDS);
PatchRequest<Greeting> patch = PatchGenerator.diff(original, res);
client.partialUpdate(longKey, msgKey, patch).toCompletableFuture().get(5000, TimeUnit.MILLISECONDS); // Only tone differ
res = client.get(longKey, msgKey).toCompletableFuture().get(5000, TimeUnit.MILLISECONDS);
Assert.assertEquals(res.getMessage(), newMsg);
Assert.assertEquals(res.getTone(), updatedTone);

// Delete
try
{
// that resource implementation does not allow deletion
client.delete(longKey, msgKey).toCompletableFuture().get(5000, TimeUnit.MILLISECONDS);
}
catch (ExecutionException e)
{
Assert.assertEquals(((RestLiResponseException) e.getCause()).getStatus(), 404);
}

// Action
Assert.assertEquals(client.testAction(longKey, msgKey)
.toCompletableFuture().get(5000, TimeUnit.MILLISECONDS), "Hello!");
}

// ----- Test with Sub Resources ------
// These tests is to verify subresources methods in FluentClient subresources
// works as expected as non-subresources.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,5 +208,15 @@ public boolean hasProjectionParams()
{
return getSupportedProjectionParams().size() > 0;
}

public List<CompoundKeySpec.AssocKeySpec> getAssocKeys()
{
if (_resourceSpec instanceof AssociationResourceSpec)
{
return new ArrayList<>(((AssociationResourceSpec) _resourceSpec).getCompoundKeySpec().getAssocKeySpecs());
}

return Collections.emptyList();
}
}

Loading

0 comments on commit 487a1bb

Please sign in to comment.