use of i5.las2peer.services.noracleService.model.Vote in project Distributed-Noracle-Backend by Distributed-Noracle.
the class NoracleServiceTest method testVotes.
@Test
public void testVotes() {
try {
// create space, question, relation
String testSpaceId = createAndFetchTestSpace().getSpaceId();
String questionId1 = createTestQuestion(testSpaceId);
String questionId2 = createTestQuestion(testSpaceId);
String relationId = createTestQuestionRelation(testSpaceId, questionId1, questionId2).getRelationId();
// test get votes for all resources
VoteList question1Votes = getVotes("/" + SpacesResource.RESOURCE_NAME + "/" + testSpaceId + "/" + QuestionsResource.RESOURCE_NAME + "/" + questionId1 + "/" + QuestionVotesResource.RESOURCE_NAME);
Assert.assertEquals(0, question1Votes.size());
VoteList question2Votes = getVotes("/" + SpacesResource.RESOURCE_NAME + "/" + testSpaceId + "/" + QuestionsResource.RESOURCE_NAME + "/" + questionId2 + "/" + QuestionVotesResource.RESOURCE_NAME);
Assert.assertEquals(0, question2Votes.size());
VoteList relationVotes = getVotes("/" + SpacesResource.RESOURCE_NAME + "/" + testSpaceId + "/" + QuestionRelationsResource.RESOURCE_NAME + "/" + relationId + "/" + QuestionVotesResource.RESOURCE_NAME);
Assert.assertEquals(0, relationVotes.size());
// vote for each with one agent
setVote("/" + SpacesResource.RESOURCE_NAME + "/" + testSpaceId + "/" + QuestionsResource.RESOURCE_NAME + "/" + questionId1 + "/" + QuestionVotesResource.RESOURCE_NAME + "/" + testAgent.getIdentifier(), basicAuthHeader, 3);
setVote("/" + SpacesResource.RESOURCE_NAME + "/" + testSpaceId + "/" + QuestionsResource.RESOURCE_NAME + "/" + questionId2 + "/" + QuestionVotesResource.RESOURCE_NAME + "/" + testAgent.getIdentifier(), basicAuthHeader, 3);
setVote("/" + SpacesResource.RESOURCE_NAME + "/" + testSpaceId + "/" + QuestionRelationsResource.RESOURCE_NAME + "/" + relationId + "/" + QuestionVotesResource.RESOURCE_NAME + "/" + testAgent.getIdentifier(), basicAuthHeader, 3);
// check my votes for each resource
Vote question1AgentVote = getAgentVote("/" + SpacesResource.RESOURCE_NAME + "/" + testSpaceId + "/" + QuestionsResource.RESOURCE_NAME + "/" + questionId1 + "/" + QuestionVotesResource.RESOURCE_NAME + "/" + testAgent.getIdentifier());
Assert.assertEquals(3, question1AgentVote.getValue());
Vote question2AgentVote = getAgentVote("/" + SpacesResource.RESOURCE_NAME + "/" + testSpaceId + "/" + QuestionsResource.RESOURCE_NAME + "/" + questionId2 + "/" + QuestionVotesResource.RESOURCE_NAME + "/" + testAgent.getIdentifier());
Assert.assertEquals(3, question2AgentVote.getValue());
Vote relationAgentVote = getAgentVote("/" + SpacesResource.RESOURCE_NAME + "/" + testSpaceId + "/" + QuestionRelationsResource.RESOURCE_NAME + "/" + relationId + "/" + QuestionVotesResource.RESOURCE_NAME + "/" + testAgent.getIdentifier());
Assert.assertEquals(3, relationAgentVote.getValue());
// test get votes for all resources
question1Votes = getVotes("/" + SpacesResource.RESOURCE_NAME + "/" + testSpaceId + "/" + QuestionsResource.RESOURCE_NAME + "/" + questionId1 + "/" + QuestionVotesResource.RESOURCE_NAME);
Assert.assertEquals(1, question1Votes.size());
Assert.assertEquals(1, question1Votes.get(0).getValue());
question2Votes = getVotes("/" + SpacesResource.RESOURCE_NAME + "/" + testSpaceId + "/" + QuestionsResource.RESOURCE_NAME + "/" + questionId2 + "/" + QuestionVotesResource.RESOURCE_NAME);
Assert.assertEquals(1, question2Votes.size());
Assert.assertEquals(1, question2Votes.get(0).getValue());
relationVotes = getVotes("/" + SpacesResource.RESOURCE_NAME + "/" + testSpaceId + "/" + QuestionRelationsResource.RESOURCE_NAME + "/" + relationId + "/" + QuestionVotesResource.RESOURCE_NAME);
Assert.assertEquals(1, relationVotes.size());
Assert.assertEquals(1, relationVotes.get(0).getValue());
// (down-)vote for each with another agent and check all votes
setVote("/" + SpacesResource.RESOURCE_NAME + "/" + testSpaceId + "/" + QuestionsResource.RESOURCE_NAME + "/" + questionId1 + "/" + QuestionVotesResource.RESOURCE_NAME + "/" + testAgent2.getIdentifier(), basicAuthHeader2, -5);
setVote("/" + SpacesResource.RESOURCE_NAME + "/" + testSpaceId + "/" + QuestionsResource.RESOURCE_NAME + "/" + questionId2 + "/" + QuestionVotesResource.RESOURCE_NAME + "/" + testAgent2.getIdentifier(), basicAuthHeader2, -5);
setVote("/" + SpacesResource.RESOURCE_NAME + "/" + testSpaceId + "/" + QuestionRelationsResource.RESOURCE_NAME + "/" + relationId + "/" + QuestionVotesResource.RESOURCE_NAME + "/" + testAgent2.getIdentifier(), basicAuthHeader2, -5);
// test get votes for all resources
question1Votes = getVotes("/" + SpacesResource.RESOURCE_NAME + "/" + testSpaceId + "/" + QuestionsResource.RESOURCE_NAME + "/" + questionId1 + "/" + QuestionVotesResource.RESOURCE_NAME);
Assert.assertEquals(2, question1Votes.size());
Assert.assertEquals(1, question1Votes.get(0).getValue());
Assert.assertEquals(-1, question1Votes.get(1).getValue());
question2Votes = getVotes("/" + SpacesResource.RESOURCE_NAME + "/" + testSpaceId + "/" + QuestionsResource.RESOURCE_NAME + "/" + questionId2 + "/" + QuestionVotesResource.RESOURCE_NAME);
Assert.assertEquals(2, question2Votes.size());
Assert.assertEquals(1, question2Votes.get(0).getValue());
Assert.assertEquals(-1, question2Votes.get(1).getValue());
relationVotes = getVotes("/" + SpacesResource.RESOURCE_NAME + "/" + testSpaceId + "/" + QuestionRelationsResource.RESOURCE_NAME + "/" + relationId + "/" + QuestionVotesResource.RESOURCE_NAME);
Assert.assertEquals(2, relationVotes.size());
Assert.assertEquals(1, relationVotes.get(0).getValue());
Assert.assertEquals(-1, relationVotes.get(1).getValue());
} catch (Exception e) {
e.printStackTrace();
Assert.fail(e.toString());
}
}
use of i5.las2peer.services.noracleService.model.Vote in project Distributed-Noracle-Backend by Distributed-Noracle.
the class QuestionVotesResource method getAllVotes.
@Override
public VoteList getAllVotes(String objectId) throws ServiceInvocationException {
Serializable rmiResult = Context.get().invoke(new ServiceNameVersion(NoracleVoteService.class.getCanonicalName(), NoracleService.API_VERSION), "getAllVotes", objectId);
VoteList vote;
if (rmiResult instanceof VoteList) {
vote = (VoteList) rmiResult;
} else {
throw new InternalServiceException("Unexpected result (" + rmiResult.getClass().getCanonicalName() + ") of RMI call");
}
return vote;
}
use of i5.las2peer.services.noracleService.model.Vote in project Distributed-Noracle-Backend by Distributed-Noracle.
the class QuestionVotesResource method getAgentVote.
@Override
public Vote getAgentVote(String objectId, String agentId) throws ServiceInvocationException {
Serializable rmiResult = Context.get().invoke(new ServiceNameVersion(NoracleVoteService.class.getCanonicalName(), NoracleService.API_VERSION), "getAgentVote", objectId, agentId);
Vote vote;
if (rmiResult instanceof Vote) {
vote = (Vote) rmiResult;
} else {
throw new InternalServiceException("Unexpected result (" + rmiResult.getClass().getCanonicalName() + ") of RMI call");
}
return vote;
}
use of i5.las2peer.services.noracleService.model.Vote in project Distributed-Noracle-Backend by Distributed-Noracle.
the class QuestionVotesResource method putSetQuestionVote.
@PUT
@Path("/{agentId}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@ApiResponses({ @ApiResponse(code = HttpURLConnection.HTTP_OK, message = "Vote successfully set", response = Vote.class), @ApiResponse(code = HttpURLConnection.HTTP_UNAUTHORIZED, message = "You have to be logged in to vote", response = ExceptionEntity.class), @ApiResponse(code = HttpURLConnection.HTTP_INTERNAL_ERROR, message = "Internal Server Error", response = ExceptionEntity.class) })
public Vote putSetQuestionVote(@PathParam("spaceId") String spaceId, @PathParam("questionId") String questionId, @PathParam("agentId") String agentId, @ApiParam(required = true) SetVotePojo setVotePojo) throws ServiceInvocationException {
String objectId = buildObjectId(spaceId, questionId);
Gson gson = new Gson();
String voteJSON = gson.toJson(setVotePojo);
JSONParser p = new JSONParser(JSONParser.MODE_PERMISSIVE);
try {
JSONObject obj = (JSONObject) p.parse(voteJSON);
JSONObject attributes = new JSONObject();
obj.put("spaceId", spaceId);
obj.put("qId", questionId);
obj.put("functionName", "putSetQuestionVote");
obj.put("serviceAlias", "distributed-noracle");
obj.put("uid", Context.getCurrent().getMainAgent().getIdentifier());
attributes.put("spaceId", spaceId);
attributes.put("qId", questionId);
attributes.put("body", p.parse(voteJSON));
attributes.put("result", "");
obj.put("attributes", attributes);
Context.get().monitorEvent(MonitoringEvent.SERVICE_CUSTOM_MESSAGE_7, obj.toJSONString());
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Try to log training data
try {
String from = getQuestionText(questionId);
Integer to = setVotePojo.getValue();
JSONObject trainingData = new JSONObject();
trainingData.put("unit", spaceId);
trainingData.put("from", from);
trainingData.put("to", to);
Context.get().monitorEvent(MonitoringEvent.SERVICE_CUSTOM_MESSAGE_47, trainingData.toString());
} catch (ServiceInvocationException e) {
Context.get().monitorEvent(MonitoringEvent.SERVICE_CUSTOM_ERROR_47, questionId);
}
return setVote(agentId, objectId, setVotePojo.getValue());
}
use of i5.las2peer.services.noracleService.model.Vote in project Distributed-Noracle-Backend by Distributed-Noracle.
the class RelationVotesResource method getAgentVote.
@Override
public Vote getAgentVote(String objectId, String agentId) throws ServiceInvocationException {
Serializable rmiResult = Context.get().invoke(new ServiceNameVersion(NoracleVoteService.class.getCanonicalName(), NoracleService.API_VERSION), "getAgentVote", objectId, agentId);
Vote vote;
if (rmiResult instanceof Vote) {
vote = (Vote) rmiResult;
} else {
throw new InternalServiceException("Unexpected result (" + rmiResult.getClass().getCanonicalName() + ") of RMI call");
}
return vote;
}
Aggregations