001package org.conqat.engine.index.shared.tests;
002
003import static com.teamscale.commons.commit.CommitDescriptorTestUtils.masterCommit;
004
005import java.time.Duration;
006import java.util.ArrayList;
007import java.util.List;
008
009import org.conqat.engine.index.shared.CommitDescriptor;
010import org.conqat.engine.index.shared.ParentedCommitDescriptor;
011import org.conqat.lib.commons.uniformpath.UniformPathCompatibilityUtil;
012
013/** Builder for creating {@link TestExecutionWithPartition}s for testing. */
014public class TestTestExecutionWithPartitionBuilder {
015
016        private String uniformPath;
017        private long durationSeconds;
018        private ETestExecutionResult result;
019        private String message;
020        private CommitDescriptor commit;
021        private String partition;
022        private boolean isArtificialMergeTestExecution;
023        private List<CommitDescriptor> predecessorCommits = new ArrayList<>();
024
025        /**
026         * Creates a {@link TestTestExecutionWithPartitionBuilder} with some default
027         * values for the {@link TestExecutionWithPartition}.
028         */
029        public static TestTestExecutionWithPartitionBuilder someTestExecution() {
030                return new TestTestExecutionWithPartitionBuilder()//
031                                .withUniformPath("TestClass/testCase")//
032                                .withDurationSeconds(1)//
033                                .withResult(ETestExecutionResult.PASSED)//
034                                .withMessage(null)//
035                                .withCommit(masterCommit(1))//
036                                .withPartition("Unit Tests")//
037                                .withIsArtificialMergeTestExecution(false);
038        }
039
040        /** Sets {@link #uniformPath}. */
041        public TestTestExecutionWithPartitionBuilder withUniformPath(String uniformPath) {
042                this.uniformPath = uniformPath;
043                return this;
044        }
045
046        /** Sets {@link #durationSeconds}. */
047        public TestTestExecutionWithPartitionBuilder withDurationSeconds(long durationSeconds) {
048                this.durationSeconds = durationSeconds;
049                return this;
050        }
051
052        /** Sets {@link #result}. */
053        public TestTestExecutionWithPartitionBuilder withResult(ETestExecutionResult result) {
054                this.result = result;
055                return this;
056        }
057
058        /** Sets {@link #message}. */
059        public TestTestExecutionWithPartitionBuilder withMessage(String message) {
060                this.message = message;
061                return this;
062        }
063
064        /**
065         * Sets {@link #commit} as well as the {@link #predecessorCommits} if the commit
066         * is a {@link ParentedCommitDescriptor}.
067         */
068        public TestTestExecutionWithPartitionBuilder withCommit(CommitDescriptor commit) {
069                this.commit = commit;
070                if (commit instanceof ParentedCommitDescriptor) {
071                        predecessorCommits.addAll(((ParentedCommitDescriptor) commit).getParentCommits());
072                }
073                return this;
074        }
075
076        /** Sets {@link #partition}. */
077        public TestTestExecutionWithPartitionBuilder withPartition(String partition) {
078                this.partition = partition;
079                return this;
080        }
081
082        /** Sets {@link #isArtificialMergeTestExecution}. */
083        public TestTestExecutionWithPartitionBuilder withIsArtificialMergeTestExecution(
084                        boolean isArtificialMergeTestExecution) {
085                this.isArtificialMergeTestExecution = isArtificialMergeTestExecution;
086                return this;
087        }
088
089        /** Returns the built {@link TestExecutionWithPartition}. */
090        public TestExecutionWithPartition build() {
091                TestExecution testExecution = TestExecution.Builder//
092                                .fromPath(UniformPathCompatibilityUtil.convertRelative(uniformPath))//
093                                .setResult(result)//
094                                .setDuration(Duration.ofSeconds(durationSeconds))//
095                                .setFailureMessage(message)//
096                                .build();
097
098                return new TestExecutionWithPartition(testExecution, partition, commit, predecessorCommits,
099                                isArtificialMergeTestExecution);
100        }
101}