Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests for condition based on build description #131

Merged
merged 1 commit into from
Jun 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package pl.damianszczepanik.jenkins.buildhistorymanager.descriptors.conditions;

import static org.assertj.core.api.Assertions.assertThat;

import hudson.model.AbstractDescribableImpl;
import hudson.model.Descriptor;
import hudson.util.ListBoxModel;
import org.jenkinsci.plugins.structs.SymbolLookup;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;
import pl.damianszczepanik.jenkins.buildhistorymanager.model.conditions.BuildDescriptionCondition.MatchingMethodType;

/**
* @author Damian Szczepanik (damianszczepanik@github)
*/
public class BuildDescriptionDescriptorTest {

@Rule
public JenkinsRule j = new JenkinsRule();

@Test
public void doFillMatchingMethodItems_ReturnsMethodItems() {

// given
BuildDescriptionDescriptor descriptor = (BuildDescriptionDescriptor)
SymbolLookup.get().findDescriptor(AbstractDescribableImpl.class, "BuildDescription");

// when
ListBoxModel listBoxModel = descriptor.doFillMatchingMethodItems();

// then
assertThat(listBoxModel.stream().map(option -> option.value))
.containsExactly(
MatchingMethodType.EQUALS.name(),
MatchingMethodType.CONTAINS.name(),
MatchingMethodType.MATCHES.name());
}

@Test
public void getDisplayName_ReturnsDescriptorName() {

// given
Descriptor descriptor = SymbolLookup.get().findDescriptor(AbstractDescribableImpl.class, "BuildDescription");

// when
String displayName = descriptor.getDisplayName();

// then
assertThat(displayName).isEqualTo("Build description");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public void matches_OnTodayBuild_ReturnsTrue() throws IOException {
// when
boolean matches = condition.matches(run, null);

// then
assertThat(matches).isTrue();
}

Expand All @@ -74,6 +75,7 @@ public void matches_OnBuildTimeBelowMax_ReturnsFalse() throws IOException {
// when
boolean matches = condition.matches(run, null);

// then
assertThat(matches).isFalse();
}

Expand All @@ -92,6 +94,7 @@ public void matches_OnBuildTimeAboveMin_ReturnsFalse() throws IOException {
// when
boolean matches = condition.matches(run, null);

// then
assertThat(matches).isFalse();
}

Expand All @@ -109,6 +112,7 @@ public void matches_OnBuildTimeInRange_ReturnsTrue() throws IOException {
// when
boolean matches = condition.matches(run, null);

// then
assertThat(matches).isTrue();
}

Expand All @@ -125,6 +129,7 @@ public void matches_OnInvalidRange_ReturnsFalse() throws IOException {
// when
boolean matches = condition.matches(run, null);

// then
assertThat(matches).isFalse();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package pl.damianszczepanik.jenkins.buildhistorymanager.model.conditions;

import static org.assertj.core.api.Assertions.assertThat;

import java.io.IOException;

import hudson.model.Run;
import org.junit.Test;
import pl.damianszczepanik.jenkins.buildhistorymanager.model.conditions.BuildDescriptionCondition.MatchingMethodType;
import pl.damianszczepanik.jenkins.buildhistorymanager.utils.RunStub;

/**
* @author Damian Szczepanik (damianszczepanik@github)
*/
public class BuildDescriptionConditionTest {

@Test
public void setPattern_SetsPattern() {

// given
BuildDescriptionCondition condition = new BuildDescriptionCondition();
String pattern = "[release] 22.0r5";
condition.setPattern(pattern);

// when
String returnedPattern = condition.getPattern();

// then
assertThat(returnedPattern).isEqualTo(pattern);
}

@Test
public void setMatchingMethod_SetsMatchingMethod() {

// given
BuildDescriptionCondition condition = new BuildDescriptionCondition();
MatchingMethodType matchingMethodType = MatchingMethodType.CONTAINS;

// make sure that default matching method is not same
// as the one used by this test
assertThat(condition.getMatchingMethod()).isNotEqualTo(matchingMethodType.name());
condition.setMatchingMethod(matchingMethodType.name());

// when
String returnedMethodType = condition.getMatchingMethod();

// then
assertThat(returnedMethodType).isEqualTo(matchingMethodType.name());
}

@Test
public void matches_OnEqualDescription_ReturnsTrue() throws IOException {

// given
BuildDescriptionCondition condition = new BuildDescriptionCondition();
condition.setMatchingMethod(MatchingMethodType.EQUALS.name());
final String description = "[release] 11.3.4.1";
condition.setPattern(description);

Run<?, ?> run = new RunStub();
run.setDescription(description);

// when
boolean matches = condition.matches(run, null);

// then
assertThat(matches).isTrue();
}

@Test
public void matches_OnLongerDescription_ReturnsTrue() throws IOException {

// given
BuildDescriptionCondition condition = new BuildDescriptionCondition();
condition.setMatchingMethod(MatchingMethodType.CONTAINS.name());
final String description = "[release] 1987.3.14";
condition.setPattern(description);

Run<?, ?> run = new RunStub();
run.setDescription(description + "some extra description");

// when
boolean matches = condition.matches(run, null);

// then
assertThat(matches).isTrue();
}

@Test
public void matches_OnMatchedDescription_ReturnsTrue() throws IOException {

// given
BuildDescriptionCondition condition = new BuildDescriptionCondition();
condition.setMatchingMethod(MatchingMethodType.MATCHES.name());
final String description = "[release] 11.3.4";
condition.setPattern(".*release.*");

Run<?, ?> run = new RunStub();
run.setDescription(description + "some extra description");

// when
boolean matches = condition.matches(run, null);

// then
assertThat(matches).isTrue();
}

@Test
public void matches_OnNullDescription_ReturnsFalse() throws IOException {

// given
BuildDescriptionCondition condition = new BuildDescriptionCondition();

Run<?, ?> run = new RunStub();
run.setDescription(null);

// when
boolean matches = condition.matches(run, null);

// then
assertThat(matches).isFalse();
}

}
Loading