Skip to content
This repository has been archived by the owner on Apr 6, 2022. It is now read-only.

Commit

Permalink
Merge pull request #3 from AlexanderPraegla/JENKINS-50074
Browse files Browse the repository at this point in the history
 JENKINS-50074 Mockito Test for ByIdResultSelector
  • Loading branch information
AlexanderPraegla committed Apr 11, 2018
2 parents e424421 + ce0e81d commit 2267c09
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 48 deletions.
2 changes: 1 addition & 1 deletion clean.sh
@@ -1,4 +1,4 @@
#!/bin/sh
#!/bin/bash

if [[ -z "$JENKINS_HOME" ]]; then
echo "JENKINS_HOME is not defined" 1>&2
Expand Down
2 changes: 1 addition & 1 deletion go.sh
@@ -1,4 +1,4 @@
#!/bin/sh
#!/bin/bash

if [[ -z "$JENKINS_HOME" ]]; then
echo "JENKINS_HOME is not defined" 1>&2
Expand Down
2 changes: 1 addition & 1 deletion jenkins.sh
@@ -1,4 +1,4 @@
#!/bin/sh
#!/bin/bash

if [[ -z "$JENKINS_HOME" ]]; then
echo "JENKINS_HOME is not defined" 1>&2
Expand Down
2 changes: 1 addition & 1 deletion skip.sh
@@ -1,4 +1,4 @@
#!/bin/sh
#!/bin/bash

if [[ -z "$JENKINS_HOME" ]]; then
echo "JENKINS_HOME is not defined" 1>&2
Expand Down
@@ -1,10 +1,12 @@
package io.jenkins.plugins.analysis.core.model; // NOPMD

import javax.annotation.CheckForNull;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand All @@ -15,7 +17,6 @@
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.annotation.CheckForNull;

import org.apache.commons.lang3.StringUtils;
import org.eclipse.collections.api.list.ImmutableList;
Expand All @@ -24,6 +25,10 @@
import org.kohsuke.stapler.export.Exported;
import org.kohsuke.stapler.export.ExportedBean;

import edu.hm.hafner.analysis.Issue;
import edu.hm.hafner.analysis.Issues;
import edu.hm.hafner.analysis.Priority;
import edu.hm.hafner.util.VisibleForTesting;
import io.jenkins.plugins.analysis.core.JenkinsFacade;
import io.jenkins.plugins.analysis.core.history.ReferenceProvider;
import io.jenkins.plugins.analysis.core.quality.AnalysisBuild;
Expand All @@ -35,11 +40,6 @@
import hudson.model.Result;
import hudson.model.Run;

import edu.hm.hafner.analysis.Issue;
import edu.hm.hafner.analysis.Issues;
import edu.hm.hafner.analysis.Priority;
import edu.hm.hafner.util.VisibleForTesting;

/**
* Stores the results of a static analysis run. This class is capable of storing a reference to the current build.
* Provides support for persisting the results of the build and loading and saving of issues (all, new, and fixed) and
Expand Down Expand Up @@ -548,6 +548,19 @@ public Map<String, Integer> getSizePerOrigin() {
return Maps.immutable.ofAll(sizePerOrigin).toMap();
}

/**
* Returns the number of issues in this analysis run, mapped by priority.
*
* @return number of issues per priority
*/
public Map<Priority, Integer> getSizePerPriority() {
EnumMap<Priority, Integer> sizePerPriority = new EnumMap<>(Priority.class);
sizePerPriority.put(Priority.HIGH, highPrioritySize);
sizePerPriority.put(Priority.NORMAL, normalPrioritySize);
sizePerPriority.put(Priority.LOW, lowPrioritySize);
return sizePerPriority;
}

/**
* Returns the associated build that this run was part of.
*
Expand Down
@@ -1,11 +1,11 @@
package io.jenkins.plugins.analysis.core.quality;

import java.io.Serializable;
import java.util.Map;

import org.jvnet.localizer.Localizable;

import edu.hm.hafner.analysis.Priority;
import io.jenkins.plugins.analysis.core.model.AnalysisResult;

import hudson.model.HealthReport;
import hudson.plugins.analysis.Messages;
Expand Down Expand Up @@ -38,49 +38,49 @@ public HealthReportBuilder(final HealthDescriptor healthDescriptor) {
* greater than {@link HealthDescriptor#getUnHealthy()}. The computation takes only annotations of the specified
* severity into account.
*
* @param result
* annotations of the current build
* @param sizePerPriority
* number of issues per priority
*
* @return the healthiness of a build
*/
public HealthReport computeHealth(final AnalysisResult result) {
int numberOfAnnotations = 0;
public HealthReport computeHealth(final Map<Priority, Integer> sizePerPriority) {
int size = 0;
for (Priority priority : Priority.collectPrioritiesFrom(healthDescriptor.getMinimumPriority())) {
numberOfAnnotations += result.getTotalSize(priority);
size += sizePerPriority.get(priority);
}

if (healthDescriptor.isEnabled()) {
int percentage;
int healthy = healthDescriptor.getHealthy();
if (numberOfAnnotations < healthy) {
if (size < healthy) {
percentage = 100;
}
else {
int unHealthy = healthDescriptor.getUnHealthy();
if (numberOfAnnotations > unHealthy) {
if (size > unHealthy) {
percentage = 0;
}
else {
percentage = 100 - ((numberOfAnnotations - healthy) * 100 / (unHealthy - healthy));
percentage = 100 - ((size - healthy) * 100 / (unHealthy - healthy));
}
}

return new HealthReport(percentage, getDescription(result));
return new HealthReport(percentage, getDescription(size));
}
return null;
}


private Localizable getDescription(final AnalysisResult result) {
private Localizable getDescription(final int size) {
String name = "Static Analysis"; // FIXME: extract from IssueParser.find(id)
if (result.getTotalSize() == 0) {
if (size == 0) {
return Messages._ResultAction_HealthReportNoItem(name);
}
else if (result.getTotalSize() == 1) {
else if (size == 1) {
return Messages._ResultAction_HealthReportSingleItem(name);
}
else {
return Messages._ResultAction_HealthReportMultipleItem(name, result.getTotalSize());
return Messages._ResultAction_HealthReportMultipleItem(name, size);
}
}
}
Expand Down
Expand Up @@ -108,7 +108,7 @@ public String getUrlName() {

@Override
public HealthReport getBuildHealth() {
return new HealthReportBuilder(healthDescriptor).computeHealth(getResult());
return new HealthReportBuilder(healthDescriptor).computeHealth(getResult().getSizePerPriority());
}

@Override
Expand Down
Expand Up @@ -6,59 +6,111 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/**
* Tests the class {@link ByIdResultSelector}
*
* @author Alexander Praegla
*/
class ByIdResultSelectorTest {

private ByIdResultSelector cut;
/**
* Verifies the returned optional is empty if the provided list of {@link ResultAction} is empty
*/
@Test
void shouldBeEmptyIfActionsAreEmpty() {
ByIdResultSelector cut = new ByIdResultSelector("1");

Run<?, ?> run = createRunStub(new ArrayList<>());

assertThat(cut.get(run)).isEmpty();
}

/**
* Verifies the returned optional is empty if the provided list of {@link ResultAction}
* does not contains the id of the instance of {@link ByIdResultSelector}
*/
@Test
void shouldBeEmptyOptionalFromEmptyList() {
cut = new ByIdResultSelector("1");
Run<?, ?> run = mock(Run.class);
when(run.getActions(ResultAction.class)).thenReturn(new ArrayList<>());
void shouldBeEmptyIfActionsHasDifferentId() {
ByIdResultSelector cut = new ByIdResultSelector("1");

List<ResultAction> resultActions = new ArrayList<>();
resultActions.add(createResultActionStub("2"));

Optional<ResultAction> resultAction = cut.get(run);
assertThat(resultAction).isEmpty();
Run<?, ?> run = createRunStub(resultActions);

assertThat(cut.get(run)).isEmpty();
}

/**
* Verifies the returned optional is not empty if the provided list of {@link ResultAction} contains one element
*/
@Test
void shouldBeEmptyOptionalFromList() {
cut = new ByIdResultSelector("1");
ResultAction resultAction = mock(ResultAction.class);
when(resultAction.getId()).thenReturn("2");
void shouldFindActionWithSameId() {
ByIdResultSelector cut = new ByIdResultSelector("1");

List<ResultAction> resultActions = new ArrayList<>();
resultActions.add(resultAction);
resultActions.add(createResultActionStub("1"));

Run<?, ?> run = mock(Run.class);
when(run.getActions(ResultAction.class)).thenReturn(resultActions);
Run<?, ?> run = createRunStub(resultActions);

assertThat(cut.get(run)).isPresent();
}

/**
* Verifies the returned optional is empty if the provided list of {@link ResultAction} contains two elements with different id
*/
@Test
void shouldFindEmptyIfListContainsTwoDifferentIds() {
ByIdResultSelector cut = new ByIdResultSelector("1");

List<ResultAction> resultActions = new ArrayList<>();
resultActions.add(createResultActionStub("2"));
resultActions.add(createResultActionStub("3"));

Run<?, ?> run = createRunStub(resultActions);

assertThat(cut.get(run)).isEmpty();
}

/**
* Verifies the returned optional is not empty if the provided list of {@link ResultAction} contains two elements
*/
@Test
void shouldBeNonEmptyOptional() {
cut = new ByIdResultSelector("1");
ResultAction resultAction = mock(ResultAction.class);
when(resultAction.getId()).thenReturn("1");
void shouldFindActionWithSameIdIfListContainsTwoDifferentIds() {
ByIdResultSelector cut = new ByIdResultSelector("1");

List<ResultAction> resultActions = new ArrayList<>();
resultActions.add(resultAction);
resultActions.add(createResultActionStub("2"));
resultActions.add(createResultActionStub("1"));

Run<?, ?> run = mock(Run.class);
when(run.getActions(ResultAction.class)).thenReturn(resultActions);
Run<?, ?> run = createRunStub(resultActions);

assertThat(cut.get(run)).isPresent();
}

/**
* Verifies the toString method
*/
@Test
void testToString() {
cut = new ByIdResultSelector("1");
ByIdResultSelector cut = new ByIdResultSelector("1");
assertThat(cut.toString()).isEqualTo("io.jenkins.plugins.analysis.core.views.ResultAction with ID 1");
}

private Run<?, ?> createRunStub(List<ResultAction> resultActions) {
Run<?, ?> run = mock(Run.class);
when(run.getActions(ResultAction.class)).thenReturn(resultActions);
return run;
}

private ResultAction createResultActionStub(String mockedIdValue) {
ResultAction resultAction = mock(ResultAction.class);
when(resultAction.getId()).thenReturn(mockedIdValue);
return resultAction;
}
}
@@ -0,0 +1,24 @@
package io.jenkins.plugins.analysis.core.steps;

import org.junit.jupiter.api.Test;

import io.jenkins.plugins.analysis.core.steps.IssuesRecorder.Descriptor;
import static io.jenkins.plugins.analysis.core.testutil.FormValidationAssert.*;

import hudson.util.FormValidation;

/**
* Tests the class {@link IssuesRecorder}.
*
* @author Ullrich Hafner
*/
class IssuesRecorderTest {
@Test
void shouldBeOkIfIfEncodingIsEmpty() {
Descriptor descriptor = new Descriptor();

FormValidation actualResult = descriptor.doCheckSourceCodeEncoding("");

assertThat(actualResult).isOk();
}
}

0 comments on commit 2267c09

Please sign in to comment.