Skip to content

Commit 00d9d02

Browse files
authored
BitConDec-11: Enable unit testing with Jacoco (#4)
* Add interface for ApplicationLinkService class to make its responsibility clearer * Get CommitService via ComponentLocator instead of injecting it for better testability * Add interface for MergeCheckHandler and rename merge check classes, separate MergChecking bean from completeness checking logic * Add Jacoco maven plugin to generate coverage reports and send them to Codecov GitHub plugin in .travis.yml * Test completion checking in JSON String * Refactor and test method to retrieve project key from text * Add mock package and class for a pull request * Add test for getCommitsOfPullRequest method
1 parent 553e6de commit 00d9d02

File tree

16 files changed

+674
-354
lines changed

16 files changed

+674
-354
lines changed

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ before_install:
1515
- export PATH=opt/atlassian-plugin-sdk/bin:opt/atlassian-plugin-sdk/apache-maven-*/bin:$PATH
1616
install: true
1717
script:
18-
- atlas-package
18+
- atlas-package -P jacoco
1919
notifications:
2020
email: false
2121
deploy:
@@ -28,4 +28,4 @@ deploy:
2828
tags: true
2929
repo: cures-hub/cures-condec-bitbucket
3030
after_success:
31-
- bash <(curl -s https://codecov.io/bash)
31+
- bash <(curl -s https://codecov.io/bash) -f $TRAVIS_BUILD_DIR/target/site/jacoco/jacoco.xml

pom.xml

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -188,26 +188,49 @@
188188
<verbose>false</verbose>
189189
</configuration>
190190
</plugin>
191+
</plugins>
192+
</build>
193+
194+
<profiles>
195+
<!-- Creates coverage reports in target/site/jacoco/ -->
196+
<profile>
197+
<id>jacoco</id>
198+
<activation>
199+
<activeByDefault>false</activeByDefault>
200+
</activation>
201+
<build>
202+
<plugins>
191203
<plugin>
192-
<groupId>org.codehaus.mojo</groupId>
193-
<artifactId>cobertura-maven-plugin</artifactId>
194-
<version>2.7</version>
195-
<configuration>
196-
<formats>
197-
<format>html</format>
198-
<format>xml</format>
199-
</formats>
200-
<check />
201-
</configuration>
204+
<groupId>org.jacoco</groupId>
205+
<artifactId>jacoco-maven-plugin</artifactId>
206+
<version>${jacoco-version}</version>
207+
<executions>
208+
<execution>
209+
<goals>
210+
<goal>prepare-agent</goal>
211+
</goals>
212+
</execution>
213+
<!-- attached to Maven test phase -->
214+
<execution>
215+
<id>report</id>
216+
<phase>test</phase>
217+
<goals>
218+
<goal>report</goal>
219+
</goals>
220+
</execution>
221+
</executions>
202222
</plugin>
203223
</plugins>
204-
</build>
224+
</build>
225+
</profile>
226+
</profiles>
205227

206228
<properties>
207229
<bitbucket.version>6.6.2</bitbucket.version>
208230
<bitbucket.data.version>${bitbucket.version}</bitbucket.data.version>
209231
<amps.version>8.0.2</amps.version>
210232
<atlassian.spring.scanner.version>2.1.12</atlassian.spring.scanner.version>
233+
<jacoco-version>0.8.4</jacoco-version>
211234
<!-- This key is used to keep the consistency between the key in atlassian-plugin.xml and the key to
212235
generate bundle. -->
213236
<atlassian.plugin.key>${project.groupId}.${project.artifactId}</atlassian.plugin.key>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package de.uhd.ifi.se.decision.management.bitbucket.merge.checks;
2+
3+
import java.util.Locale;
4+
import java.util.Set;
5+
6+
/**
7+
* Interface to check the completeness of the documentation of decision
8+
* knowledge related to a pull request.
9+
*/
10+
public interface CompletenessCheckHandler {
11+
12+
/**
13+
* Checks whether the documentation is complete.
14+
*
15+
* @return true if the documentation is complete.
16+
*/
17+
boolean isDocumentationComplete();
18+
19+
/**
20+
* Returns a set of Jira issues with an incomplete documentation.
21+
*
22+
* @return set of Jira issue keys.
23+
*/
24+
Set<String> getJiraIssuesWithIncompleteDocumentation();
25+
26+
/**
27+
* Looks for the first Jira issue key (e.g. CONDEC-1) in a String and returns the
28+
* Jira project key (e.g. CONDEC).
29+
*
30+
* @param text
31+
* that might contain a project key, e.g., a commit message, branch
32+
* name oder pull request title.
33+
* @return potential project key in upper case.
34+
*/
35+
static String retrieveProjectKey(String text) {
36+
if (text.indexOf("-") == -1) {
37+
// there is no Jira issue key (e.g. CONDEC-1) in the text
38+
return "";
39+
}
40+
String splitted = text.split("-")[0];
41+
String[] words = splitted.split(" ");
42+
String projectKey = words[words.length - 1];
43+
return projectKey.toUpperCase(Locale.ENGLISH);
44+
}
45+
46+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package de.uhd.ifi.se.decision.management.bitbucket.merge.checks;
2+
3+
import javax.annotation.Nonnull;
4+
5+
import org.springframework.stereotype.Component;
6+
7+
import com.atlassian.bitbucket.hook.repository.PreRepositoryHookContext;
8+
import com.atlassian.bitbucket.hook.repository.PullRequestMergeHookRequest;
9+
import com.atlassian.bitbucket.hook.repository.RepositoryHookResult;
10+
import com.atlassian.bitbucket.hook.repository.RepositoryMergeCheck;
11+
import com.atlassian.bitbucket.pull.PullRequest;
12+
13+
import de.uhd.ifi.se.decision.management.bitbucket.merge.checks.impl.CompletenessCheckHandlerImpl;
14+
15+
/**
16+
* Enforces that pull requests can only be accepted, i.e., the respective branch
17+
* can only be merged back to the maineline if the decision knowledge
18+
* documentation is complete. The completeness is determined in the
19+
* {@link CompletenessCheckHandler}.
20+
*
21+
*/
22+
@Component("completenessMergeCheck")
23+
public class CompletenessMergeCheck implements RepositoryMergeCheck {
24+
25+
@Nonnull
26+
@Override
27+
public RepositoryHookResult preUpdate(@Nonnull PreRepositoryHookContext context,
28+
@Nonnull PullRequestMergeHookRequest request) {
29+
PullRequest pullRequest = request.getPullRequest();
30+
31+
CompletenessCheckHandler completenessCheckHandler = new CompletenessCheckHandlerImpl(pullRequest);
32+
if (completenessCheckHandler.isDocumentationComplete()) {
33+
return RepositoryHookResult.accepted();
34+
}
35+
String summaryMessage = "There are not enough decision knowledge elements linked to the pull request.";
36+
String detailMessage = "Every Jira ticket has to have at least one decision problem (=issue) and one decision linked. "
37+
+ "Jira tickets with an incomplete documentation are: ";
38+
for (String jiraIssueKey : completenessCheckHandler.getJiraIssuesWithIncompleteDocumentation()) {
39+
detailMessage += jiraIssueKey + " ";
40+
}
41+
42+
// TODO
43+
// @issue Should the developers be allowed to merge the branch with an
44+
// incomplete decision knowledge documentation?
45+
// @decision It should be configurable on a setting page whether the incomplete
46+
// documentation enforces that the branch cannot be merged or not.
47+
return RepositoryHookResult.rejected(summaryMessage, detailMessage);
48+
}
49+
}

src/main/java/de/uhd/ifi/se/decision/management/bitbucket/merge/checks/HasDecisionKnowledgeCheck.java

Lines changed: 0 additions & 75 deletions
This file was deleted.

src/main/java/de/uhd/ifi/se/decision/management/bitbucket/merge/checks/MergeCheckDataHandler.java

Lines changed: 0 additions & 146 deletions
This file was deleted.

0 commit comments

Comments
 (0)