From 0c1916268c861f5e784b4223b1ba579629207eaf Mon Sep 17 00:00:00 2001 From: Sheng Chen Date: Wed, 13 Dec 2017 11:56:38 +0800 Subject: [PATCH 1/3] add the selection for HttpTrigger's authLevel property --- .../azure/maven/function/AddMojo.java | 84 ++++++++++++++++--- .../src/main/resources/templates.json | 6 +- .../azure/maven/function/AddMojoTest.java | 23 ++--- 3 files changed, 83 insertions(+), 30 deletions(-) diff --git a/azure-functions-maven-plugin/src/main/java/com/microsoft/azure/maven/function/AddMojo.java b/azure-functions-maven-plugin/src/main/java/com/microsoft/azure/maven/function/AddMojo.java index d6e940e59b..3f8c50749d 100644 --- a/azure-functions-maven-plugin/src/main/java/com/microsoft/azure/maven/function/AddMojo.java +++ b/azure-functions-maven-plugin/src/main/java/com/microsoft/azure/maven/function/AddMojo.java @@ -31,6 +31,8 @@ import static org.codehaus.plexus.util.IOUtil.copy; import static org.codehaus.plexus.util.StringUtils.isNotEmpty; +import javax.annotation.Nullable; + /** * Create new Azure Function (as Java class) and add to current project. */ @@ -274,18 +276,38 @@ protected Map prepareTemplateParameters(final FunctionTemplate t for (final String property : template.getMetadata().getUserPrompt()) { info(format("Trigger specific parameter [%s]", property)); + final List options = OptionTypedProperty.get(property) == null ? + null : getOptionsForProperty(property); if (settings != null && !settings.isInteractiveMode()) { - assureInputInBatchMode(System.getProperty(property), - str -> isNotEmpty(str), + String initValue = System.getProperty(property); + if (options != null && options.size() > 0) { + final String foundElement = findElementInOptions(options, initValue); + initValue = foundElement == null ? options.get(0) : foundElement; + } + + assureInputInBatchMode( + initValue, + StringUtils::isNotEmpty, str -> params.put(property, str), - false); + false + ); } else { - assureInputFromUser(format("Enter value for %s: ", property), - System.getProperty(property), - str -> isNotEmpty(str), - "Input should be a non-empty string.", - str -> params.put(property, str)); - + if (options == null) { + assureInputFromUser( + format("Enter value for %s: ", property), + System.getProperty(property), + StringUtils::isNotEmpty, + "Input should be a non-empty string.", + str -> params.put(property, str) + ); + } else { + assureInputFromUser( + format("Enter value for %s: ", property), + System.getProperty(property), + options, + str -> params.put(property, str) + ); + } } } @@ -365,8 +387,10 @@ protected void saveToTargetFile(final File targetFile, final String newFunctionC protected void assureInputFromUser(final String prompt, final String initValue, final List options, final Consumer setter) { - if (options.stream().filter(Objects::nonNull).anyMatch(o -> o.equalsIgnoreCase(initValue))) { + final String option = findElementInOptions(options, initValue); + if (option != null) { info(FOUND_VALID_VALUE); + setter.accept(option); return; } @@ -440,5 +464,45 @@ protected Scanner getScanner() { return new Scanner(System.in, "UTF-8"); } + @Nullable + private String findElementInOptions(List options, String item) { + return options.stream() + .filter(o -> o != null && o.equalsIgnoreCase(item)) + .findFirst() + .orElse(null); + } + + @Nullable + private List getOptionsForProperty(final String property) { + final OptionTypedProperty optionTypedProperty = OptionTypedProperty.get(property); + if (optionTypedProperty != null) { + switch (optionTypedProperty) { + case AUTH_LEVEL: + return Arrays.asList("ANONYMOUS", "FUNCTION", "ADMIN"); + default: + return null; + } + } + return null; + } + + private enum OptionTypedProperty { + AUTH_LEVEL("authLevel"); + + private final String property; + + OptionTypedProperty(String property) { + this.property = property; + } + + @Nullable + public static OptionTypedProperty get(final String property) { + return Arrays.stream(values()) + .filter(prop -> prop.property.equals(property)) + .findFirst() + .orElse(null); + } + } + //endregion } diff --git a/azure-functions-maven-plugin/src/main/resources/templates.json b/azure-functions-maven-plugin/src/main/resources/templates.json index 84a5bbcc36..330a570b02 100644 --- a/azure-functions-maven-plugin/src/main/resources/templates.json +++ b/azure-functions-maven-plugin/src/main/resources/templates.json @@ -6,10 +6,12 @@ "description": "$HttpTriggerJava_description", "defaultFunctionName": "httpTriggerJava", "language": "Java", - "userPrompt": [] + "userPrompt": [ + "authLevel" + ] }, "files": { - "function.java": "package $packageName$;\n\nimport java.util.*;\nimport com.microsoft.azure.serverless.functions.annotation.*;\nimport com.microsoft.azure.serverless.functions.*;\n\n/**\n * Azure Functions with HTTP trigger.\n */\npublic class $className$ {\n /**\n * This function will listen at HTTP endpoint \"/api/$functionName$\". Two approaches to invoke it using \"curl\" command in bash:\n * 1. curl -d \"Http Body\" {your host}/api/$functionName$\n * 2. curl {your host}/api/$functionName$?name=HTTP%20Query\n */\n @FunctionName(\"$functionName$\")\n public HttpResponseMessage httpHandler(\n @HttpTrigger(name = \"req\", methods = { \"get\", \"post\" }, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage> request,\n final ExecutionContext context\n ) {\n context.getLogger().info(\"Java HTTP trigger processed a request.\");\n\n // Parse query parameters\n String query = request.getQueryParameters().get(\"name\");\n String name = request.getBody().orElse(query);\n\n if (name == null) {\n return request.createResponse(400, \"Please pass a name on the query string or in the request body\");\n } else {\n return request.createResponse(200, \"Hello, \" + name);\n }\n }\n}\n" + "function.java": "package $packageName$;\n\nimport java.util.*;\nimport com.microsoft.azure.serverless.functions.annotation.*;\nimport com.microsoft.azure.serverless.functions.*;\n\n/**\n * Azure Functions with HTTP trigger.\n */\npublic class $className$ {\n /**\n * This function will listen at HTTP endpoint \"/api/$functionName$\". Two approaches to invoke it using \"curl\" command in bash:\n * 1. curl -d \"Http Body\" {your host}/api/$functionName$\n * 2. curl {your host}/api/$functionName$?name=HTTP%20Query\n */\n @FunctionName(\"$functionName$\")\n public HttpResponseMessage httpHandler(\n @HttpTrigger(name = \"req\", methods = { \"get\", \"post\" }, authLevel = AuthorizationLevel.$authLevel$) HttpRequestMessage> request,\n final ExecutionContext context\n ) {\n context.getLogger().info(\"Java HTTP trigger processed a request.\");\n\n // Parse query parameters\n String query = request.getQueryParameters().get(\"name\");\n String name = request.getBody().orElse(query);\n\n if (name == null) {\n return request.createResponse(400, \"Please pass a name on the query string or in the request body\");\n } else {\n return request.createResponse(200, \"Hello, \" + name);\n }\n }\n}\n" } }, { diff --git a/azure-functions-maven-plugin/src/test/java/com/microsoft/azure/maven/function/AddMojoTest.java b/azure-functions-maven-plugin/src/test/java/com/microsoft/azure/maven/function/AddMojoTest.java index 5e7abb9c29..6fd4103557 100644 --- a/azure-functions-maven-plugin/src/test/java/com/microsoft/azure/maven/function/AddMojoTest.java +++ b/azure-functions-maven-plugin/src/test/java/com/microsoft/azure/maven/function/AddMojoTest.java @@ -40,12 +40,15 @@ public void getConfiguration() throws Exception { @Test public void doExecute() throws Exception { final AddMojo mojo = getMojoFromPom(); + final Settings settings = new Settings(); + settings.setInteractiveMode(false); ReflectionUtils.setVariableValueInObject(mojo, "basedir", new File("target/test")); + ReflectionUtils.setVariableValueInObject(mojo, "settings", settings); mojo.setFunctionTemplate("HttpTrigger"); - mojo.setFunctionName("NewFunction"); + mojo.setFunctionName("New-Function"); mojo.setFunctionPackageName("com.microsoft.azure"); - final File newFunctionFile = new File("target/test/src/main/java/com/microsoft/azure/NewFunction.java"); + final File newFunctionFile = new File("target/test/src/main/java/com/microsoft/azure/New_Function.java"); newFunctionFile.delete(); mojo.doExecute(); @@ -67,22 +70,6 @@ public void doExecuteWithInvalidFunctionName() throws Exception { mojo.doExecute(); } - @Test - public void doExecuteWithSpecialFunctionName() throws Exception { - final AddMojo mojo = getMojoFromPom(); - ReflectionUtils.setVariableValueInObject(mojo, "basedir", new File("target/test")); - mojo.setFunctionTemplate("HttpTrigger"); - mojo.setFunctionName("New-Function"); - mojo.setFunctionPackageName("com.microsoft.azure"); - - final File newFunctionFile = new File("target/test/src/main/java/com/microsoft/azure/New_Function.java"); - newFunctionFile.delete(); - - mojo.doExecute(); - - assertTrue(newFunctionFile.exists()); - } - @Test public void assureInputFromUser() throws Exception { final AddMojo mojo = getMojoFromPom(); From 72c476561a471224b3bcf9af4992cf962e6d0be5 Mon Sep 17 00:00:00 2001 From: Sheng Chen Date: Wed, 13 Dec 2017 13:47:13 +0800 Subject: [PATCH 2/3] remove the enum which is redundant --- .../azure/maven/function/AddMojo.java | 37 ++++--------------- 1 file changed, 7 insertions(+), 30 deletions(-) diff --git a/azure-functions-maven-plugin/src/main/java/com/microsoft/azure/maven/function/AddMojo.java b/azure-functions-maven-plugin/src/main/java/com/microsoft/azure/maven/function/AddMojo.java index 3f8c50749d..5c00e0b8b8 100644 --- a/azure-functions-maven-plugin/src/main/java/com/microsoft/azure/maven/function/AddMojo.java +++ b/azure-functions-maven-plugin/src/main/java/com/microsoft/azure/maven/function/AddMojo.java @@ -276,8 +276,7 @@ protected Map prepareTemplateParameters(final FunctionTemplate t for (final String property : template.getMetadata().getUserPrompt()) { info(format("Trigger specific parameter [%s]", property)); - final List options = OptionTypedProperty.get(property) == null ? - null : getOptionsForProperty(property); + final List options = getOptionsForUserPrompt(property); if (settings != null && !settings.isInteractiveMode()) { String initValue = System.getProperty(property); if (options != null && options.size() > 0) { @@ -473,34 +472,12 @@ private String findElementInOptions(List options, String item) { } @Nullable - private List getOptionsForProperty(final String property) { - final OptionTypedProperty optionTypedProperty = OptionTypedProperty.get(property); - if (optionTypedProperty != null) { - switch (optionTypedProperty) { - case AUTH_LEVEL: - return Arrays.asList("ANONYMOUS", "FUNCTION", "ADMIN"); - default: - return null; - } - } - return null; - } - - private enum OptionTypedProperty { - AUTH_LEVEL("authLevel"); - - private final String property; - - OptionTypedProperty(String property) { - this.property = property; - } - - @Nullable - public static OptionTypedProperty get(final String property) { - return Arrays.stream(values()) - .filter(prop -> prop.property.equals(property)) - .findFirst() - .orElse(null); + private List getOptionsForUserPrompt(final String promptName) { + switch (promptName.trim().toLowerCase(Locale.getDefault())) { + case "authlevel": + return Arrays.asList("ANONYMOUS", "FUNCTION", "ADMIN"); + default: + return null; } } From b772644c8e4d488bb5cf27542da3cc616a6c812e Mon Sep 17 00:00:00 2001 From: Sheng Chen Date: Fri, 15 Dec 2017 10:01:06 +0800 Subject: [PATCH 3/3] change switch to equalignorecase, which is more safer --- .../java/com/microsoft/azure/maven/function/AddMojo.java | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/azure-functions-maven-plugin/src/main/java/com/microsoft/azure/maven/function/AddMojo.java b/azure-functions-maven-plugin/src/main/java/com/microsoft/azure/maven/function/AddMojo.java index 5c00e0b8b8..07571e54c1 100644 --- a/azure-functions-maven-plugin/src/main/java/com/microsoft/azure/maven/function/AddMojo.java +++ b/azure-functions-maven-plugin/src/main/java/com/microsoft/azure/maven/function/AddMojo.java @@ -473,12 +473,10 @@ private String findElementInOptions(List options, String item) { @Nullable private List getOptionsForUserPrompt(final String promptName) { - switch (promptName.trim().toLowerCase(Locale.getDefault())) { - case "authlevel": - return Arrays.asList("ANONYMOUS", "FUNCTION", "ADMIN"); - default: - return null; + if ("authlevel".equalsIgnoreCase(promptName.trim())) { + return Arrays.asList("ANONYMOUS", "FUNCTION", "ADMIN"); } + return null; } //endregion