Search in sources :

Example 1 with Vote

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());
    }
}
Also used : Vote(i5.las2peer.services.noracleService.model.Vote) VoteList(i5.las2peer.services.noracleService.model.VoteList) Test(org.junit.Test)

Example 2 with Vote

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;
}
Also used : Serializable(java.io.Serializable) ServiceNameVersion(i5.las2peer.api.p2p.ServiceNameVersion) VoteList(i5.las2peer.services.noracleService.model.VoteList) InternalServiceException(i5.las2peer.api.execution.InternalServiceException)

Example 3 with 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;
}
Also used : Serializable(java.io.Serializable) Vote(i5.las2peer.services.noracleService.model.Vote) ServiceNameVersion(i5.las2peer.api.p2p.ServiceNameVersion) InternalServiceException(i5.las2peer.api.execution.InternalServiceException)

Example 4 with 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());
}
Also used : JSONObject(net.minidev.json.JSONObject) Gson(com.google.gson.Gson) JSONParser(net.minidev.json.parser.JSONParser) ParseException(net.minidev.json.parser.ParseException) ServiceInvocationException(i5.las2peer.api.execution.ServiceInvocationException) ApiResponses(io.swagger.annotations.ApiResponses)

Example 5 with Vote

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;
}
Also used : Serializable(java.io.Serializable) Vote(i5.las2peer.services.noracleService.model.Vote) ServiceNameVersion(i5.las2peer.api.p2p.ServiceNameVersion) InternalServiceException(i5.las2peer.api.execution.InternalServiceException)

Aggregations

InternalServiceException (i5.las2peer.api.execution.InternalServiceException)7 Vote (i5.las2peer.services.noracleService.model.Vote)7 ServiceNameVersion (i5.las2peer.api.p2p.ServiceNameVersion)4 Envelope (i5.las2peer.api.persistency.Envelope)4 EnvelopeNotFoundException (i5.las2peer.api.persistency.EnvelopeNotFoundException)4 VoteList (i5.las2peer.services.noracleService.model.VoteList)4 Serializable (java.io.Serializable)4 ServiceInvocationException (i5.las2peer.api.execution.ServiceInvocationException)3 EnvelopeAccessDeniedException (i5.las2peer.api.persistency.EnvelopeAccessDeniedException)3 EnvelopeOperationFailedException (i5.las2peer.api.persistency.EnvelopeOperationFailedException)3 InvocationBadArgumentException (i5.las2peer.api.execution.InvocationBadArgumentException)2 ServiceAccessDeniedException (i5.las2peer.api.execution.ServiceAccessDeniedException)2 VoteEntry (i5.las2peer.services.noracleService.model.VoteEntry)2 Gson (com.google.gson.Gson)1 Agent (i5.las2peer.api.security.Agent)1 AnonymousAgent (i5.las2peer.api.security.AnonymousAgent)1 ApiResponses (io.swagger.annotations.ApiResponses)1 JSONObject (net.minidev.json.JSONObject)1 JSONParser (net.minidev.json.parser.JSONParser)1 ParseException (net.minidev.json.parser.ParseException)1