001/*-------------------------------------------------------------------------+
002|                                                                          |
003| Copyright (c) 2009-2019 CQSE GmbH                                        |
004|                                                                          |
005+-------------------------------------------------------------------------*/
006package org.conqat.engine.commons.findings;
007
008import static java.util.Collections.emptyMap;
009import static java.util.Objects.requireNonNull;
010
011import java.util.Map;
012import java.util.concurrent.atomic.AtomicInteger;
013
014import org.conqat.engine.commons.findings.location.ElementLocation;
015import org.conqat.engine.commons.findings.location.TextRegionLocation;
016import org.conqat.lib.commons.assessment.ETrafficLightColor;
017
018/**
019 * Builds {@link DetachedFinding}s for test purposes.
020 */
021public class TestDetachedFindingBuilder {
022
023        /**
024         * The number of instances built so far.
025         */
026        private static AtomicInteger instanceCounter = new AtomicInteger();
027
028        private String group;
029        private String category;
030        private String message;
031        private ElementLocation location;
032        private ETrafficLightColor assessment;
033
034        /**
035         * @return a builder creating some fresh {@link DetachedFinding} whose fields
036         *         are all initialized with valid values.
037         */
038        public static TestDetachedFindingBuilder someDetachedFinding() {
039                int instance = instanceCounter.incrementAndGet();
040                TestDetachedFindingBuilder someDetachedFinding = new TestDetachedFindingBuilder();
041                return someDetachedFinding.withGroup("Some Group") //
042                                .withCategory("Some Category") //
043                                .withMessage("Some Finding") //
044                                .withLocation(new TextRegionLocation(String.format("Example%d.java", instance),
045                                                String.format("org/example/Example%d.java", instance), //
046                                                instance * 1000, instance * 1001, // offsets
047                                                instance, instance + 1)) // lines
048                                .withAssessment(ETrafficLightColor.RED);
049        }
050
051        /**
052         * Ensures that the {@link DetachedFinding} is {@linkplain #build() built} with
053         * the given {@linkplain DetachedFinding#getGroupName() group}.
054         */
055        public TestDetachedFindingBuilder withGroup(String group) {
056                this.group = requireNonNull(group);
057                return this;
058        }
059
060        /**
061         * Ensures that the {@link DetachedFinding} is {@linkplain #build() built} with
062         * the given {@linkplain DetachedFinding#getCategoryName() category}.
063         */
064        public TestDetachedFindingBuilder withCategory(String category) {
065                this.category = requireNonNull(category);
066                return this;
067        }
068
069        /**
070         * Ensures that the {@link DetachedFinding} is {@linkplain #build() built} with
071         * the given {@linkplain DetachedFinding#getMessage() message}.
072         */
073        public TestDetachedFindingBuilder withMessage(String message) {
074                this.message = requireNonNull(message);
075                return this;
076        }
077
078        /**
079         * Ensures that the {@link DetachedFinding} is {@linkplain #build() built} with
080         * the given {@linkplain DetachedFinding#getAssessment() assessment}.
081         */
082        public TestDetachedFindingBuilder withAssessment(ETrafficLightColor assessment) {
083                this.assessment = assessment;
084                return this;
085        }
086
087        /**
088         * Ensures that the {@link DetachedFinding} is {@linkplain #build() built} with
089         * the given {@linkplain DetachedFinding#getLocation() location}.
090         */
091        public TestDetachedFindingBuilder withLocation(ElementLocation location) {
092                this.location = requireNonNull(location);
093                return this;
094        }
095
096        /**
097         * Builds the {@link DetachedFinding}.
098         *
099         * @return {@link DetachedFinding} whose fields are all initialized with
100         *         arbitrary but valid values, unless overwritten using this builder's
101         *         methods.
102         */
103        public DetachedFinding build() {
104                Map<String, Object> findingProperties = emptyMap(); // Properties not yet supported by this builder
105                return new DetachedFinding(group, category, message, location, assessment, findingProperties);
106        }
107}