Search in sources :

Example 1 with VoteList

use of i5.las2peer.services.noracleService.model.VoteList 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 VoteList

use of i5.las2peer.services.noracleService.model.VoteList 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 VoteList

use of i5.las2peer.services.noracleService.model.VoteList in project Distributed-Noracle-Backend by Distributed-Noracle.

the class NoracleVoteService method getAllVotes.

@Override
public VoteList getAllVotes(String objectId) throws ServiceInvocationException {
    VoteList result = new VoteList();
    for (int num = 1; num < MAX_VOTES_PER_OBJECT; num++) {
        try {
            String pubEnvId = getPublicVoteEnvelopeIdentifier(objectId, num);
            Envelope pubEnv = Context.get().requestEnvelope(pubEnvId);
            Vote vote = (Vote) pubEnv.getContent();
            int normalizedVal = vote.getValue();
            if (normalizedVal > 1) {
                normalizedVal = 1;
            } else if (normalizedVal < -1) {
                normalizedVal = -1;
            }
            Vote normalizedVote = new Vote(normalizedVal, vote.getVoterAgentId());
            result.add(normalizedVote);
        } catch (EnvelopeNotFoundException e) {
            logger.warning("EnvelopeNotFoundException inside NoracleVoteService.getAllVotes(...): " + e.getMessage());
            break;
        } catch (Exception e) {
            logger.warning("Exception inside NoracleVoteService.getAllVotes(...): " + e.getMessage());
        }
    }
    return result;
}
Also used : Vote(i5.las2peer.services.noracleService.model.Vote) EnvelopeNotFoundException(i5.las2peer.api.persistency.EnvelopeNotFoundException) VoteList(i5.las2peer.services.noracleService.model.VoteList) Envelope(i5.las2peer.api.persistency.Envelope) EnvelopeAccessDeniedException(i5.las2peer.api.persistency.EnvelopeAccessDeniedException) EnvelopeOperationFailedException(i5.las2peer.api.persistency.EnvelopeOperationFailedException) InvocationBadArgumentException(i5.las2peer.api.execution.InvocationBadArgumentException) ServiceAccessDeniedException(i5.las2peer.api.execution.ServiceAccessDeniedException) ServiceInvocationException(i5.las2peer.api.execution.ServiceInvocationException) InternalServiceException(i5.las2peer.api.execution.InternalServiceException) EnvelopeNotFoundException(i5.las2peer.api.persistency.EnvelopeNotFoundException)

Example 4 with VoteList

use of i5.las2peer.services.noracleService.model.VoteList in project Distributed-Noracle-Backend by Distributed-Noracle.

the class RelationVotesResource 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 5 with VoteList

use of i5.las2peer.services.noracleService.model.VoteList in project Distributed-Noracle-Backend by Distributed-Noracle.

the class QuestionRelationsResource method getQuestions.

@GET
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@ApiResponses({ @ApiResponse(code = HttpURLConnection.HTTP_OK, message = "A list of relations from the network", response = QuestionRelationList.class), @ApiResponse(code = HttpURLConnection.HTTP_BAD_REQUEST, message = "No space id given", response = ExceptionEntity.class), @ApiResponse(code = HttpURLConnection.HTTP_FORBIDDEN, message = "Access Denied", response = ExceptionEntity.class), @ApiResponse(code = HttpURLConnection.HTTP_INTERNAL_ERROR, message = "Internal Server Error", response = ExceptionEntity.class) })
public Response getQuestions(@PathParam("spaceId") String spaceId, @QueryParam("order") String order, @QueryParam("limit") Integer limit, @QueryParam("startAt") Integer startAt) throws ServiceInvocationException {
    QuestionRelationList questionRelationList = getQuestionRelations(spaceId, order, limit, startAt);
    VotedQuestionRelationList votedQuestionRelationList = new VotedQuestionRelationList();
    for (QuestionRelation questionRelation : questionRelationList) {
        VotedQuestionRelation votedQuestionRelation = new VotedQuestionRelation(questionRelation);
        String objectId = RelationVotesResource.buildObjectId(spaceId, questionRelation.getRelationId());
        Serializable rmiResult = Context.get().invoke(new ServiceNameVersion(NoracleVoteService.class.getCanonicalName(), NoracleService.API_VERSION), "getAllVotes", objectId);
        if (rmiResult instanceof VoteList) {
            votedQuestionRelation.setVotes((VoteList) rmiResult);
        }
        votedQuestionRelationList.add(votedQuestionRelation);
    }
    ResponseBuilder responseBuilder = Response.ok(votedQuestionRelationList);
    String queryOrder = order != null ? "order=" + order : "";
    String queryLimit = limit != null ? "limit=" + Integer.toString(limit) : "";
    String queryStartAt = "";
    if (startAt != null && limit != null) {
        if (order.equalsIgnoreCase("desc")) {
            queryStartAt = "startat=" + Integer.toString(startAt - limit);
        } else {
            queryStartAt = "startat=" + Integer.toString(startAt + limit);
        }
    }
    String nextLinkStr = "";
    for (String param : new String[] { queryOrder, queryLimit, queryStartAt }) {
        if (param != null && !param.isEmpty()) {
            if (nextLinkStr.isEmpty()) {
                nextLinkStr += "?";
            } else {
                nextLinkStr += "&";
            }
            nextLinkStr += param;
        }
    }
    if (!nextLinkStr.isEmpty()) {
        responseBuilder.header(HttpHeaders.LINK, "<" + nextLinkStr + ">; rel=\"next\"");
    }
    return responseBuilder.build();
}
Also used : Serializable(java.io.Serializable) ServiceNameVersion(i5.las2peer.api.p2p.ServiceNameVersion) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) ApiResponses(io.swagger.annotations.ApiResponses)

Aggregations

ServiceNameVersion (i5.las2peer.api.p2p.ServiceNameVersion)4 VoteList (i5.las2peer.services.noracleService.model.VoteList)4 Serializable (java.io.Serializable)4 InternalServiceException (i5.las2peer.api.execution.InternalServiceException)3 Vote (i5.las2peer.services.noracleService.model.Vote)2 ApiResponses (io.swagger.annotations.ApiResponses)2 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)2 InvocationBadArgumentException (i5.las2peer.api.execution.InvocationBadArgumentException)1 ServiceAccessDeniedException (i5.las2peer.api.execution.ServiceAccessDeniedException)1 ServiceInvocationException (i5.las2peer.api.execution.ServiceInvocationException)1 Envelope (i5.las2peer.api.persistency.Envelope)1 EnvelopeAccessDeniedException (i5.las2peer.api.persistency.EnvelopeAccessDeniedException)1 EnvelopeNotFoundException (i5.las2peer.api.persistency.EnvelopeNotFoundException)1 EnvelopeOperationFailedException (i5.las2peer.api.persistency.EnvelopeOperationFailedException)1 Test (org.junit.Test)1