Search in sources :

Example 1 with Space

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

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

the class NoracleAgentService method unsubscribeFromSpace.

@Override
public void unsubscribeFromSpace(String spaceId) throws ServiceInvocationException {
    Agent mainAgent = Context.get().getMainAgent();
    if (spaceId == null || spaceId.isEmpty()) {
        throw new InvocationBadArgumentException("No space id given");
    } else if (mainAgent instanceof AnonymousAgent) {
        throw new ServiceAccessDeniedException("You have to be logged in to unsubscribe to a space");
    }
    String envIdentifier = buildSubscriptionId(mainAgent.getIdentifier());
    Envelope env;
    SpaceSubscriptionList subscriptionList;
    try {
        try {
            env = Context.get().requestEnvelope(envIdentifier);
            subscriptionList = (SpaceSubscriptionList) env.getContent();
        } catch (EnvelopeNotFoundException e) {
            return;
        }
    } catch (EnvelopeAccessDeniedException e) {
        throw new ServiceAccessDeniedException("Envelope Access Denied");
    } catch (EnvelopeOperationFailedException e) {
        throw new InternalServiceException("Could not read envelope for space unsubscription", e);
    }
    Iterator<SpaceSubscription> itSubscription = subscriptionList.iterator();
    while (itSubscription.hasNext()) {
        SpaceSubscription subscription = itSubscription.next();
        if (subscription.getSpaceId().equals(spaceId)) {
            itSubscription.remove();
        }
    }
    env.setContent(subscriptionList);
    try {
        Context.get().storeEnvelope(env, mainAgent);
    } catch (EnvelopeAccessDeniedException e) {
        throw new ServiceAccessDeniedException("Envelope Access Denied");
    } catch (EnvelopeOperationFailedException e) {
        throw new InternalServiceException("Could not store space subscription envelope", e);
    }
}
Also used : AnonymousAgent(i5.las2peer.api.security.AnonymousAgent) Agent(i5.las2peer.api.security.Agent) EnvelopeOperationFailedException(i5.las2peer.api.persistency.EnvelopeOperationFailedException) AnonymousAgent(i5.las2peer.api.security.AnonymousAgent) SpaceSubscriptionList(i5.las2peer.services.noracleService.model.SpaceSubscriptionList) EnvelopeAccessDeniedException(i5.las2peer.api.persistency.EnvelopeAccessDeniedException) Envelope(i5.las2peer.api.persistency.Envelope) EnvelopeNotFoundException(i5.las2peer.api.persistency.EnvelopeNotFoundException) SpaceSubscription(i5.las2peer.services.noracleService.model.SpaceSubscription)

Example 3 with Space

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

the class NoracleQuestionService method createQuestion.

@Override
public Question createQuestion(String questionSpaceId, String text) throws ServiceInvocationException {
    Agent mainAgent = Context.get().getMainAgent();
    if (questionSpaceId == null || questionSpaceId.isEmpty()) {
        throw new InvocationBadArgumentException("No question space id given");
    } else if (text == null || text.isEmpty()) {
        throw new InvocationBadArgumentException("No question text given");
    } else if (mainAgent instanceof AnonymousAgent) {
        throw new ServiceNotAuthorizedException("You have to be logged in to create a question");
    }
    Space targetSpace;
    Serializable rmiResult = Context.get().invoke(new ServiceNameVersion(NoracleSpaceService.class.getCanonicalName(), NoracleService.API_VERSION), "getSpace", questionSpaceId);
    if (rmiResult instanceof Space) {
        targetSpace = (Space) rmiResult;
    } else {
        throw new InternalServiceException("Unexpected result (" + rmiResult.getClass().getCanonicalName() + ") of RMI call");
    }
    String targetReaderGroupId = targetSpace.getSpaceReaderGroupId();
    GroupAgent targetReaderGroup;
    try {
        targetReaderGroup = (GroupAgent) Context.get().requestAgent(targetReaderGroupId, mainAgent);
    } catch (AgentNotFoundException | AgentOperationFailedException e) {
        throw new InternalServiceException("Could not fetch reader group agent for space", e);
    } catch (ClassCastException e) {
        throw new InternalServiceException("Agent for space reader group is not a GroupAgent", e);
    } catch (AgentAccessDeniedException e) {
        throw new ServiceAccessDeniedException("Agent not in space reader group", e);
    }
    String questionId = buildQuestionId();
    Envelope env;
    try {
        env = Context.get().createEnvelope(getQuestionEnvelopeIdentifier(questionId), mainAgent);
    } catch (EnvelopeAccessDeniedException e) {
        throw new ServiceAccessDeniedException("Envelope Access Denied");
    } catch (EnvelopeOperationFailedException e) {
        throw new InternalServiceException("Could not create envelope for question", e);
    }
    env.addReader(targetReaderGroup);
    Question question = new Question(questionId, text, questionSpaceId, mainAgent.getIdentifier(), Instant.now().toString());
    env.setContent(question);
    try {
        Context.get().storeEnvelope(env, mainAgent);
    } catch (EnvelopeAccessDeniedException e) {
        throw new ServiceAccessDeniedException("Envelope Access Denied");
    } catch (EnvelopeOperationFailedException e) {
        throw new InternalServiceException("Could not store question envelope", e);
    }
    if (questionSpaceId != null && !questionSpaceId.isEmpty()) {
        linkQuestionToSpace(questionSpaceId, questionId);
    }
    return question;
}
Also used : Space(i5.las2peer.services.noracleService.model.Space) Serializable(java.io.Serializable) EnvelopeOperationFailedException(i5.las2peer.api.persistency.EnvelopeOperationFailedException) EnvelopeAccessDeniedException(i5.las2peer.api.persistency.EnvelopeAccessDeniedException) Envelope(i5.las2peer.api.persistency.Envelope) ServiceNameVersion(i5.las2peer.api.p2p.ServiceNameVersion) Question(i5.las2peer.services.noracleService.model.Question)

Example 4 with Space

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

the class NoracleSpaceService method getSubscribers.

@Override
public SpaceSubscribersList getSubscribers(String spaceId) throws ServiceInvocationException {
    if (spaceId == null || spaceId.isEmpty()) {
        throw new InvocationBadArgumentException("No space id given");
    }
    try {
        UserAgentImpl inviteAgent = this.getInviteAgent(spaceId);
        GroupAgent memberAgent = this.getMemberAgent(spaceId);
        memberAgent.unlock(Context.get().getMainAgent());
        String[] memberIds = memberAgent.getMemberList();
        SpaceSubscribersList subscribers = new SpaceSubscribersList();
        for (int i = 0; i < memberIds.length; i++) {
            if (!memberIds[i].equals(inviteAgent.getIdentifier())) {
                UserAgentImpl agent = (UserAgentImpl) Context.get().fetchAgent(memberIds[i]);
                NoracleAgentProfile profile = new NoracleAgentProfile();
                profile.setName(agent.getLoginName());
                subscribers.add(profile);
            }
        }
        return subscribers;
    } catch (Exception e) {
        throw new ServiceInvocationException("Could not fetch list", e);
    }
}
Also used : NoracleAgentProfile(i5.las2peer.services.noracleService.model.NoracleAgentProfile) UserAgentImpl(i5.las2peer.security.UserAgentImpl) SpaceSubscribersList(i5.las2peer.services.noracleService.model.SpaceSubscribersList) EnvelopeAccessDeniedException(i5.las2peer.api.persistency.EnvelopeAccessDeniedException) CryptoException(i5.las2peer.tools.CryptoException) EnvelopeOperationFailedException(i5.las2peer.api.persistency.EnvelopeOperationFailedException) SerializationException(i5.las2peer.serialization.SerializationException) EnvelopeNotFoundException(i5.las2peer.api.persistency.EnvelopeNotFoundException)

Example 5 with Space

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

the class NoracleQuestionRelationService method getQuestionRelations.

@Override
public QuestionRelationList getQuestionRelations(String spaceId, String order, Integer limit, Integer startAt) throws ServiceInvocationException {
    if (spaceId == null || spaceId.isEmpty()) {
        throw new InvocationBadArgumentException("No space id given");
    } else if (limit != null && limit <= 0) {
        throw new InvocationBadArgumentException("Invalid limit given");
    } else if (startAt != null && startAt < 1) {
        throw new InvocationBadArgumentException("Invalid startAt given");
    }
    if (order == null || order.isEmpty()) {
        order = "asc";
    }
    if (limit == null) {
        limit = 10;
    }
    if (startAt == null) {
        startAt = 1;
    }
    int direction = order.equalsIgnoreCase("desc") ? -1 : 1;
    QuestionRelationList result = new QuestionRelationList();
    for (int relationNumber = startAt; relationNumber < startAt + direction * limit; relationNumber += direction) {
        try {
            Envelope spaceQuestionRelationEnv;
            try {
                spaceQuestionRelationEnv = Context.get().requestEnvelope(buildSpaceQuestionRelationNumberId(spaceId, relationNumber));
            } catch (EnvelopeNotFoundException e) {
                break;
            }
            String questionId = (String) spaceQuestionRelationEnv.getContent();
            Envelope questionEnv = Context.get().requestEnvelope(getQuestionRelationEnvelopeIdentifier(questionId));
            result.add((QuestionRelation) questionEnv.getContent());
        } catch (Exception e) {
        // XXX logging
        }
    }
    return result;
}
Also used : EnvelopeNotFoundException(i5.las2peer.api.persistency.EnvelopeNotFoundException) QuestionRelationList(i5.las2peer.services.noracleService.model.QuestionRelationList) Envelope(i5.las2peer.api.persistency.Envelope) EnvelopeAccessDeniedException(i5.las2peer.api.persistency.EnvelopeAccessDeniedException) EnvelopeOperationFailedException(i5.las2peer.api.persistency.EnvelopeOperationFailedException) EnvelopeNotFoundException(i5.las2peer.api.persistency.EnvelopeNotFoundException)

Aggregations

EnvelopeAccessDeniedException (i5.las2peer.api.persistency.EnvelopeAccessDeniedException)10 EnvelopeOperationFailedException (i5.las2peer.api.persistency.EnvelopeOperationFailedException)10 Envelope (i5.las2peer.api.persistency.Envelope)8 EnvelopeNotFoundException (i5.las2peer.api.persistency.EnvelopeNotFoundException)8 ServiceNameVersion (i5.las2peer.api.p2p.ServiceNameVersion)6 Space (i5.las2peer.services.noracleService.model.Space)5 SpaceSubscription (i5.las2peer.services.noracleService.model.SpaceSubscription)4 Serializable (java.io.Serializable)4 Agent (i5.las2peer.api.security.Agent)3 AnonymousAgent (i5.las2peer.api.security.AnonymousAgent)3 SerializationException (i5.las2peer.serialization.SerializationException)3 SpaceSubscriptionList (i5.las2peer.services.noracleService.model.SpaceSubscriptionList)3 CryptoException (i5.las2peer.tools.CryptoException)3 ApiResponses (io.swagger.annotations.ApiResponses)3 UserAgentImpl (i5.las2peer.security.UserAgentImpl)2 Question (i5.las2peer.services.noracleService.model.Question)2 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)2 Gson (com.google.gson.Gson)1 GroupAgentImpl (i5.las2peer.security.GroupAgentImpl)1 NoracleAgentService (i5.las2peer.services.noracleService.NoracleAgentService)1