Skip to content
Open
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
3 changes: 2 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
<!-- TODO some violations remaining -->
<spotbugs.threshold>High</spotbugs.threshold>
<spotless.check.skip>false</spotless.check.skip>
<ban-junit4-imports.skip>false</ban-junit4-imports.skip>
</properties>

<dependencyManagement>
Expand Down Expand Up @@ -258,7 +259,7 @@
<!-- Test dependencies -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<artifactId>mockito-junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@
package com.google.jenkins.plugins.storage;

import static com.google.common.base.Preconditions.checkNotNull;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.isA;
import static org.mockito.Mockito.when;

Expand All @@ -34,27 +35,34 @@
import com.google.jenkins.plugins.util.ForbiddenException;
import com.google.jenkins.plugins.util.MockExecutor;
import com.google.jenkins.plugins.util.NotFoundException;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
import hudson.Extension;
import hudson.FilePath;
import hudson.model.FreeStyleBuild;
import hudson.model.FreeStyleProject;
import hudson.model.TaskListener;
import hudson.util.FormValidation;
import java.io.IOException;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.Verifier;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.WithoutJenkins;
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;

/** Tests for {@link AbstractBucketLifecycleManager}. */
public class AbstractBucketLifecycleManagerTest {
@WithJenkins
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
class AbstractBucketLifecycleManagerTest {

@Rule
public JenkinsRule jenkins = new JenkinsRule();
private JenkinsRule jenkins;

@Mock
private GoogleRobotCredentials credentials;
Expand All @@ -67,24 +75,18 @@ public class AbstractBucketLifecycleManagerTest {
private NotFoundException notFoundException;

private Predicate<Storage.Buckets.Insert> checkBucketName(final String bucketName) {
return new Predicate<Storage.Buckets.Insert>() {
@Override
public boolean apply(Storage.Buckets.Insert operation) {
Bucket bucket = (Bucket) operation.getJsonContent();
assertEquals(bucketName, bucket.getName());
return true;
}
return operation -> {
Bucket bucket = (Bucket) operation.getJsonContent();
assertEquals(bucketName, bucket.getName());
return true;
};
}

private Predicate<Storage.Buckets.Update> checkSameBucket(final Bucket theBucket) {
return new Predicate<Storage.Buckets.Update>() {
@Override
public boolean apply(Storage.Buckets.Update operation) {
Bucket bucket = (Bucket) operation.getJsonContent();
assertSame(bucket, theBucket);
return true;
}
return operation -> {
Bucket bucket = (Bucket) operation.getJsonContent();
assertSame(bucket, theBucket);
return true;
};
}

Expand Down Expand Up @@ -113,15 +115,6 @@ public MockExecutor newExecutor() {
private final int retryCount;
}

@Rule
public Verifier verifySawAll = new Verifier() {
@Override
public void verify() {
assertTrue(executor.sawAll());
assertFalse(executor.sawUnexpected());
}
};

private static class FakeUpload extends AbstractBucketLifecycleManager {

public FakeUpload(String bucketName, MockUploadModule module, String details, @Nullable Bucket bucket) {
Expand Down Expand Up @@ -161,6 +154,8 @@ public DescriptorImpl() {
super(FakeUpload.class);
}

@NonNull
@Override
public String getDisplayName() {
return "asdf";
}
Expand All @@ -170,9 +165,9 @@ public String getDisplayName() {
private FreeStyleProject project;
private FreeStyleBuild build;

@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
@BeforeEach
void beforeEach(JenkinsRule rule) throws Exception {
jenkins = rule;

when(credentials.getId()).thenReturn(CREDENTIALS_ID);
when(credentials.getProjectId()).thenReturn(PROJECT_ID);
Expand Down Expand Up @@ -200,9 +195,15 @@ public void setUp() throws Exception {
forbiddenException = new ForbiddenException();
}

@AfterEach
void afterEach() {
assertTrue(executor.sawAll());
assertFalse(executor.sawUnexpected());
}

@Test
@WithoutJenkins
public void testGetters() {
void testGetters() {
FakeUpload underTest =
new FakeUpload(BUCKET_URI, new MockUploadModule(executor), FAKE_DETAILS, null /* bucket */);

Expand All @@ -211,7 +212,7 @@ public void testGetters() {
}

@Test
public void testFailingBucketCheck() throws Exception {
void testFailingBucketCheck() throws Exception {
final Bucket bucket = new Bucket().setName(BUCKET_NAME);

FakeUpload underTest = new FakeUpload(BUCKET_URI, new MockUploadModule(executor), FAKE_DETAILS, bucket);
Expand All @@ -224,7 +225,7 @@ public void testFailingBucketCheck() throws Exception {
}

@Test
public void testPassingBucketCheck() throws Exception {
void testPassingBucketCheck() throws Exception {
final Bucket bucket = new Bucket().setName(BUCKET_NAME);

FakeUpload underTest = new FakeUpload(
Expand All @@ -237,7 +238,7 @@ public void testPassingBucketCheck() throws Exception {
}

@Test
public void testPassingBucketCheckAfterNotFoundThenConflict() throws Exception {
void testPassingBucketCheckAfterNotFoundThenConflict() throws Exception {
final Bucket bucket = new Bucket().setName(BUCKET_NAME);

FakeUpload underTest = new FakeUpload(BUCKET_URI, new MockUploadModule(executor), FAKE_DETAILS, bucket);
Expand All @@ -251,28 +252,28 @@ public void testPassingBucketCheckAfterNotFoundThenConflict() throws Exception {
underTest.perform(CREDENTIALS_ID, build, build.getWorkspace(), TaskListener.NULL);
}

@Test(expected = UploadException.class)
public void testRandomErrorExecutor() throws Exception {
@Test
void testRandomErrorExecutor() {
FakeUpload underTest = new FakeUpload(
BUCKET_URI, new MockUploadModule(executor), FAKE_DETAILS, null /* pass the bucket check */);

executor.throwWhen(Storage.Buckets.Get.class, conflictException);

underTest.perform(CREDENTIALS_ID, build, build.getWorkspace(), TaskListener.NULL);
FilePath workspace = build.getWorkspace();
TaskListener x = TaskListener.NULL;
assertThrows(UploadException.class, () -> underTest.perform(CREDENTIALS_ID, build, workspace, x));
}

@Test(expected = UploadException.class)
public void testRandomErrorIOException() throws Exception {
@Test
void testRandomErrorIOException() {
FakeUpload underTest = new FakeUpload(
BUCKET_URI, new MockUploadModule(executor), FAKE_DETAILS, null /* pass the bucket check */);

executor.throwWhen(Storage.Buckets.Get.class, new IOException("test"));

underTest.perform(CREDENTIALS_ID, build, build.getWorkspace(), TaskListener.NULL);
FilePath workspace = build.getWorkspace();
TaskListener x = TaskListener.NULL;
assertThrows(UploadException.class, () -> underTest.perform(CREDENTIALS_ID, build, workspace, x));
}

@Test
public void testCustomBucketNameValidation() throws Exception {
void testCustomBucketNameValidation() throws Exception {
FakeUpload underTest = new FakeUpload(
BUCKET_URI, new MockUploadModule(executor), FAKE_DETAILS, null /* pass the bucket check */);

Expand Down
Loading
Loading