Skip to content

Commit

Permalink
add more tests, a constant
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul-Hess committed Apr 23, 2019
1 parent d2e317b commit 60c9433
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import com.flightstats.hub.model.ChannelConfig;
import com.flightstats.hub.model.Content;
import com.flightstats.hub.model.ContentKey;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;

import javax.inject.Inject;
Expand Down Expand Up @@ -89,10 +89,10 @@ private CompletableFuture<String> waitForRead() {
return s3Object.getObjectMetadata().getVersionId();
} catch (IOException e) {
log.info("error closing connection to s3", e);
return "";
return StringUtils.EMPTY;
}
});
} catch(Exception e) {
} catch (Exception e) {
log.error("error getting object from s3", e);
throw e;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;

import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.junit.Assert.assertFalse;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

public class S3AccessMonitorTest {
Expand All @@ -39,7 +42,7 @@ public void setUpTest() {

@Test
public void testVerifyReadWriteAccess_errorPutObject_false() {
doThrow(new RuntimeException("public void testVerifyReadWriteAccess_errorPutObject_false"))
doThrow(new RuntimeException("testVerifyReadWriteAccess_errorPutObject_false"))
.when(s3Client).putObject(any(PutObjectRequest.class));
assertFalse(monitor.verifyReadWriteAccess());
}
Expand All @@ -60,4 +63,30 @@ public void testVerifyReadWriteAccess_mockVersionId_true() {
when(s3Client.putObject(any(PutObjectRequest.class))).thenReturn(putObjectResult);
assertTrue(monitor.verifyReadWriteAccess());
}

@Test
public void testVerifyReadWriteAccess_closeCalledGreenField_true() throws IOException {
S3Object mockS3Object = mock(S3Object.class);
when(s3Client.getObject(any(GetObjectRequest.class))).thenReturn(mockS3Object);
when(s3Client.putObject(any(PutObjectRequest.class))).thenReturn(putObjectResult);
monitor.verifyReadWriteAccess();
verify(mockS3Object).close();
}

@Test
public void testVerifyReadWriteAccess_closeCalledWithErrorOnGet_true() throws IOException {
S3Object mockS3Object = mock(S3Object.class);
when(s3Client.getObject(any(GetObjectRequest.class)))
.thenReturn(mockS3Object)
.thenThrow(new RuntimeException("testVerifyReadWriteAccess_closeCalledErrorOnGet_true"));
assertFalse(monitor.verifyReadWriteAccess());
verify(mockS3Object).close();
}

@Test
public void testVerifyReadWriteAccess_handlesNullSafely_false() throws IOException {
when(s3Client.getObject(any(GetObjectRequest.class)))
.thenReturn(null);
assertFalse(monitor.verifyReadWriteAccess());
}
}

0 comments on commit 60c9433

Please sign in to comment.