use of i5.las2peer.services.noracleService.model.Question 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.Question in project Distributed-Noracle-Backend by Distributed-Noracle.
the class QuestionVotesResource method getQuestionText.
private String getQuestionText(String questionId) throws ServiceInvocationException {
String qText = "";
Serializable rmiResult = Context.get().invoke(new ServiceNameVersion(NoracleQuestionService.class.getCanonicalName(), NoracleService.API_VERSION), "getQuestion", questionId);
if (rmiResult instanceof Question)
qText = ((Question) rmiResult).getText();
return qText;
}
use of i5.las2peer.services.noracleService.model.Question 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;
}
use of i5.las2peer.services.noracleService.model.Question in project Distributed-Noracle-Backend by Distributed-Noracle.
the class NoracleQuestionService method changeQuestionDepth.
@Override
public Question changeQuestionDepth(String questionId, int depth) throws ServiceInvocationException {
if (questionId == null) {
throw new InvocationBadArgumentException("No question id given");
}
try {
Envelope questionEnvelope = Context.get().requestEnvelope(getQuestionEnvelopeIdentifier(questionId));
Question question = (Question) questionEnvelope.getContent();
question.setDepth(depth);
question.setTimestampLastModified(Instant.now().toString());
questionEnvelope.setContent(question);
Context.get().storeEnvelope(questionEnvelope);
return question;
} catch (EnvelopeNotFoundException e) {
throw new ResourceNotFoundException("Question not found");
} catch (EnvelopeAccessDeniedException e) {
throw new ServiceAccessDeniedException("Envelope Access Denied");
} catch (EnvelopeOperationFailedException e) {
throw new InternalServiceException("Could not fetch question envelope", e);
}
}
use of i5.las2peer.services.noracleService.model.Question in project Distributed-Noracle-Backend by Distributed-Noracle.
the class NoracleQuestionService method changeQuestionText.
@Override
public Question changeQuestionText(String questionId, String text) throws ServiceInvocationException {
if (questionId == null) {
throw new InvocationBadArgumentException("No question id given");
}
try {
Envelope questionEnvelope = Context.get().requestEnvelope(getQuestionEnvelopeIdentifier(questionId));
Question question = (Question) questionEnvelope.getContent();
question.setText(text);
question.setTimestampLastModified(Instant.now().toString());
questionEnvelope.setContent(question);
Context.get().storeEnvelope(questionEnvelope);
return question;
} catch (EnvelopeNotFoundException e) {
throw new ResourceNotFoundException("Question not found");
} catch (EnvelopeAccessDeniedException e) {
throw new ServiceAccessDeniedException("Envelope Access Denied");
} catch (EnvelopeOperationFailedException e) {
throw new InternalServiceException("Could not fetch question envelope", e);
}
}
Aggregations