String uuid = result.getJSONObject("data").getString("id");
assertNotNull(uuid);
FakeRequest fk = new FakeRequest(GET, "/document/testFriendship/"+uuid);
fk = fk.withHeader(TestConfig.KEY_APPCODE, TestConfig.VALUE_APPCODE);
fk = fk.withHeader(TestConfig.KEY_AUTH, TestConfig.encodeAuth(firstUser, "passw1"));
Result getDocumentResult = routeAndCall(fk);
assertRoute(getDocumentResult, "Get document by friend", Status.NOT_FOUND, null, false);
//the owner of the doc gives grant to its friends
fk = new FakeRequest(PUT, "/document/testFriendship/"+uuid+"/read/role/friends_of_"+secondUser);
fk = fk.withHeader(TestConfig.KEY_APPCODE, TestConfig.VALUE_APPCODE);
fk = fk.withHeader(TestConfig.KEY_AUTH, TestConfig.encodeAuth(secondUser, "passw1"));
Result res = routeAndCall(fk);
assertRoute(res, "Change the document permission", Status.OK, null, false);
//now the friend reload the doc and read it
fk = new FakeRequest(GET, "/document/testFriendship/"+uuid);
fk = fk.withHeader(TestConfig.KEY_APPCODE, TestConfig.VALUE_APPCODE);
fk = fk.withHeader(TestConfig.KEY_AUTH, TestConfig.encodeAuth(firstUser, "passw1"));
getDocumentResult = routeAndCall(fk);
assertRoute(getDocumentResult, "Get document by friend 2", Status.OK, null, false);
JSONObject createdDocument = (JSONObject)toJSON(contentAsString(getDocumentResult));
String author = createdDocument.getJSONObject("data").getString("_author");
assertEquals(secondUser,author);
FakeRequest theFolloweds = new FakeRequest(GET, "/following");
theFolloweds = theFolloweds.withHeader(TestConfig.KEY_APPCODE, TestConfig.VALUE_APPCODE);
theFolloweds = theFolloweds.withHeader(TestConfig.KEY_AUTH, TestConfig.encodeAuth(firstUser, "passw1"));
Result getTheFollowedResult = routeAndCall(theFolloweds);
JSONObject followedJson = (JSONObject)toJSON(contentAsString(getTheFollowedResult));
String followed = followedJson.getJSONArray("data").getJSONObject(0).getJSONObject("user").getString("name");
assertEquals(followed,secondUser);
//check the followers
FakeRequest followers = new FakeRequest(GET, "/followers");
followers = followers.withHeader(TestConfig.KEY_APPCODE, TestConfig.VALUE_APPCODE);
followers = followers.withHeader(TestConfig.KEY_AUTH, TestConfig.encodeAuth(secondUser, "passw1"));
Result getFollowersResult = routeAndCall(followers);
JSONObject followersJson = (JSONObject)toJSON(contentAsString(getFollowersResult));
String follower = followersJson.getJSONArray("data").getJSONObject(0).getJSONObject("user").getString("name");
assertEquals(follower,firstUser);
//the first user get the followers of the second one
followers = new FakeRequest(GET, "/followers/"+secondUser);
followers = followers.withHeader(TestConfig.KEY_APPCODE, TestConfig.VALUE_APPCODE);
followers = followers.withHeader(TestConfig.KEY_AUTH, TestConfig.encodeAuth(firstUser, "passw1"));
getFollowersResult = routeAndCall(followers);
followersJson = (JSONObject)toJSON(contentAsString(getFollowersResult));
follower = followersJson.getJSONArray("data").getJSONObject(0).getJSONObject("user").getString("name");
assertEquals("test: GET /followers/:secondUser firstUser="+firstUser+" secondUser="+secondUser,follower,firstUser);
//the second user get the followed by the first one
FakeRequest followeds = new FakeRequest(GET, "/following/"+firstUser);
followeds = followeds.withHeader(TestConfig.KEY_APPCODE, TestConfig.VALUE_APPCODE);
followeds = followeds.withHeader(TestConfig.KEY_AUTH, TestConfig.encodeAuth(secondUser, "passw1"));
Result getFollowedsResult = routeAndCall(followeds);
JSONObject followedsJson = (JSONObject)toJSON(contentAsString(getFollowedsResult));
followed = followedsJson.getJSONArray("data").getJSONObject(0).getJSONObject("user").getString("name");
assertEquals("test: GET /following/:firstUser firstUser="+firstUser+" secondUser="+secondUser,followed,secondUser);
routeDeleteFollowRelationship(firstUser,secondUser);