Search in sources :

Example 1 with Version

use of models.Version in project civiform by seattle-uat.

the class VersionRepositoryTest method updatePredicateNode.

@Test
public void updatePredicateNode() {
    Version draft = versionRepository.getDraftVersion();
    Version active = versionRepository.getActiveVersion();
    // Old versions of questions
    Question oldOne = resourceCreator.insertQuestion("one");
    oldOne.addVersion(active);
    oldOne.save();
    Question oldTwo = resourceCreator.insertQuestion("two");
    oldTwo.addVersion(active);
    oldTwo.save();
    // New versions of questions
    Question newOne = resourceCreator.insertQuestion("one");
    newOne.addVersion(draft);
    newOne.save();
    Question newTwo = resourceCreator.insertQuestion("two");
    newTwo.addVersion(draft);
    newTwo.save();
    // Build a predicate tree that covers all node types:
    // AND
    // /     \
    // LEAF1    OR
    // /    \
    // LEAF2   LEAF3
    PredicateExpressionNode leafOne = PredicateExpressionNode.create(LeafOperationExpressionNode.create(oldOne.id, Scalar.TEXT, Operator.EQUAL_TO, PredicateValue.of("")));
    PredicateExpressionNode leafTwo = PredicateExpressionNode.create(LeafOperationExpressionNode.create(oldTwo.id, Scalar.TEXT, Operator.EQUAL_TO, PredicateValue.of("")));
    PredicateExpressionNode leafThree = PredicateExpressionNode.create(LeafOperationExpressionNode.create(oldOne.id, Scalar.NUMBER, Operator.LESS_THAN, PredicateValue.of(12)));
    PredicateExpressionNode or = PredicateExpressionNode.create(OrNode.create(ImmutableSet.of(leafTwo, leafThree)));
    PredicateExpressionNode and = PredicateExpressionNode.create(AndNode.create(ImmutableSet.of(leafOne, or)));
    PredicateExpressionNode updated = versionRepository.updatePredicateNode(and);
    // The tree should have the same structure, just with question IDs for the draft version.
    PredicateExpressionNode expectedLeafOne = PredicateExpressionNode.create(leafOne.getLeafNode().toBuilder().setQuestionId(newOne.id).build());
    PredicateExpressionNode expectedLeafTwo = PredicateExpressionNode.create(leafTwo.getLeafNode().toBuilder().setQuestionId(newTwo.id).build());
    PredicateExpressionNode expectedLeafThree = PredicateExpressionNode.create(leafThree.getLeafNode().toBuilder().setQuestionId(newOne.id).build());
    PredicateExpressionNode expectedOr = PredicateExpressionNode.create(OrNode.create(ImmutableSet.of(expectedLeafTwo, expectedLeafThree)));
    PredicateExpressionNode expectedAnd = PredicateExpressionNode.create(AndNode.create(ImmutableSet.of(expectedLeafOne, expectedOr)));
    assertThat(updated.getType()).isEqualTo(PredicateExpressionNodeType.AND);
    assertThat(updated).isEqualTo(expectedAnd);
}
Also used : Version(models.Version) Question(models.Question) PredicateExpressionNode(services.program.predicate.PredicateExpressionNode) Test(org.junit.Test)

Example 2 with Version

use of models.Version in project civiform by seattle-uat.

the class VersionRepositoryTest method testPublish.

@Test
public void testPublish() {
    resourceCreator.insertActiveProgram("foo");
    resourceCreator.insertActiveProgram("bar");
    resourceCreator.insertDraftProgram("bar");
    assertThat(this.versionRepository.getActiveVersion().getPrograms()).hasSize(2);
    assertThat(this.versionRepository.getDraftVersion().getPrograms()).hasSize(1);
    Version oldDraft = this.versionRepository.getDraftVersion();
    this.versionRepository.publishNewSynchronizedVersion();
    assertThat(this.versionRepository.getActiveVersion().getPrograms()).hasSize(2);
    assertThat(this.versionRepository.getDraftVersion().getPrograms()).hasSize(0);
    oldDraft.refresh();
    assertThat(oldDraft.getLifecycleStage()).isEqualTo(LifecycleStage.ACTIVE);
}
Also used : Version(models.Version) Test(org.junit.Test)

Example 3 with Version

use of models.Version in project civiform by seattle-uat.

the class ReadOnlyCurrentQuestionServiceImplTest method getEnumeratorQuestions.

@Test
public void getEnumeratorQuestions() {
    QuestionDefinition enumeratorQuestion = testQuestionBank.applicantHouseholdMembers().getQuestionDefinition();
    service = new ReadOnlyCurrentQuestionServiceImpl(new Version(LifecycleStage.ACTIVE) {

        @Override
        public ImmutableList<Question> getQuestions() {
            return ImmutableList.<QuestionDefinition>builder().addAll(questions).add(enumeratorQuestion).build().stream().map(q -> new Question(q)).collect(ImmutableList.toImmutableList());
        }
    }, new Version(LifecycleStage.DRAFT));
    assertThat(service.getAllEnumeratorQuestions().size()).isEqualTo(1);
    assertThat(service.getAllEnumeratorQuestions().get(0)).isEqualTo(enumeratorQuestion);
    assertThat(service.getUpToDateEnumeratorQuestions().size()).isEqualTo(1);
    assertThat(service.getUpToDateEnumeratorQuestions().get(0)).isEqualTo(enumeratorQuestion);
}
Also used : Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) QuestionDefinition(services.question.types.QuestionDefinition) Test(org.junit.Test) LifecycleStage(models.LifecycleStage) AddressQuestionDefinition(services.question.types.AddressQuestionDefinition) Version(models.Version) QuestionNotFoundException(services.question.exceptions.QuestionNotFoundException) TextQuestionDefinition(services.question.types.TextQuestionDefinition) ImmutableList(com.google.common.collect.ImmutableList) TestQuestionBank(support.TestQuestionBank) Question(models.Question) NameQuestionDefinition(services.question.types.NameQuestionDefinition) Before(org.junit.Before) QuestionDefinition(services.question.types.QuestionDefinition) AddressQuestionDefinition(services.question.types.AddressQuestionDefinition) TextQuestionDefinition(services.question.types.TextQuestionDefinition) NameQuestionDefinition(services.question.types.NameQuestionDefinition) Version(models.Version) Question(models.Question) Test(org.junit.Test)

Example 4 with Version

use of models.Version in project civiform by seattle-uat.

the class ReadOnlyVersionedQuestionServiceImplTest method getEnumeratorQuestions.

@Test
public void getEnumeratorQuestions() {
    QuestionDefinition enumeratorQuestion = testQuestionBank.applicantHouseholdMembers().getQuestionDefinition();
    service = new ReadOnlyVersionedQuestionServiceImpl(new Version(LifecycleStage.OBSOLETE) {

        @Override
        public ImmutableList<Question> getQuestions() {
            return ImmutableList.<QuestionDefinition>builder().addAll(questions).add(enumeratorQuestion).build().stream().map(q -> new Question(q)).collect(ImmutableList.toImmutableList());
        }
    });
    assertThat(service.getAllEnumeratorQuestions().size()).isEqualTo(1);
    assertThat(service.getAllEnumeratorQuestions().get(0)).isEqualTo(enumeratorQuestion);
}
Also used : Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) QuestionDefinition(services.question.types.QuestionDefinition) Test(org.junit.Test) LifecycleStage(models.LifecycleStage) AddressQuestionDefinition(services.question.types.AddressQuestionDefinition) Version(models.Version) QuestionNotFoundException(services.question.exceptions.QuestionNotFoundException) TextQuestionDefinition(services.question.types.TextQuestionDefinition) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) ImmutableList(com.google.common.collect.ImmutableList) TestQuestionBank(support.TestQuestionBank) Question(models.Question) NameQuestionDefinition(services.question.types.NameQuestionDefinition) Before(org.junit.Before) QuestionDefinition(services.question.types.QuestionDefinition) AddressQuestionDefinition(services.question.types.AddressQuestionDefinition) TextQuestionDefinition(services.question.types.TextQuestionDefinition) NameQuestionDefinition(services.question.types.NameQuestionDefinition) Version(models.Version) Question(models.Question) Test(org.junit.Test)

Example 5 with Version

use of models.Version in project civiform by seattle-uat.

the class ProgramServiceImpl method syncProgramAssociations.

private CompletionStage<ProgramDefinition> syncProgramAssociations(Program program) {
    if (isActiveOrDraftProgram(program)) {
        return syncProgramDefinitionQuestions(program.getProgramDefinition()).thenApply(programDefinition -> programDefinition.orderBlockDefinitions());
    }
    // Any version that the program is in has all the questions the program has.
    Version version = program.getVersions().stream().findAny().get();
    ProgramDefinition programDefinition = syncProgramDefinitionQuestions(program.getProgramDefinition(), version);
    return CompletableFuture.completedStage(programDefinition.orderBlockDefinitions());
}
Also used : Version(models.Version)

Aggregations

Version (models.Version)23 Question (models.Question)10 Test (org.junit.Test)7 ImmutableList (com.google.common.collect.ImmutableList)6 LifecycleStage (models.LifecycleStage)6 Transaction (io.ebean.Transaction)5 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)4 Database (io.ebean.Database)4 List (java.util.List)4 Optional (java.util.Optional)4 Before (org.junit.Before)4 Program (models.Program)3 InvalidUpdateException (services.question.exceptions.InvalidUpdateException)3 Preconditions (com.google.common.base.Preconditions)2 Inject (com.google.inject.Inject)2 controllers.admin.routes (controllers.admin.routes)2 DB (io.ebean.DB)2 SerializableConflictException (io.ebean.SerializableConflictException)2 TxScope (io.ebean.TxScope)2 TagCreator.div (j2html.TagCreator.div)2