org.conqat.engine.index.shared.TestTrackedFindingBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of teamscale-commons-test-fixtures Show documentation
Show all versions of teamscale-commons-test-fixtures Show documentation
Provides common DTOs for Teamscale
/*
* Copyright (c) CQSE GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.conqat.engine.index.shared;
import static java.util.Objects.requireNonNull;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.atomic.AtomicInteger;
import org.conqat.engine.commons.findings.StatementPathElement;
import org.conqat.engine.commons.findings.location.ElementLocation;
import org.conqat.engine.commons.findings.location.TextRegionLocation;
import org.conqat.lib.commons.assessment.ETrafficLightColor;
/**
* Builds {@link TrackedFinding}s for test purposes.
*/
public class TestTrackedFindingBuilder {
/**
* The source of a unique but otherwise arbitrary instance number for each instance created by this
* builder. Initialized with a random value to make it impossible for tests to rely on any specific
* value. If a test depends on a particular value, it must make this dependence explicit by calling
* one of the builder's methods.
*/
private static final AtomicInteger instanceNumber = new AtomicInteger(ThreadLocalRandom.current().nextInt(1000));
private TestIndexFindingBuilder indexFinding = TestIndexFindingBuilder.someIndexFinding();
private String id;
private CommitDescriptor birthCommit;
private CommitDescriptor deathCommit;
private String findingIndexPartition;
/**
* @return a builder creating some fresh {@link TrackedFinding} whose fields are all initialized
* with valid values.
*/
public static TestTrackedFindingBuilder someTrackedFinding() {
int instance = instanceNumber.incrementAndGet();
long birth = 1000000000L + instance;
TestTrackedFindingBuilder someTrackedFinding = new TestTrackedFindingBuilder();
return someTrackedFinding.withId(Integer.toHexString(instance)) //
.withBirthCommit(new CommitDescriptor("branch" + instance, birth)) //
.withDeathCommit(null) //
.withPartition("some-partition");
}
private TestTrackedFindingBuilder() {
// Intentionally private. Clients must obtain Builder via someTrackedFinding().
}
/**
* Ensures that the {@link TrackedFinding} is {@linkplain #build() built} with the given
* {@linkplain TrackedFinding#getId() ID}.
*/
public TestTrackedFindingBuilder withId(String id) {
this.id = requireNonNull(id);
return this;
}
/**
* Ensures that the {@link TrackedFinding} is {@linkplain #build() built} with the given
* {@linkplain TrackedFinding#getBirthCommit() birth commit}.
*/
public TestTrackedFindingBuilder withBirthCommit(CommitDescriptor birthCommit) {
this.birthCommit = requireNonNull(birthCommit);
return this;
}
/**
* Ensures that the {@link TrackedFinding} is {@linkplain #build() built} with the given
* {@linkplain TrackedFinding#getDeathCommit() death commit}.
*/
public TestTrackedFindingBuilder withDeathCommit(CommitDescriptor deathCommit) {
this.deathCommit = deathCommit;
return this;
}
/**
* Ensures that the {@link TrackedFinding} is {@linkplain #build() built} with the given partition.
*/
public TestTrackedFindingBuilder withPartition(String partition) {
findingIndexPartition = partition;
return this;
}
/**
* Ensures that the {@link TrackedFinding} is {@linkplain #build() built} with the given
* {@linkplain TrackedFinding#getAssessment assessment}.
*/
public TestTrackedFindingBuilder withAssessment(ETrafficLightColor assessment) {
indexFinding.withAssessment(assessment);
return this;
}
/**
* Ensures that the {@link TrackedFinding} is {@linkplain #build() built} with the given
* {@linkplain TrackedFinding#getCategoryName() category}.
*/
public TestTrackedFindingBuilder withCategory(String category) {
indexFinding.withCategory(category);
return this;
}
/**
* Ensures that the {@link TrackedFinding} is {@linkplain #build() built} with the given
* {@linkplain TrackedFinding#getMessage() message}.
*/
public TestTrackedFindingBuilder withMessage(String message) {
indexFinding.withMessage(message);
return this;
}
/**
* Ensures that the {@link TrackedFinding} is {@linkplain #build() built} with the given
* {@linkplain TrackedFinding#getLocation() location}.
*/
public TestTrackedFindingBuilder withLocation(ElementLocation location) {
indexFinding.withLocation(location);
return this;
}
/**
* Ensures that the {@link TrackedFinding} is {@linkplain #build() built} with an element location
* that has the { {@link ElementLocation#getUniformPath()} ()} set to the given uniform path.
*/
public TestTrackedFindingBuilder withLocation(String uniformPath) {
indexFinding.withLocation(uniformPath);
return this;
}
/**
* Ensures that the {@link TrackedFinding} is {@linkplain #build() built} with the given
* {@linkplain TextRegionLocation#getRawStartOffset() start} and
* {@linkplain TextRegionLocation#getRawEndOffset() end} offsets
*/
public TestTrackedFindingBuilder withStartAndEndOffsets(int startOffset, int endOffset) {
indexFinding.withStartAndEndOffsets(startOffset, endOffset);
return this;
}
/**
* Ensures that the {@link TrackedFinding} is {@linkplain #build() built} with the given
* {@linkplain TextRegionLocation#getRawStartLine() start} and
* {@linkplain TextRegionLocation#getRawEndLine() end} lines
*/
public TestTrackedFindingBuilder withStartAndEndLine(int startLine, int endLine) {
indexFinding.withStartAndEndLine(startLine, endLine);
return this;
}
/**
* Ensures that the {@link TrackedFinding} is {@linkplain #build() built} with the given
* {@linkplain TrackedFinding#getGroupName() group name}.
*/
public TestTrackedFindingBuilder withGroupName(String groupName) {
indexFinding.withGroupName(groupName);
return this;
}
/**
* Ensures that the {@link TrackedFinding} is {@linkplain #build() built} with the given
* {@linkplain TrackedFinding#getTypeId()} () typeId}.
*/
public TestTrackedFindingBuilder withTypeId(String typeId) {
indexFinding.withTypeId(typeId);
return this;
}
/**
* Ensures that the {@link TrackedFinding} is {@linkplain #build() built} with the given
* {@linkplain TrackedFinding#getStatementPath()} statementPathElement}.
*/
public TestTrackedFindingBuilder withStatementPathElement(StatementPathElement pathElement) {
indexFinding.withStatementPathElement(pathElement);
return this;
}
/**
* Ensures that the {@link TrackedFinding} is {@linkplain #build() built} with the given property.
*/
public TestTrackedFindingBuilder withFindingProperty(String key, Object property) {
indexFinding.withFindingProperty(key, property);
return this;
}
/**
* Builds the {@link TrackedFinding}.
*
* @return {@link TrackedFinding} whose fields are all initialized with arbitrary but valid values,
* unless overwritten using this builder's methods.
*/
public TrackedFinding build() {
return new TrackedFinding(indexFinding.build(), id, birthCommit, findingIndexPartition, deathCommit);
}
}