001/*-------------------------------------------------------------------------+
002|                                                                          |
003| Copyright (c) 2009-2019 CQSE GmbH                                        |
004|                                                                          |
005+-------------------------------------------------------------------------*/
006package org.conqat.engine.index.shared;
007
008import static java.util.Objects.requireNonNull;
009
010import java.util.concurrent.atomic.AtomicInteger;
011
012import org.conqat.lib.commons.assessment.ETrafficLightColor;
013
014/**
015 * Builds {@link TrackedFinding}s for test purposes.
016 */
017public class TestTrackedFindingBuilder {
018
019        /** The number of instances built so far. */
020        private static AtomicInteger instanceCounter = new AtomicInteger();
021
022        private TestIndexFindingBuilder indexFinding = TestIndexFindingBuilder.someIndexFinding();
023        private String id;
024        private CommitDescriptor birthCommit;
025        private CommitDescriptor deathCommit;
026
027        /**
028         * @return a builder creating some fresh {@link TrackedFinding} whose fields are
029         *         all initialized with valid values.
030         */
031        public static TestTrackedFindingBuilder someTrackedFinding() {
032                int instance = instanceCounter.incrementAndGet();
033                long birth = 1000000000L + instance;
034                TestTrackedFindingBuilder someTrackedFinding = new TestTrackedFindingBuilder();
035                return someTrackedFinding.withId(Integer.toHexString(instance))
036                                .withBirthCommit(new CommitDescriptor("branch" + instance, birth)) //
037                                .withDeathCommit(null); //
038        }
039
040        /**
041         * Ensures that the {@link TrackedFinding} is {@linkplain #build() built} with
042         * the given {@linkplain TrackedFinding#getId() ID}.
043         */
044        public TestTrackedFindingBuilder withId(String id) {
045                this.id = requireNonNull(id);
046                return this;
047        }
048
049        /**
050         * Ensures that the {@link TrackedFinding} is {@linkplain #build() built} with
051         * the given {@linkplain TrackedFinding#getBirthCommit() birth commit}.
052         */
053        public TestTrackedFindingBuilder withBirthCommit(CommitDescriptor birthCommit) {
054                this.birthCommit = requireNonNull(birthCommit);
055                return this;
056        }
057
058        /**
059         * Ensures that the {@link TrackedFinding} is {@linkplain #build() built} with
060         * the given {@linkplain TrackedFinding#getDeathCommit() death commit}.
061         */
062        public TestTrackedFindingBuilder withDeathCommit(CommitDescriptor deathCommit) {
063                this.deathCommit = deathCommit;
064                return this;
065        }
066
067        /**
068         * Ensures that the {@link TrackedFinding} is {@linkplain #build() built} with
069         * the given {@linkplain TrackedFinding#getAssessment assessment}.
070         */
071        public TestTrackedFindingBuilder withAssessment(ETrafficLightColor assessment) {
072                indexFinding.withAssessment(assessment);
073                return this;
074        }
075
076        /**
077         * Builds the {@link TrackedFinding}.
078         *
079         * @return {@link TrackedFinding} whose fields are all initialized with
080         *         arbitrary but valid values, unless overwritten using this builder's
081         *         methods.
082         */
083        public TrackedFinding build() {
084                String findingIndexPartition = null; // null as not findingIndexPartition is not serialized
085                return new TrackedFinding(indexFinding.build(), id, birthCommit, findingIndexPartition, deathCommit);
086        }
087}