diff --git a/build.gradle b/build.gradle index f5707c3..2e3d64a 100644 --- a/build.gradle +++ b/build.gradle @@ -11,6 +11,7 @@ subprojects { } dependencies { + testCompile 'junit:junit:4.12' } } \ No newline at end of file diff --git a/left-pad-lambda/build.gradle b/left-pad-lambda/build.gradle new file mode 100644 index 0000000..5008611 --- /dev/null +++ b/left-pad-lambda/build.gradle @@ -0,0 +1,3 @@ +dependencies { + compile 'com.amazonaws:aws-lambda-java-core:1.2.0' +} \ No newline at end of file diff --git a/left-pad-lambda/src/main/java/pl/grondek/aws/lambda/LeftPadFunction.java b/left-pad-lambda/src/main/java/pl/grondek/aws/lambda/LeftPadFunction.java new file mode 100644 index 0000000..32c629f --- /dev/null +++ b/left-pad-lambda/src/main/java/pl/grondek/aws/lambda/LeftPadFunction.java @@ -0,0 +1,24 @@ +package pl.grondek.aws.lambda; + +import com.amazonaws.services.lambda.runtime.Context; +import com.amazonaws.services.lambda.runtime.RequestHandler; + +public class LeftPadFunction implements RequestHandler { + @Override + public String handleRequest(PadRequest request, Context context) { + if (request.getSize() < request.getString().length()) { + return request.getString(); + } else { + final char[] input = request.getString().toCharArray(); + char[] output = new char[request.getSize()]; + final int paddingSize = request.getSize() - input.length; + + for (int i = 0; i < paddingSize; i++) { + output[i] = request.getCharacter(); + } + + System.arraycopy(input, 0, output, paddingSize, input.length); + return String.valueOf(output); + } + } +} diff --git a/left-pad-lambda/src/main/java/pl/grondek/aws/lambda/PadRequest.java b/left-pad-lambda/src/main/java/pl/grondek/aws/lambda/PadRequest.java new file mode 100644 index 0000000..74aaa9a --- /dev/null +++ b/left-pad-lambda/src/main/java/pl/grondek/aws/lambda/PadRequest.java @@ -0,0 +1,31 @@ +package pl.grondek.aws.lambda; + +public class PadRequest { + private String string; + private int size; + private char character; + + public String getString() { + return string; + } + + public void setString(String string) { + this.string = string; + } + + public int getSize() { + return size; + } + + public void setSize(int size) { + this.size = size; + } + + public char getCharacter() { + return character; + } + + public void setCharacter(char character) { + this.character = character; + } +} diff --git a/left-pad-lambda/src/test/java/pl/grondek/aws/lambda/LeftPadFunctionTest.java b/left-pad-lambda/src/test/java/pl/grondek/aws/lambda/LeftPadFunctionTest.java new file mode 100644 index 0000000..233269f --- /dev/null +++ b/left-pad-lambda/src/test/java/pl/grondek/aws/lambda/LeftPadFunctionTest.java @@ -0,0 +1,26 @@ +package pl.grondek.aws.lambda; + +import org.junit.Assert; +import org.junit.Test; + +public class LeftPadFunctionTest { + + @Test + public void testFunction() { + // setup + LeftPadFunction leftPadFunction = new LeftPadFunction(); + + // given + PadRequest request = new PadRequest(); + request.setCharacter(' '); + request.setSize(10); + request.setString("Test"); + + // when + final String response = leftPadFunction.handleRequest(request, null); + + // then + Assert.assertEquals(response.length(), 10); + Assert.assertEquals(response, " Test"); + } +} \ No newline at end of file diff --git a/settings.gradle b/settings.gradle index 53fbc67..31211bb 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,2 +1,3 @@ rootProject.name = 'aws' +include "left-pad-lambda" \ No newline at end of file