Original file line number Diff line number Diff line change
Expand Up @@ -41,29 +41,40 @@
import java.util.concurrent.TimeUnit;

import jenkins.model.Jenkins;
import org.jvnet.hudson.test.HudsonTestCase;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.MockBuilder;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
import org.powermock.api.mockito.PowerMockito;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;

//CS IGNORE MagicNumber FOR NEXT 100 LINES. REASON: TestData.

/**
* Tests for the ScanOnDemandBaseAction.
* Tests for {@link ScanOnDemandBaseAction}.
* @author Shemeer Sulaiman <shemeer.x.sulaiman@sonymobile.com>
* @throws Exception if so.
*/
public class ScanOnDemandBaseActionTest extends HudsonTestCase {
public class ScanOnDemandBaseActionTest {

//CS IGNORE VisibilityModifier FOR NEXT 5 LINES. REASON: by design
/**
* The Jenkins rule, duh.
*/
@Rule
public JenkinsRule j = new JenkinsRule();

private static final String TO_PRINT = "ERROR";
/**
* Tests for performScanMethod by passing failed build.
*
* @throws Exception if so.
*/
@Test
public void testPerformScanFailedProject() throws Exception {
FreeStyleProject project = createFreeStyleProject();
FreeStyleProject project = j.createFreeStyleProject();
project.getBuildersList().add(new PrintToLogBuilder(TO_PRINT));
project.getBuildersList().add(new MockBuilder(Result.FAILURE));
Future<FreeStyleBuild> future = project.scheduleBuild2(0);
Expand All @@ -72,12 +83,8 @@ public void testPerformScanFailedProject() throws Exception {
build.getActions().remove(build.getAction(FailureCauseBuildAction.class));
}
assertNull(build.getAction(FailureCauseBuildAction.class));
assertBuildStatus(Result.FAILURE, build);
ScanOnDemandBaseAction sodbaseaction = new ScanOnDemandBaseAction(project);
sodbaseaction.setBuildType("nonscanned");
StaplerRequest mockrequest = PowerMockito.mock(StaplerRequest.class);
StaplerResponse mockresponse = PowerMockito.mock(StaplerResponse.class);
sodbaseaction.doPerformScan(mockrequest, mockresponse);
j.assertBuildStatus(Result.FAILURE, build);
j.createWebClient().getPage(project, "scan-on-demand/nonscanned/performScan");
ScanOnDemandQueue.shutdown();
assertNotNull(build.getAction(FailureCauseBuildAction.class));

Expand All @@ -88,21 +95,19 @@ public void testPerformScanFailedProject() throws Exception {
*
* @throws Exception if so.
*/
@Test
public void testPerformScanSuccessProject() throws Exception {
FreeStyleProject project = createFreeStyleProject();
FreeStyleProject project = j.createFreeStyleProject();
project.getBuildersList().add(new MockBuilder(Result.SUCCESS));
Future<FreeStyleBuild> future = project.scheduleBuild2(0);
FreeStyleBuild build = future.get(10, TimeUnit.SECONDS);
if (build.getAction(FailureCauseBuildAction.class) != null) {
build.getActions().remove(build.getAction(FailureCauseBuildAction.class));
}
assertNull(build.getAction(FailureCauseBuildAction.class));
assertBuildStatus(Result.SUCCESS, build);
ScanOnDemandBaseAction sodbaseaction = new ScanOnDemandBaseAction(project);
sodbaseaction.setBuildType("nonscanned");
StaplerRequest mockrequest = PowerMockito.mock(StaplerRequest.class);
StaplerResponse mockresponse = PowerMockito.mock(StaplerResponse.class);
sodbaseaction.doPerformScan(mockrequest, mockresponse);
j.assertBuildStatus(Result.SUCCESS, build);
j.createWebClient().getPage(project, "scan-on-demand/nonscanned/performScan");
ScanOnDemandQueue.shutdown();
assertNull(build.getAction(FailureCauseBuildAction.class));
}

Expand All @@ -112,11 +117,12 @@ public void testPerformScanSuccessProject() throws Exception {
* @throws Exception if problemos
* @see ScanOnDemandBaseAction#hasPermission()
*/
@Test
public void testShouldOnlyShowWhenHasPermission() throws Exception {
FreeStyleProject project = createFreeStyleProject();
String expectedHref = "/" + project.getUrl() + "scan-on-demand";
FreeStyleProject project = j.createFreeStyleProject();
String expectedHref = "/jenkins/" + project.getUrl() + "scan-on-demand";

SecurityRealm securityRealm = createDummySecurityRealm();
SecurityRealm securityRealm = j.createDummySecurityRealm();
Jenkins.getInstance().setSecurityRealm(securityRealm);
GlobalMatrixAuthorizationStrategy strategy = new GlobalMatrixAuthorizationStrategy();
strategy.add(Jenkins.READ, "anonymous");
Expand All @@ -128,7 +134,7 @@ public void testShouldOnlyShowWhenHasPermission() throws Exception {
Jenkins.getInstance().setAuthorizationStrategy(strategy);


WebClient client = createWebClient();
JenkinsRule.WebClient client = j.createWebClient();

HtmlPage page = client.login("alice").getPage(project);
try {
Expand All @@ -151,12 +157,13 @@ public void testShouldOnlyShowWhenHasPermission() throws Exception {
*
* @throws Exception if problemos
*/
@Test
public void testShouldOnlyShowWhenScanningIsEnabled() throws Exception {
FreeStyleProject project = createFreeStyleProject();
project = configRoundtrip(project);
String expectedHref = "/" + project.getUrl() + "scan-on-demand";
FreeStyleProject project = j.createFreeStyleProject();
project = j.configRoundtrip(project);
String expectedHref = "/jenkins/" + project.getUrl() + "scan-on-demand";

WebClient client = createWebClient();
JenkinsRule.WebClient client = j.createWebClient();

//Assert visible
HtmlPage page = client.getPage(project);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package com.sonyericsson.jenkins.plugins.bfa.sod;

import com.google.common.collect.Lists;
import com.sonyericsson.jenkins.plugins.bfa.PluginImpl;
import com.sonyericsson.jenkins.plugins.bfa.model.FoundFailureCause;
import hudson.model.AbstractBuild;
Expand Down Expand Up @@ -66,7 +67,6 @@
@PrepareForTest({Jenkins.class, PluginImpl.class, ScanOnDemandQueue.class, ScanOnDemandTask.class })
public class ScanOnDemandTaskTest {

private ScanOnDemandBaseAction sodbaseaction;
private AbstractProject mockproject;
private PluginImpl pluginMock;

Expand Down Expand Up @@ -99,8 +99,8 @@ public void testOneSODbuildfoundwithBuildFailure() throws Exception {
RunList<AbstractBuild> builds = new RunList<AbstractBuild>(Collections.<Job>emptyList());
Whitebox.setInternalState(builds, "base", Arrays.asList(mockbuild1, mockbuild2));
PowerMockito.when(mockproject.getBuilds()).thenReturn(builds);
sodbaseaction = new ScanOnDemandBaseAction(mockproject);
assertEquals("Nonscanned buils", 1, sodbaseaction.getNotScannedBuilds().size());
ScanOnDemandBaseAction.NonScanned action = new ScanOnDemandBaseAction.NonScanned();
assertEquals("Nonscanned buils", 1, Lists.newArrayList(action.getRuns(mockproject)).size());
}

/**
Expand All @@ -121,8 +121,8 @@ public void testTwoSODbuildfoundwithBuildFailure() throws Exception {
Whitebox.setInternalState(builds, "base", Arrays.asList(mockbuild1, mockbuild2));

PowerMockito.when(mockproject.getBuilds()).thenReturn(builds);
sodbaseaction = new ScanOnDemandBaseAction(mockproject);
assertEquals("Nonscanned buils", 2, sodbaseaction.getNotScannedBuilds().size());
ScanOnDemandBaseAction.NonScanned action = new ScanOnDemandBaseAction.NonScanned();
assertEquals("Nonscanned buils", 2, Lists.newArrayList(action.getRuns(mockproject)).size());
}

/**
Expand All @@ -141,8 +141,8 @@ public void testNoSODbuildfoundwithBuildSuccess() {
RunList<AbstractBuild> builds = new RunList<AbstractBuild>(Collections.<Job>emptyList());
Whitebox.setInternalState(builds, "base", Arrays.asList(mockbuild1, mockbuild2));
PowerMockito.when(mockproject.getBuilds()).thenReturn(builds);
sodbaseaction = new ScanOnDemandBaseAction(mockproject);
assertEquals("Nonscanned buils", 0, sodbaseaction.getNotScannedBuilds().size());
ScanOnDemandBaseAction.NonScanned action = new ScanOnDemandBaseAction.NonScanned();
assertEquals("Nonscanned buils", 0, Lists.newArrayList(action.getRuns(mockproject)).size());
}

/**
Expand All @@ -169,8 +169,8 @@ public void testNoSODbuildfoundwithBuildFailedButAlreadyScanned() throws Excepti

PowerMockito.when(mockbuild1.getActions(FailureCauseBuildAction.class)).thenReturn(failureCauseBuildActions);
PowerMockito.when(mockproject.getBuilds()).thenReturn(builds);
sodbaseaction = new ScanOnDemandBaseAction(mockproject);
assertEquals("Nonscanned buils", 0, sodbaseaction.getNotScannedBuilds().size());
ScanOnDemandBaseAction.NonScanned action = new ScanOnDemandBaseAction.NonScanned();
assertEquals("Nonscanned buils", 0, Lists.newArrayList(action.getRuns(mockproject)).size());
}

/**
Expand All @@ -192,8 +192,8 @@ public void testOneSODMatrixbuildfoundwithBuildFailure() throws Exception {
Whitebox.setInternalState(builds, "base", Arrays.asList(matrixbuild1, matrixbuild2));

PowerMockito.when(mockproject.getBuilds()).thenReturn(builds);
sodbaseaction = new ScanOnDemandBaseAction(mockproject);
assertEquals("Nonscanned buils", 1, sodbaseaction.getNotScannedBuilds().size());
ScanOnDemandBaseAction.NonScanned action = new ScanOnDemandBaseAction.NonScanned();
assertEquals("Nonscanned buils", 1, Lists.newArrayList(action.getRuns(mockproject)).size());
}

/**
Expand All @@ -215,8 +215,8 @@ public void testNoSODMatrixBuildfoundwithBuildSuccess() throws Exception {
Whitebox.setInternalState(builds, "base", Arrays.asList(matrixbuild1, matrixbuild2));

PowerMockito.when(mockproject.getBuilds()).thenReturn(builds);
sodbaseaction = new ScanOnDemandBaseAction(mockproject);
assertEquals("Nonscanned buils", 0, sodbaseaction.getNotScannedBuilds().size());
ScanOnDemandBaseAction.NonScanned action = new ScanOnDemandBaseAction.NonScanned();
assertEquals("Nonscanned buils", 0, Lists.newArrayList(action.getRuns(mockproject)).size());
}

/**
Expand All @@ -243,12 +243,12 @@ public void testNoSODmatrixbuildfoundwithBuildFailedButAlreadyScanned() throws E

PowerMockito.when(mockbuild1.getActions(FailureCauseBuildAction.class)).thenReturn(failureCauseBuildActions);
PowerMockito.when(mockproject.getBuilds()).thenReturn(builds);
sodbaseaction = new ScanOnDemandBaseAction(mockproject);
assertEquals("Nonscanned buils", 0, sodbaseaction.getNotScannedBuilds().size());
ScanOnDemandBaseAction.NonScanned action = new ScanOnDemandBaseAction.NonScanned();
assertEquals("Nonscanned buils", 0, Lists.newArrayList(action.getRuns(mockproject)).size());
}

/**
* Convenience method for a standard cause that finds {@link #TO_PRINT} in the build log.
* Convenience method for a standard cause that finds ERROR in the build log.
*
* @return the configured cause that was added to the global config.
* @throws Exception if something goes wrong in handling the causes.
Expand Down