diff --git a/src/test/java/org/facil/practice/AbstractLambdaFileHandlerTest.java b/src/test/java/org/facil/practice/AbstractLambdaFileHandlerTest.java index 16104d4..b23c8b2 100644 --- a/src/test/java/org/facil/practice/AbstractLambdaFileHandlerTest.java +++ b/src/test/java/org/facil/practice/AbstractLambdaFileHandlerTest.java @@ -1,7 +1,16 @@ package org.facil.practice; +import com.amazonaws.services.lambda.AWSLambda; +import static org.mockito.Mockito.*; +import static org.junit.Assert.*; +import org.junit.Before; import org.junit.Test; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.IOException; +import java.io.PrintStream; + /** * User: blangel * Date: 12/31/15 @@ -9,9 +18,50 @@ */ public class AbstractLambdaFileHandlerTest { + //Use an inheritance class to test the abstract class + class AbstractClassImplement extends AbstractLambdaFileHandler{ + public AbstractClassImplement(String functionName, String fileName, File file, AWSLambda awsLambda){ + super(functionName, fileName, file, awsLambda); + } + } + @Test public void createFileIfNotExists() { // TODO + String functionName = "FunctionName"; + String fileName = "FileName"; + File file = mock(File.class); + AWSLambda awsLambda = mock(AWSLambda.class); + AbstractClassImplement abstractLambdaFileHandler = new AbstractClassImplement(functionName, fileName, file, awsLambda); + + + ByteArrayOutputStream outContent = new ByteArrayOutputStream(); + System.setOut(new PrintStream(outContent)); + String errorMessage = "^error^ Could not create file [ FileName ]^r^"; + + + when(file.exists()).thenReturn(true); + assertTrue(abstractLambdaFileHandler.createFileIfNotExists()); + when(file.exists()).thenReturn(false); + try { + when(file.createNewFile()).thenReturn(true); + }catch (IOException e){} + assertTrue(abstractLambdaFileHandler.createFileIfNotExists()); + + try { + when(file.createNewFile()).thenReturn(false); + }catch (IOException e){} + assertTrue(!abstractLambdaFileHandler.createFileIfNotExists()); + assertTrue(outContent.toString().indexOf(errorMessage) >= 0); + + try { + doThrow(new IOException("UnitTest")).when(file).createNewFile(); + }catch (IOException e){ + assertTrue(!abstractLambdaFileHandler.createFileIfNotExists()); + assertTrue(outContent.toString().indexOf(errorMessage) >= 0); + } + + } } diff --git a/src/test/java/org/facil/practice/AbstractLambdaHandlerTest.java b/src/test/java/org/facil/practice/AbstractLambdaHandlerTest.java index 24dcb65..2314d43 100644 --- a/src/test/java/org/facil/practice/AbstractLambdaHandlerTest.java +++ b/src/test/java/org/facil/practice/AbstractLambdaHandlerTest.java @@ -1,5 +1,16 @@ package org.facil.practice; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.*; +import static org.junit.Assert.*; + +import java.io.*; + +import com.amazonaws.AmazonClientException; +import com.amazonaws.services.lambda.AWSLambda; +import com.amazonaws.services.lambda.model.GetAliasResult; +import org.junit.Before; import org.junit.Test; /** @@ -9,24 +20,77 @@ */ public class AbstractLambdaHandlerTest { + //Use an inheritance class to test the abstract class + class AbstractClassImplement extends AbstractLambdaHandler{ + public AbstractClassImplement(String functionName, AWSLambda awsLambda){ + super(functionName, awsLambda); + } + } + @Test public void doesLambdaFunctionExist() { // TODO + AbstractLambdaHandler mockAbstractLambdaHandler = mock(AbstractLambdaHandler.class); + + when(mockAbstractLambdaHandler.doesLambdaFunctionExist()).thenCallRealMethod(); + doReturn(true).when(mockAbstractLambdaHandler).doesLambdaFunctionExist(true); + assertEquals(mockAbstractLambdaHandler.doesLambdaFunctionExist(), true); } @Test public void doesLambdaFunctionExistWithArgument() { // TODO + String functionName = "FunctionName"; + AWSLambda awsLambda = mock(AWSLambda.class); + AbstractClassImplement abstractClassImplement = new AbstractClassImplement(functionName, awsLambda); + + ByteArrayOutputStream outContent = new ByteArrayOutputStream(); + System.setOut(new PrintStream(outContent)); + String errorMessage = "^error^ Lambda function [ FunctionName ] does not exist^r^"; + + when(awsLambda.getFunction(any())).thenReturn(any()); + assertEquals(abstractClassImplement.doesLambdaFunctionExist(true), true); + + doThrow(new AmazonClientException("UnitTest")).when(awsLambda).getFunction(any()); + assertEquals(abstractClassImplement.doesLambdaFunctionExist(false), false); + + + assertEquals(abstractClassImplement.doesLambdaFunctionExist(true), false); + assertTrue(outContent.toString().indexOf(errorMessage) >= 0); } @Test public void getAliasVersion() { // TODO + String aliasType = "AliasType"; + AbstractLambdaHandler mockAbstractLambdaHandler = mock(AbstractLambdaHandler.class); + + when(mockAbstractLambdaHandler.getAliasVersion(aliasType)).thenCallRealMethod(); + doReturn(aliasType).when(mockAbstractLambdaHandler).getAliasVersion(aliasType, true); + assertEquals(mockAbstractLambdaHandler.getAliasVersion(aliasType), aliasType); } @Test public void getAliasVersionWithTwoArguments() { // TODO + String functionName = "FunctionName"; + AWSLambda awsLambda = mock(AWSLambda.class); + AbstractClassImplement abstractClassImplement = new AbstractClassImplement(functionName, awsLambda); + ByteArrayOutputStream outContent = new ByteArrayOutputStream(); + System.setOut(new PrintStream(outContent)); + String functionVersion = "FunctionVersion"; + String aliasType = "AliasType"; + String errorMessage = "^error^ Alias [ AliasType ] does not exist for Lambda function [ FunctionName ]^r^"; + + GetAliasResult getAliasResult = mock(GetAliasResult.class); + when(awsLambda.getAlias(any())).thenReturn(getAliasResult); + when(getAliasResult.getFunctionVersion()).thenReturn(functionVersion); + assertEquals(abstractClassImplement.getAliasVersion(aliasType, true), functionVersion); + + doThrow(new AmazonClientException("Exception")).when(awsLambda).getAlias(any()); + assertEquals(abstractClassImplement.getAliasVersion(aliasType, false), null); + assertEquals(abstractClassImplement.getAliasVersion(aliasType, true), null); + assertTrue(outContent.toString().indexOf(errorMessage) >= 0); } } diff --git a/src/test/java/org/facil/practice/DefaultFileHandlerTest.java b/src/test/java/org/facil/practice/DefaultFileHandlerTest.java index 5e68958..785db15 100644 --- a/src/test/java/org/facil/practice/DefaultFileHandlerTest.java +++ b/src/test/java/org/facil/practice/DefaultFileHandlerTest.java @@ -1,5 +1,11 @@ package org.facil.practice; +import static org.mockito.Mockito.*; +import static org.junit.Assert.*; + +import java.io.*; + +import org.junit.Before; import org.junit.Test; /** @@ -10,13 +16,38 @@ public class DefaultFileHandlerTest { @Test - public void saveFile() { + public void saveFile() throws Exception { // TODO + String version = "version"; + BufferedWriter mockBufferedWritter = mock(BufferedWriter.class); + BufferedReader mockBufferedReader = mock(BufferedReader.class); + DefaultFileHandler defaultFileHandlerTest = new DefaultFileHandler(mockBufferedWritter, mockBufferedReader); + + assertEquals(defaultFileHandlerTest.saveFile(null), false); + + doNothing().when(mockBufferedWritter).write(anyString()); + assertEquals(defaultFileHandlerTest.saveFile(version), true); + + doThrow(new IOException()).when(mockBufferedWritter).write(anyString()); + assertEquals(defaultFileHandlerTest.saveFile(version), false); + } @Test - public void readFile() { + public void readFile() throws Exception { // TODO + String version = "version"; + BufferedWriter mockBufferedWritter = mock(BufferedWriter.class); + BufferedReader mockBufferedReader = mock(BufferedReader.class); + DefaultFileHandler defaultFileHandlerTest = new DefaultFileHandler(mockBufferedWritter, mockBufferedReader); + + doReturn(version).when(mockBufferedReader).readLine(); + assertEquals(defaultFileHandlerTest.readFile(), version); + + doThrow(new IOException()).when(mockBufferedReader).readLine(); + assertEquals(defaultFileHandlerTest.readFile(), null); } + + } diff --git a/src/test/java/org/facil/practice/LambdaFunctionPublishTest.java b/src/test/java/org/facil/practice/LambdaFunctionPublishTest.java index 618b2c7..5641927 100644 --- a/src/test/java/org/facil/practice/LambdaFunctionPublishTest.java +++ b/src/test/java/org/facil/practice/LambdaFunctionPublishTest.java @@ -1,7 +1,18 @@ package org.facil.practice; +import com.amazonaws.AmazonClientException; +import com.amazonaws.services.lambda.AWSLambda; +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.*; +import static org.junit.Assert.*; + +import com.amazonaws.services.lambda.model.GetFunctionResult; +import com.amazonaws.services.lambda.model.PublishVersionResult; +import org.junit.Before; import org.junit.Test; +import java.io.*; + /** * User: blangel * Date: 12/31/15 @@ -12,11 +23,64 @@ public class LambdaFunctionPublishTest { @Test public void publishVersion() { // TODO + String functionName = "FunctionName"; + String versionDescription = "VersionDescription"; + String fileName = "FileName"; + AWSLambda awsLambda = mock(AWSLambda.class, RETURNS_DEEP_STUBS); + File file = mock(File.class); + FileHandler fileHandler = mock(FileHandler.class); + LambdaFunctionPublish lambdaFunctionPublish = new LambdaFunctionPublish(functionName, versionDescription, awsLambda, fileName, file, fileHandler); + + when(file.exists()).thenReturn(false); + assertTrue(!lambdaFunctionPublish.publishNewVersion()); + + when(file.exists()).thenReturn(true); + doThrow(new AmazonClientException("UnitTest")).when(awsLambda).getFunction(any()); + assertTrue(!lambdaFunctionPublish.publishNewVersion()); + + doReturn(new GetFunctionResult()).when(awsLambda).getFunction(any()); + when(awsLambda.publishVersion(any())).thenReturn(new PublishVersionResult()); + when(fileHandler.saveFile(any())).thenReturn(true); + assertTrue(lambdaFunctionPublish.publishNewVersion()); + + doReturn(new GetFunctionResult()).when(awsLambda).getFunction(any()); + when(awsLambda.publishVersion(any())).thenReturn(new PublishVersionResult()); + when(fileHandler.saveFile(any())).thenReturn(false); + assertTrue(!lambdaFunctionPublish.publishNewVersion()); + + doReturn(new GetFunctionResult()).when(awsLambda).getFunction(any()); + when(awsLambda.publishVersion(any())).thenReturn(null); + when(fileHandler.saveFile(any())).thenReturn(true); + assertTrue(!lambdaFunctionPublish.publishNewVersion()); + + } @Test public void publishNewVersion() { // TODO + String functionName = "FunctionName"; + String versionDescription = "VersionDescription"; + String fileName = "FileName"; + AWSLambda awsLambda = mock(AWSLambda.class, RETURNS_DEEP_STUBS); + File file = mock(File.class); + FileHandler fileHandler = mock(FileHandler.class); + LambdaFunctionPublish lambdaFunctionPublish = new LambdaFunctionPublish(functionName, versionDescription, awsLambda, fileName, file, fileHandler); + + ByteArrayOutputStream outContent = new ByteArrayOutputStream(); + System.setOut(new PrintStream(outContent)); + String errorMessage = "^error^ Lambda function [ FunctionName ] does not exist^r^"; + + when(file.exists()).thenReturn(true); + when(fileHandler.saveFile(any())).thenReturn(true); + doReturn(new GetFunctionResult()).when(awsLambda).getFunction(any()); + + when(awsLambda.publishVersion(any())).thenReturn(new PublishVersionResult()); + assertTrue(lambdaFunctionPublish.publishNewVersion()); + + doThrow(new AmazonClientException("UnitTest")).when(awsLambda).publishVersion(any()); + assertTrue(!lambdaFunctionPublish.publishNewVersion()); + assertTrue(outContent.toString().indexOf(errorMessage) >= 0); } }