Skip to content
This repository was archived by the owner on Mar 2, 2022. It is now read-only.

Commit 1dafc07

Browse files
committed
create new project.
1 parent 377cdab commit 1dafc07

File tree

11 files changed

+419
-3
lines changed

11 files changed

+419
-3
lines changed

.gitignore

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
# Compiled class file
1+
# Build output
2+
target/
23
*.class
34

45
# Log file
@@ -13,11 +14,25 @@
1314
# Package Files #
1415
*.jar
1516
*.war
16-
*.nar
1717
*.ear
1818
*.zip
1919
*.tar.gz
2020
*.rar
2121

2222
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
2323
hs_err_pid*
24+
25+
# IDE
26+
.idea/
27+
*.iml
28+
.settings/
29+
.project
30+
.classpath
31+
32+
# macOS
33+
.DS_Store
34+
35+
# Azure Functions
36+
local.settings.json
37+
bin/
38+
obj/

.vscode/extensions.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"recommendations": [
3+
"ms-azuretools.vscode-azurefunctions",
4+
"vscjava.vscode-java-debug"
5+
]
6+
}

.vscode/launch.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
{
5+
"name": "Attach to Java Functions",
6+
"type": "java",
7+
"request": "attach",
8+
"hostName": "127.0.0.1",
9+
"port": 5005,
10+
"preLaunchTask": "func: host start"
11+
}
12+
]
13+
}

.vscode/settings.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"azureFunctions.deploySubpath": "target/azure-functions/azure-functions-java11",
3+
"azureFunctions.projectLanguage": "Java",
4+
"azureFunctions.projectRuntime": "~3",
5+
"debug.internalConsoleOptions": "neverOpen",
6+
"azureFunctions.preDeployTask": "package",
7+
"java.configuration.updateBuildConfiguration": "interactive"
8+
}

.vscode/tasks.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"version": "2.0.0",
3+
"tasks": [
4+
{
5+
"type": "func",
6+
"command": "host start",
7+
"problemMatcher": "$func-java-watch",
8+
"isBackground": true,
9+
"options": {
10+
"cwd": "${workspaceFolder}/target/azure-functions/azure-functions-java11"
11+
},
12+
"dependsOn": "package"
13+
},
14+
{
15+
"label": "package",
16+
"command": "mvn clean package",
17+
"type": "shell",
18+
"group": {
19+
"kind": "build",
20+
"isDefault": true
21+
}
22+
}
23+
]
24+
}

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
# azure-functions-java11
1+
# Azure Functions Java 11
2+
3+
https://azure.microsoft.com/ja-jp/updates/java-11-support-for-azure-functions-is-now-generally-available/

host.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"version": "2.0",
3+
"extensionBundle": {
4+
"id": "Microsoft.Azure.Functions.ExtensionBundle",
5+
"version": "[1.*, 2.0.0)"
6+
}
7+
}

pom.xml

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>com.function.hosomi</groupId>
6+
<artifactId>azure-functions-java11</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
10+
<name>Azure Java Functions</name>
11+
12+
<properties>
13+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
14+
<java.version>11</java.version>
15+
<azure.functions.maven.plugin.version>1.9.0</azure.functions.maven.plugin.version>
16+
<azure.functions.java.library.version>1.4.0</azure.functions.java.library.version>
17+
<functionAppName>azure-functions-java11</functionAppName>
18+
<stagingDirectory>${project.build.directory}/azure-functions/${functionAppName}</stagingDirectory>
19+
</properties>
20+
21+
<dependencies>
22+
<dependency>
23+
<groupId>com.microsoft.azure.functions</groupId>
24+
<artifactId>azure-functions-java-library</artifactId>
25+
<version>${azure.functions.java.library.version}</version>
26+
</dependency>
27+
28+
<!-- Test -->
29+
<dependency>
30+
<groupId>org.junit.jupiter</groupId>
31+
<artifactId>junit-jupiter</artifactId>
32+
<version>5.4.2</version>
33+
<scope>test</scope>
34+
</dependency>
35+
36+
<dependency>
37+
<groupId>org.mockito</groupId>
38+
<artifactId>mockito-core</artifactId>
39+
<version>2.23.4</version>
40+
<scope>test</scope>
41+
</dependency>
42+
</dependencies>
43+
44+
<build>
45+
<plugins>
46+
<plugin>
47+
<groupId>org.apache.maven.plugins</groupId>
48+
<artifactId>maven-compiler-plugin</artifactId>
49+
<version>3.8.1</version>
50+
<configuration>
51+
<source>${java.version}</source>
52+
<target>${java.version}</target>
53+
<encoding>${project.build.sourceEncoding}</encoding>
54+
</configuration>
55+
</plugin>
56+
<plugin>
57+
<groupId>com.microsoft.azure</groupId>
58+
<artifactId>azure-functions-maven-plugin</artifactId>
59+
<version>${azure.functions.maven.plugin.version}</version>
60+
<configuration>
61+
<!-- function app name -->
62+
<appName>${functionAppName}</appName>
63+
<!-- function app resource group -->
64+
<resourceGroup>java-functions-group</resourceGroup>
65+
<!-- function app service plan name -->
66+
<appServicePlanName>java-functions-app-service-plan</appServicePlanName>
67+
<!-- function app region-->
68+
<!-- refers https://github.com/microsoft/azure-maven-plugins/wiki/Azure-Functions:-Configuration-Details#supported-regions for all valid values -->
69+
<region>japanwest</region>
70+
<!-- function pricingTier, default to be consumption if not specified -->
71+
<!-- refers https://github.com/microsoft/azure-maven-plugins/wiki/Azure-Functions:-Configuration-Details#supported-pricing-tiers for all valid values -->
72+
<!-- <pricingTier></pricingTier> -->
73+
74+
<!-- Whether to disable application insights, default is false -->
75+
<!-- refers https://github.com/microsoft/azure-maven-plugins/wiki/Azure-Functions:-Configuration-Details for all valid configurations for application insights-->
76+
<!-- <disableAppInsights></disableAppInsights> -->
77+
<runtime>
78+
<!-- runtime os, could be windows, linux or docker-->
79+
<os>windows</os>
80+
<javaVersion>11</javaVersion>
81+
<!-- for docker function, please set the following parameters -->
82+
<!-- <image>[hub-user/]repo-name[:tag]</image> -->
83+
<!-- <serverId></serverId> -->
84+
<!-- <registryUrl></registryUrl> -->
85+
</runtime>
86+
<appSettings>
87+
<property>
88+
<name>FUNCTIONS_EXTENSION_VERSION</name>
89+
<value>~3</value>
90+
</property>
91+
</appSettings>
92+
</configuration>
93+
<executions>
94+
<execution>
95+
<id>package-functions</id>
96+
<goals>
97+
<goal>package</goal>
98+
</goals>
99+
</execution>
100+
</executions>
101+
</plugin>
102+
<plugin>
103+
<groupId>org.apache.maven.plugins</groupId>
104+
<artifactId>maven-resources-plugin</artifactId>
105+
<version>3.1.0</version>
106+
<executions>
107+
<execution>
108+
<id>copy-resources</id>
109+
<phase>package</phase>
110+
<goals>
111+
<goal>copy-resources</goal>
112+
</goals>
113+
<configuration>
114+
<overwrite>true</overwrite>
115+
<outputDirectory>${stagingDirectory}</outputDirectory>
116+
<resources>
117+
<resource>
118+
<directory>${project.basedir}</directory>
119+
<includes>
120+
<include>host.json</include>
121+
<include>local.settings.json</include>
122+
</includes>
123+
</resource>
124+
</resources>
125+
</configuration>
126+
</execution>
127+
</executions>
128+
</plugin>
129+
<plugin>
130+
<groupId>org.apache.maven.plugins</groupId>
131+
<artifactId>maven-dependency-plugin</artifactId>
132+
<version>3.1.1</version>
133+
<executions>
134+
<execution>
135+
<id>copy-dependencies</id>
136+
<phase>prepare-package</phase>
137+
<goals>
138+
<goal>copy-dependencies</goal>
139+
</goals>
140+
<configuration>
141+
<outputDirectory>${stagingDirectory}/lib</outputDirectory>
142+
<overWriteReleases>false</overWriteReleases>
143+
<overWriteSnapshots>false</overWriteSnapshots>
144+
<overWriteIfNewer>true</overWriteIfNewer>
145+
<includeScope>runtime</includeScope>
146+
<excludeArtifactIds>azure-functions-java-library</excludeArtifactIds>
147+
</configuration>
148+
</execution>
149+
</executions>
150+
</plugin>
151+
<!--Remove obj folder generated by .NET SDK in maven clean-->
152+
<plugin>
153+
<artifactId>maven-clean-plugin</artifactId>
154+
<version>3.1.0</version>
155+
<configuration>
156+
<filesets>
157+
<fileset>
158+
<directory>obj</directory>
159+
</fileset>
160+
</filesets>
161+
</configuration>
162+
</plugin>
163+
</plugins>
164+
</build>
165+
</project>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.function.hosomi;
2+
3+
import com.microsoft.azure.functions.ExecutionContext;
4+
import com.microsoft.azure.functions.HttpMethod;
5+
import com.microsoft.azure.functions.HttpRequestMessage;
6+
import com.microsoft.azure.functions.HttpResponseMessage;
7+
import com.microsoft.azure.functions.HttpStatus;
8+
import com.microsoft.azure.functions.annotation.AuthorizationLevel;
9+
import com.microsoft.azure.functions.annotation.FunctionName;
10+
import com.microsoft.azure.functions.annotation.HttpTrigger;
11+
12+
import java.util.Optional;
13+
14+
/**
15+
* Azure Functions with HTTP Trigger.
16+
*/
17+
public class Function {
18+
/**
19+
* This function listens at endpoint "/api/HttpExample". Two ways to invoke it using "curl" command in bash:
20+
* 1. curl -d "HTTP Body" {your host}/api/HttpExample
21+
* 2. curl "{your host}/api/HttpExample?name=HTTP%20Query"
22+
*/
23+
@FunctionName("HttpExample")
24+
public HttpResponseMessage run(
25+
@HttpTrigger(
26+
name = "req",
27+
methods = {HttpMethod.GET, HttpMethod.POST},
28+
authLevel = AuthorizationLevel.ANONYMOUS)
29+
HttpRequestMessage<Optional<String>> request,
30+
final ExecutionContext context) {
31+
context.getLogger().info("Java HTTP trigger processed a request.");
32+
33+
// Parse query parameter
34+
final String query = request.getQueryParameters().get("name");
35+
final String name = request.getBody().orElse(query);
36+
37+
if (name == null) {
38+
return request.createResponseBuilder(HttpStatus.BAD_REQUEST).body("Please pass a name on the query string or in the request body").build();
39+
} else {
40+
return request.createResponseBuilder(HttpStatus.OK).body("Hello, " + name).build();
41+
}
42+
}
43+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.function.hosomi;
2+
3+
import com.microsoft.azure.functions.*;
4+
import org.mockito.invocation.InvocationOnMock;
5+
import org.mockito.stubbing.Answer;
6+
7+
import java.util.*;
8+
import java.util.logging.Logger;
9+
10+
import org.junit.jupiter.api.Test;
11+
import static org.junit.jupiter.api.Assertions.*;
12+
import static org.mockito.ArgumentMatchers.*;
13+
import static org.mockito.Mockito.*;
14+
15+
16+
/**
17+
* Unit test for Function class.
18+
*/
19+
public class FunctionTest {
20+
/**
21+
* Unit test for HttpTriggerJava method.
22+
*/
23+
@Test
24+
public void testHttpTriggerJava() throws Exception {
25+
// Setup
26+
@SuppressWarnings("unchecked")
27+
final HttpRequestMessage<Optional<String>> req = mock(HttpRequestMessage.class);
28+
29+
final Map<String, String> queryParams = new HashMap<>();
30+
queryParams.put("name", "Azure");
31+
doReturn(queryParams).when(req).getQueryParameters();
32+
33+
final Optional<String> queryBody = Optional.empty();
34+
doReturn(queryBody).when(req).getBody();
35+
36+
doAnswer(new Answer<HttpResponseMessage.Builder>() {
37+
@Override
38+
public HttpResponseMessage.Builder answer(InvocationOnMock invocation) {
39+
HttpStatus status = (HttpStatus) invocation.getArguments()[0];
40+
return new HttpResponseMessageMock.HttpResponseMessageBuilderMock().status(status);
41+
}
42+
}).when(req).createResponseBuilder(any(HttpStatus.class));
43+
44+
final ExecutionContext context = mock(ExecutionContext.class);
45+
doReturn(Logger.getGlobal()).when(context).getLogger();
46+
47+
// Invoke
48+
final HttpResponseMessage ret = new Function().run(req, context);
49+
50+
// Verify
51+
assertEquals(ret.getStatus(), HttpStatus.OK);
52+
}
53+
}

0 commit comments

Comments
 (0)