Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
H
hello-world
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
azure-functions
hello-world
Commits
afc67721
Commit
afc67721
authored
Jan 04, 2019
by
Fei Gao
Committed by
Fei Gao
Jan 04, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
first commit
parents
Pipeline
#392
failed with stages
Changes
7
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
406 additions
and
0 deletions
+406
-0
.gitignore
.gitignore
+16
-0
host.json
host.json
+3
-0
local.settings.json
local.settings.json
+7
-0
pom.xml
pom.xml
+208
-0
Function.java
src/main/java/com/sodalab/functions/Function.java
+34
-0
FunctionTest.java
src/test/java/com/sodalab/functions/FunctionTest.java
+58
-0
HttpResponseMessageMock.java
...t/java/com/sodalab/functions/HttpResponseMessageMock.java
+80
-0
No files found.
.gitignore
0 → 100644
View file @
afc67721
.gradle
target
build/
*.iml
*.ipr
*.iws
out
reports
.DS_Store
.idea
generated
.vagrant/
dump.rdb
generated-sources
api.html
_mybatis/
host.json
0 → 100644
View file @
afc67721
{
"version"
:
"2.0"
}
local.settings.json
0 → 100644
View file @
afc67721
{
"IsEncrypted"
:
false
,
"Values"
:
{
"AzureWebJobsStorage"
:
""
,
"FUNCTIONS_WORKER_RUNTIME"
:
"java"
}
}
pom.xml
0 → 100644
View file @
afc67721
<?xml version="1.0" encoding="UTF-8"?>
<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"
>
<modelVersion>
4.0.0
</modelVersion>
<groupId>
com.sodalab.functions
</groupId>
<artifactId>
sodalab-functions
</artifactId>
<version>
1.0-SNAPSHOT
</version>
<packaging>
jar
</packaging>
<name>
Azure Java Functions
</name>
<properties>
<project.build.sourceEncoding>
UTF-8
</project.build.sourceEncoding>
<maven.compiler.source>
1.8
</maven.compiler.source>
<maven.compiler.target>
1.8
</maven.compiler.target>
<azure.functions.maven.plugin.version>
1.2.1
</azure.functions.maven.plugin.version>
<azure.functions.java.library.version>
1.2.0
</azure.functions.java.library.version>
<functionAppName>
sodalab-functions-hello-world
</functionAppName>
<functionAppRegion>
China North
</functionAppRegion>
<stagingDirectory>
${project.build.directory}/azure-functions/${functionAppName}
</stagingDirectory>
<functionResourceGroup>
soda-lab
</functionResourceGroup>
</properties>
<repositories>
<repository>
<id>
maven.snapshots
</id>
<name>
Maven Central Snapshot Repository
</name>
<url>
https://oss.sonatype.org/content/repositories/snapshots/
</url>
<releases>
<enabled>
false
</enabled>
</releases>
<snapshots>
<enabled>
true
</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>
maven.snapshots
</id>
<name>
Maven Central Snapshot Repository
</name>
<url>
https://oss.sonatype.org/content/repositories/snapshots/
</url>
<releases>
<enabled>
false
</enabled>
</releases>
<snapshots>
<enabled>
true
</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>
junit
</groupId>
<artifactId>
junit
</artifactId>
<version>
4.12
</version>
</dependency>
<dependency>
<groupId>
org.mockito
</groupId>
<artifactId>
mockito-core
</artifactId>
<version>
2.4.0
</version>
</dependency>
<dependency>
<groupId>
com.microsoft.azure.functions
</groupId>
<artifactId>
azure-functions-java-library
</artifactId>
<version>
${azure.functions.java.library.version}
</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>
com.microsoft.azure.functions
</groupId>
<artifactId>
azure-functions-java-library
</artifactId>
</dependency>
<!-- Test -->
<dependency>
<groupId>
junit
</groupId>
<artifactId>
junit
</artifactId>
<scope>
test
</scope>
</dependency>
<dependency>
<groupId>
org.mockito
</groupId>
<artifactId>
mockito-core
</artifactId>
<scope>
test
</scope>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>
com.microsoft.azure
</groupId>
<artifactId>
azure-functions-maven-plugin
</artifactId>
<version>
${azure.functions.maven.plugin.version}
</version>
</plugin>
<plugin>
<groupId>
org.apache.maven.plugins
</groupId>
<artifactId>
maven-resources-plugin
</artifactId>
<version>
3.1.0
</version>
</plugin>
<plugin>
<groupId>
org.apache.maven.plugins
</groupId>
<artifactId>
maven-dependency-plugin
</artifactId>
<version>
3.1.1
</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>
com.microsoft.azure
</groupId>
<artifactId>
azure-functions-maven-plugin
</artifactId>
<configuration>
<resourceGroup>
${functionResourceGroup}
</resourceGroup>
<appName>
${functionAppName}
</appName>
<region>
${functionAppRegion}
</region>
<appSettings>
<!-- Run Azure Function from package file by default -->
<property>
<name>
WEBSITE_RUN_FROM_PACKAGE
</name>
<value>
1
</value>
</property>
<property>
<name>
FUNCTIONS_EXTENSION_VERSION
</name>
<value>
~2
</value>
</property>
<property>
<name>
FUNCTIONS_WORKER_RUNTIME
</name>
<value>
java
</value>
</property>
</appSettings>
</configuration>
<executions>
<execution>
<id>
package-functions
</id>
<goals>
<goal>
package
</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>
org.apache.maven.plugins
</groupId>
<artifactId>
maven-resources-plugin
</artifactId>
<executions>
<execution>
<id>
copy-resources
</id>
<phase>
package
</phase>
<goals>
<goal>
copy-resources
</goal>
</goals>
<configuration>
<overwrite>
true
</overwrite>
<outputDirectory>
${stagingDirectory}
</outputDirectory>
<resources>
<resource>
<directory>
${project.basedir}
</directory>
<includes>
<include>
host.json
</include>
<include>
local.settings.json
</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>
org.apache.maven.plugins
</groupId>
<artifactId>
maven-dependency-plugin
</artifactId>
<executions>
<execution>
<id>
copy-dependencies
</id>
<phase>
prepare-package
</phase>
<goals>
<goal>
copy-dependencies
</goal>
</goals>
<configuration>
<outputDirectory>
${stagingDirectory}/lib
</outputDirectory>
<overWriteReleases>
false
</overWriteReleases>
<overWriteSnapshots>
false
</overWriteSnapshots>
<overWriteIfNewer>
true
</overWriteIfNewer>
<includeScope>
runtime
</includeScope>
<excludeArtifactIds>
azure-functions-java-library
</excludeArtifactIds>
</configuration>
</execution>
</executions>
</plugin>
<!--Remove obj folder generated by .NET SDK in maven clean-->
<plugin>
<artifactId>
maven-clean-plugin
</artifactId>
<version>
3.1.0
</version>
<configuration>
<filesets>
<fileset>
<directory>
obj
</directory>
</fileset>
</filesets>
</configuration>
</plugin>
</plugins>
</build>
</project>
src/main/java/com/sodalab/functions/Function.java
0 → 100644
View file @
afc67721
package
com
.
sodalab
.
functions
;
import
java.util.*
;
import
com.microsoft.azure.functions.annotation.*
;
import
com.microsoft.azure.functions.*
;
/**
* Azure Functions with HTTP Trigger.
*/
public
class
Function
{
/**
* This function listens at endpoint "/api/HttpTrigger-Java". Two ways to invoke it using "curl" command in bash:
* 1. curl -d "HTTP Body" {your host}/api/HttpTrigger-Java&code={your function key}
* 2. curl "{your host}/api/HttpTrigger-Java?name=HTTP%20Query&code={your function key}"
* Function Key is not needed when running locally, it is used to invoke function deployed to Azure.
* More details: https://aka.ms/functions_authorization_keys
*/
@FunctionName
(
"HttpTrigger-Java"
)
public
HttpResponseMessage
run
(
@HttpTrigger
(
name
=
"req"
,
methods
=
{
HttpMethod
.
GET
,
HttpMethod
.
POST
},
authLevel
=
AuthorizationLevel
.
FUNCTION
)
HttpRequestMessage
<
Optional
<
String
>>
request
,
final
ExecutionContext
context
)
{
context
.
getLogger
().
info
(
"Java HTTP trigger processed a request."
);
// Parse query parameter
String
query
=
request
.
getQueryParameters
().
get
(
"name"
);
String
name
=
request
.
getBody
().
orElse
(
query
);
if
(
name
==
null
)
{
return
request
.
createResponseBuilder
(
HttpStatus
.
BAD_REQUEST
).
body
(
"Please pass a name on the query string or in the request body"
).
build
();
}
else
{
return
request
.
createResponseBuilder
(
HttpStatus
.
OK
).
body
(
"Hello, "
+
name
).
build
();
}
}
}
src/test/java/com/sodalab/functions/FunctionTest.java
0 → 100644
View file @
afc67721
package
com
.
sodalab
.
functions
;
import
org.junit.Test
;
import
com.microsoft.azure.functions.*
;
import
org.mockito.invocation.InvocationOnMock
;
import
org.mockito.stubbing.Answer
;
import
java.util.HashMap
;
import
java.util.Map
;
import
java.util.Optional
;
import
java.util.logging.Logger
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
mockito
.
ArgumentMatchers
.
any
;
import
static
org
.
mockito
.
Mockito
.
doAnswer
;
import
static
org
.
mockito
.
Mockito
.
doReturn
;
import
static
org
.
mockito
.
Mockito
.
mock
;
/**
* Unit test for Function class.
*/
public
class
FunctionTest
{
/**
* Unit test for HttpTriggerJava method.
*/
@Test
public
void
testHttpTriggerJava
()
throws
Exception
{
// Setup
@SuppressWarnings
(
"unchecked"
)
final
HttpRequestMessage
<
Optional
<
String
>>
req
=
mock
(
HttpRequestMessage
.
class
);
final
Map
<
String
,
String
>
queryParams
=
new
HashMap
<>();
queryParams
.
put
(
"name"
,
"Azure"
);
doReturn
(
queryParams
).
when
(
req
).
getQueryParameters
();
final
Optional
<
String
>
queryBody
=
Optional
.
empty
();
doReturn
(
queryBody
).
when
(
req
).
getBody
();
doAnswer
(
new
Answer
<
HttpResponseMessage
.
Builder
>()
{
@Override
public
HttpResponseMessage
.
Builder
answer
(
InvocationOnMock
invocation
)
{
HttpStatus
status
=
(
HttpStatus
)
invocation
.
getArguments
()[
0
];
return
new
HttpResponseMessageMock
.
HttpResponseMessageBuilderMock
().
status
(
status
);
}
}).
when
(
req
).
createResponseBuilder
(
any
(
HttpStatus
.
class
));
final
ExecutionContext
context
=
mock
(
ExecutionContext
.
class
);
doReturn
(
Logger
.
getGlobal
()).
when
(
context
).
getLogger
();
// Invoke
final
HttpResponseMessage
ret
=
new
Function
().
run
(
req
,
context
);
// Verify
assertEquals
(
ret
.
getStatus
(),
HttpStatus
.
OK
);
}
}
src/test/java/com/sodalab/functions/HttpResponseMessageMock.java
0 → 100644
View file @
afc67721
package
com
.
sodalab
.
functions
;
import
com.microsoft.azure.functions.*
;
import
java.util.Map
;
/**
* The mock for HttpResponseMessage, can be used in unit tests to verify if the
* returned response by HTTP trigger function is correct or not.
*/
public
class
HttpResponseMessageMock
implements
HttpResponseMessage
{
private
int
httpStatusCode
;
private
HttpStatusType
httpStatus
;
private
Object
body
;
private
Map
<
String
,
String
>
headers
;
public
HttpResponseMessageMock
(
HttpStatusType
status
,
Map
<
String
,
String
>
headers
,
Object
body
)
{
this
.
httpStatus
=
status
;
this
.
httpStatusCode
=
status
.
value
();
this
.
headers
=
headers
;
this
.
body
=
body
;
}
@Override
public
HttpStatusType
getStatus
()
{
return
this
.
httpStatus
;
}
@Override
public
int
getStatusCode
()
{
return
httpStatusCode
;
}
@Override
public
String
getHeader
(
String
key
)
{
return
this
.
headers
.
get
(
key
);
}
@Override
public
Object
getBody
()
{
return
this
.
body
;
}
public
static
class
HttpResponseMessageBuilderMock
implements
HttpResponseMessage
.
Builder
{
private
Object
body
;
private
int
httpStatusCode
;
private
Map
<
String
,
String
>
headers
;
private
HttpStatusType
httpStatus
;
public
Builder
status
(
HttpStatus
status
)
{
this
.
httpStatusCode
=
status
.
value
();
this
.
httpStatus
=
status
;
return
this
;
}
@Override
public
Builder
status
(
HttpStatusType
httpStatusType
)
{
this
.
httpStatusCode
=
httpStatusType
.
value
();
this
.
httpStatus
=
httpStatusType
;
return
this
;
}
@Override
public
HttpResponseMessage
.
Builder
header
(
String
key
,
String
value
)
{
this
.
headers
.
put
(
key
,
value
);
return
this
;
}
@Override
public
HttpResponseMessage
.
Builder
body
(
Object
body
)
{
this
.
body
=
body
;
return
this
;
}
@Override
public
HttpResponseMessage
build
()
{
return
new
HttpResponseMessageMock
(
this
.
httpStatus
,
this
.
headers
,
this
.
body
);
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment