Skip to content

Commit

Permalink
add batch mode for add function (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdneo authored and xscript committed Nov 16, 2017
1 parent d221bd9 commit 3f3d191
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 29 deletions.
4 changes: 2 additions & 2 deletions azure-functions-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.microsoft.azure</groupId>
<artifactId>azure-functions-maven-plugin</artifactId>
<version>0.1.6-SNAPSHOT</version>
<version>0.1.7-SNAPSHOT</version>
<packaging>maven-plugin</packaging>
<name>Maven Plugin for Azure Functions</name>
<description>Maven Plugin for Azure Functions</description>
Expand All @@ -24,7 +24,7 @@
<codehaus.plexus-utils.version>3.0.20</codehaus.plexus-utils.version>
<azure.ai.version>1.0.9</azure.ai.version>
<azure.maven-plugin-lib.version>0.1.6-SNAPSHOT</azure.maven-plugin-lib.version>
<azure.function.version>1.0.0-beta-1</azure.function.version>
<azure.function.version>[1.0.0-beta-1,1.0.0)</azure.function.version>
<azure.storage.version>5.4.0</azure.storage.version>
<reflections.version>0.9.11</reflections.version>
<zeroturnaround.zip.version>1.12</zeroturnaround.zip.version>
Expand Down
2 changes: 1 addition & 1 deletion azure-functions-maven-plugin/src/it/http-trigger/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-functions-java-core</artifactId>
<version>1.0.0-beta-1</version>
<version>[1.0.0-beta-1,1.0.0)</version>
</dependency>

<!-- Test -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public HttpResponseMessage httpHandler(
context.getLogger().info("Java HTTP trigger processed a HTTP request.");

// Parse query parameter
String name = request.getQueryParameters().get("name");
String name = request.getQueryParameters().get("name").toString();

if (name == null) {
// Get request body
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import com.fasterxml.jackson.databind.ObjectMapper;
import com.microsoft.azure.maven.function.template.FunctionTemplate;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.codehaus.plexus.util.IOUtil;
Expand Down Expand Up @@ -166,10 +167,20 @@ protected FunctionTemplate getFunctionTemplate(final List<FunctionTemplate> temp
info("");
info(FIND_TEMPLATE);

assureInputFromUser("template for new function",
getFunctionTemplate(),
getTemplateNames(templates),
this::setFunctionTemplate);
if (settings != null && !settings.isInteractiveMode()) {
assureInputInBatchMode(getFunctionTemplate(),
str -> getTemplateNames(templates)
.stream()
.filter(Objects::nonNull)
.anyMatch(o -> o.equalsIgnoreCase(str)),
this::setFunctionTemplate,
true);
} else {
assureInputFromUser("template for new function",
getFunctionTemplate(),
getTemplateNames(templates),
this::setFunctionTemplate);
}

return findTemplateByName(templates, getFunctionTemplate());
}
Expand Down Expand Up @@ -197,7 +208,8 @@ protected FunctionTemplate findTemplateByName(final List<FunctionTemplate> templ

//region Prepare parameters

protected Map<String, String> prepareRequiredParameters(final FunctionTemplate template) {
protected Map<String, String> prepareRequiredParameters(final FunctionTemplate template)
throws MojoFailureException {
info("");
info(PREPARE_PARAMS);

Expand All @@ -216,36 +228,59 @@ protected Map<String, String> prepareRequiredParameters(final FunctionTemplate t
return params;
}

protected void prepareFunctionName() {
protected void prepareFunctionName() throws MojoFailureException {
info("Common parameter [Function Name]: name for both the new function and Java class");

assureInputFromUser("Enter value for Function Name: ",
getFunctionName(),
str -> isNotEmpty(str) && isIdentifier(str) && !isKeyword(str),
"Input should be a valid Java class name.",
this::setFunctionName);
if (settings != null && !settings.isInteractiveMode()) {
assureInputInBatchMode(getFunctionName(),
str -> isNotEmpty(str) && isIdentifier(str) && !isKeyword(str),
this::setFunctionName,
true);
} else {
assureInputFromUser("Enter value for Function Name: ",
getFunctionName(),
str -> isNotEmpty(str) && isIdentifier(str) && !isKeyword(str),
"Input should be a valid Java class name.",
this::setFunctionName);
}
}

protected void preparePackageName() {
protected void preparePackageName() throws MojoFailureException {
info("Common parameter [Package Name]: package name of the new Java class");

assureInputFromUser("Enter value for Package Name: ",
getFunctionPackageName(),
str -> isNotEmpty(str) && isName(str),
"Input should be a valid Java package name.",
this::setFunctionPackageName);
if (settings != null && !settings.isInteractiveMode()) {
assureInputInBatchMode(getFunctionPackageName(),
str -> isNotEmpty(str) && isName(str),
this::setFunctionPackageName,
true);
} else {
assureInputFromUser("Enter value for Package Name: ",
getFunctionPackageName(),
str -> isNotEmpty(str) && isName(str),
"Input should be a valid Java package name.",
this::setFunctionPackageName);
}
}

protected Map<String, String> prepareTemplateParameters(final FunctionTemplate template,
final Map<String, String> params) {
final Map<String, String> params)
throws MojoFailureException {
for (final String property : template.getMetadata().getUserPrompt()) {
info(format("Trigger specific parameter [%s]", property));

assureInputFromUser(format("Enter value for %s: ", property),
null,
str -> isNotEmpty(str),
"Input should be a non-empty string.",
str -> params.put(property, str));
if (settings != null && !settings.isInteractiveMode()) {
assureInputInBatchMode(System.getProperty(property),
str -> isNotEmpty(str),
str -> params.put(property, str),
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));

}
}

return params;
Expand Down Expand Up @@ -324,7 +359,7 @@ protected void saveToTargetFile(final File targetFile, final String newFunctionC

protected void assureInputFromUser(final String prompt, final String initValue, final List<String> options,
final Consumer<String> setter) {
if (options.stream().anyMatch(o -> o.equalsIgnoreCase(initValue))) {
if (options.stream().filter(Objects::nonNull).anyMatch(o -> o.equalsIgnoreCase(initValue))) {
info(FOUND_VALID_VALUE);
return;
}
Expand Down Expand Up @@ -378,6 +413,23 @@ protected void assureInputFromUser(final String prompt, final String initValue,
}
}

protected void assureInputInBatchMode(final String input, final Function<String, Boolean> validator,
final Consumer<String> setter, final boolean required)
throws MojoFailureException {
if (validator.apply(input)) {
info(FOUND_VALID_VALUE);
setter.accept(input);
return;
}

if (required) {
throw new MojoFailureException(String.format("invalid input: %s", input));
} else {
out.printf("The input is invalid. Use empty string.%n");
setter.accept("");
}
}

protected Scanner getScanner() {
return new Scanner(System.in, "UTF-8");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@

package com.microsoft.azure.maven.function;

import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.settings.Settings;
import org.codehaus.plexus.util.ReflectionUtils;
import org.codehaus.plexus.util.StringUtils;
import org.junit.Test;

import java.io.File;
Expand Down Expand Up @@ -59,11 +62,31 @@ public void assureInputFromUser() throws Exception {
doReturn(scanner).when(mojoSpy).getScanner();

final Set<String> set = new HashSet<>();
mojoSpy.assureInputFromUser("property", "", Arrays.asList("a0", "a1", "a2"), str -> set.add(str));
mojoSpy.assureInputFromUser("property", "", Arrays.asList("a0", "a1", "a2"), set::add);

assertTrue(set.contains("a2"));
}

@Test(expected = MojoFailureException.class)
public void assureInputInBatchModeWhenRequired() throws Exception{
final AddMojo mojo = getMojoFromPom();
final AddMojo mojoSpy = spy(mojo);

final Set<String> set = new HashSet<>();
mojoSpy.assureInputInBatchMode("", StringUtils::isNotEmpty, set::add, true);
}

@Test
public void assureInputInBatchModeWhenNotRequired() throws Exception{
final AddMojo mojo = getMojoFromPom();
final AddMojo mojoSpy = spy(mojo);

final Set<String> set = new HashSet<>();
mojoSpy.assureInputInBatchMode("a0", StringUtils::isNotEmpty, set::add, true);

assertTrue(set.contains("a0"));
}

private AddMojo getMojoFromPom() throws Exception {
final AddMojo mojo = (AddMojo) getMojoFromPom("/pom.xml", "add");
assertNotNull(mojo);
Expand Down

0 comments on commit 3f3d191

Please sign in to comment.