Skip to content

Commit

Permalink
[JENKINS-40718] Integration tests for case insensitive search
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcin Zajączkowski committed Feb 12, 2017
1 parent 065a042 commit 5a6103c
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 16 deletions.
18 changes: 2 additions & 16 deletions core/src/test/java/jenkins/widgets/HistoryPageFilterTest.java
Expand Up @@ -54,6 +54,8 @@

/**
* @author <a href="mailto:tom.fennelly@gmail.com">tom.fennelly@gmail.com</a>
*
* See also HistoryPageFilterInsensitiveSearchTest integration test.
*/
public class HistoryPageFilterTest {

Expand Down Expand Up @@ -340,22 +342,6 @@ public void test_search_should_be_case_sensitive_for_anonymous_user() throws IOE
Assert.assertEquals(0, historyPageFilter.runs.size());
}

@Test
@Ignore //User with insensitiveSearch enabled needs to be injected
public void test_search_runs_by_build_result() throws IOException {
//TODO: Set user with insensitiveSearch enabled
List<ModelObject> runs = ImmutableList.<ModelObject>of(new MockRun(2, Result.FAILURE), new MockRun(1, Result.SUCCESS));
assertOneMatchingBuildForGivenSearchStringAndRunItems("failure", runs);
}

@Test
@Ignore //User with insensitiveSearch enabled needs to be injected
public void test_case_insensitivity_in_search_runs() throws IOException {
//TODO: Set user with insensitiveSearch enabled
List<ModelObject> runs = ImmutableList.<ModelObject>of(new MockRun(2, Result.FAILURE), new MockRun(1, Result.SUCCESS));
assertOneMatchingBuildForGivenSearchStringAndRunItems("FAILure", runs);
}

@Test
public void test_search_builds_by_build_variables() throws IOException {
List<ModelObject> runs = ImmutableList.<ModelObject>of(
Expand Down
@@ -0,0 +1,114 @@
package jenkins.widgets;

import java.io.IOException;
import java.util.Collections;
import java.util.List;

import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;
import org.mockito.Mockito;

import com.google.common.collect.ImmutableList;
import hudson.model.Job;
import hudson.model.ModelObject;
import hudson.model.Queue;
import hudson.model.Result;
import hudson.model.Run;
import hudson.model.User;
import hudson.search.UserSearchProperty;
import hudson.security.ACL;
import hudson.security.ACLContext;
import hudson.security.AuthorizationStrategy;

/**
* TODO: Code partially duplicated with HistoryPageFilterTest in core
*/
public class HistoryPageFilterInsensitiveSearchTest {

private static final String TEST_USER_NAME = "testUser";

@Rule
public JenkinsRule j = new JenkinsRule();

@Test
public void should_search_insensitively_when_enabled_for_user() throws IOException {
setUserContextAndAssertCaseInsensitivitySearchForGivenSearchString("failure");
}

@Test
public void should_also_lower_search_query_in_insensitive_search_enabled() throws IOException {
setUserContextAndAssertCaseInsensitivitySearchForGivenSearchString("FAILure");
}

private void setUserContextAndAssertCaseInsensitivitySearchForGivenSearchString(final String searchString) throws IOException {
AuthorizationStrategy.Unsecured strategy = new AuthorizationStrategy.Unsecured();
j.jenkins.setAuthorizationStrategy(strategy);
j.jenkins.setSecurityRealm(j.createDummySecurityRealm());

UsernamePasswordAuthenticationToken testUserAuthentication = new UsernamePasswordAuthenticationToken(TEST_USER_NAME, "any");
try (ACLContext acl = ACL.as(testUserAuthentication)) {
User.get(TEST_USER_NAME).addProperty(new UserSearchProperty(true));

//test logic
List<ModelObject> runs = ImmutableList.<ModelObject>of(new MockRun(2, Result.FAILURE), new MockRun(1, Result.SUCCESS));
assertOneMatchingBuildForGivenSearchStringAndRunItems(searchString, runs);
}
}

private void assertOneMatchingBuildForGivenSearchStringAndRunItems(String searchString, List<ModelObject> runs) {
//given
HistoryPageFilter<ModelObject> historyPageFilter = new HistoryPageFilter<>(5);
//and
historyPageFilter.setSearchString(searchString);

//when
historyPageFilter.add(runs, Collections.<Queue.Item>emptyList());

//then
Assert.assertEquals(1, historyPageFilter.runs.size());
Assert.assertEquals(HistoryPageEntry.getEntryId(2), historyPageFilter.runs.get(0).getEntryId());
}

@SuppressWarnings("unchecked")
private static class MockRun extends Run {
private final long queueId;

public MockRun(long queueId) throws IOException {
super(Mockito.mock(Job.class));
this.queueId = queueId;
}

public MockRun(long queueId, Result result) throws IOException {
this(queueId);
this.result = result;
}

@Override
public int compareTo(Run o) {
return 0;
}

@Override
public Result getResult() {
return result;
}

@Override
public boolean isBuilding() {
return false;
}

@Override
public long getQueueId() {
return queueId;
}

@Override
public int getNumber() {
return (int) queueId;
}
}
}

0 comments on commit 5a6103c

Please sign in to comment.