From 873bf1e7bc2835e413a655c7f317d559c3d62b57 Mon Sep 17 00:00:00 2001 From: Tibor Tarnai Date: Tue, 10 Dec 2019 12:03:30 +0100 Subject: [PATCH] first rest service --- src/.gitignore | 4 ++ src/tamagotchi-service/app.yaml | 2 + src/tamagotchi-service/pom.xml | 53 ++++++++++++++++++ .../src/main/java/hello/Application.java | 11 ++++ .../src/main/java/hello/Greeting.java | 20 +++++++ .../main/java/hello/GreetingController.java | 19 +++++++ .../java/hello/GreetingControllerTests.java | 54 +++++++++++++++++++ 7 files changed, 163 insertions(+) create mode 100644 src/tamagotchi-service/app.yaml create mode 100644 src/tamagotchi-service/pom.xml create mode 100644 src/tamagotchi-service/src/main/java/hello/Application.java create mode 100644 src/tamagotchi-service/src/main/java/hello/Greeting.java create mode 100644 src/tamagotchi-service/src/main/java/hello/GreetingController.java create mode 100644 src/tamagotchi-service/src/test/java/hello/GreetingControllerTests.java diff --git a/src/.gitignore b/src/.gitignore index db07b3a..6576501 100644 --- a/src/.gitignore +++ b/src/.gitignore @@ -3,3 +3,7 @@ # run "dep ensure --vendor-only" to download the dependencies to vendor/ based # on the Gopkg.{toml,lock} files in that directory. vendor/ +tamagotchi-service/target +tamagotchi-service/.idea +*.iml + diff --git a/src/tamagotchi-service/app.yaml b/src/tamagotchi-service/app.yaml new file mode 100644 index 0000000..fe4a130 --- /dev/null +++ b/src/tamagotchi-service/app.yaml @@ -0,0 +1,2 @@ +runtime: java11 +entrypoint: java -jar target/tamagotchi-service-0.1.0.jar diff --git a/src/tamagotchi-service/pom.xml b/src/tamagotchi-service/pom.xml new file mode 100644 index 0000000..0d9d8fd --- /dev/null +++ b/src/tamagotchi-service/pom.xml @@ -0,0 +1,53 @@ + + + 4.0.0 + + com.sap + tamagotchi-service + 0.1.0 + + + 1.8 + 1.8 + 1.8 + + + + org.springframework.boot + spring-boot-starter-parent + 2.1.6.RELEASE + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-test + test + + + com.jayway.jsonpath + json-path + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + com.google.appengine + appengine-maven-plugin + 1.9.77 + + + + + diff --git a/src/tamagotchi-service/src/main/java/hello/Application.java b/src/tamagotchi-service/src/main/java/hello/Application.java new file mode 100644 index 0000000..2eaac3d --- /dev/null +++ b/src/tamagotchi-service/src/main/java/hello/Application.java @@ -0,0 +1,11 @@ +package hello; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Application { + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } +} diff --git a/src/tamagotchi-service/src/main/java/hello/Greeting.java b/src/tamagotchi-service/src/main/java/hello/Greeting.java new file mode 100644 index 0000000..5d10b59 --- /dev/null +++ b/src/tamagotchi-service/src/main/java/hello/Greeting.java @@ -0,0 +1,20 @@ +package hello; + +public class Greeting { + + private final long id; + private final String content; + + public Greeting(long id, String content) { + this.id = id; + this.content = content; + } + + public long getId() { + return id; + } + + public String getContent() { + return content; + } +} diff --git a/src/tamagotchi-service/src/main/java/hello/GreetingController.java b/src/tamagotchi-service/src/main/java/hello/GreetingController.java new file mode 100644 index 0000000..e356ab2 --- /dev/null +++ b/src/tamagotchi-service/src/main/java/hello/GreetingController.java @@ -0,0 +1,19 @@ +package hello; + +import java.util.concurrent.atomic.AtomicLong; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class GreetingController { + + private static final String template = "Hello, %s!"; + private final AtomicLong counter = new AtomicLong(); + + @RequestMapping("/greeting") + public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) { + return new Greeting(counter.incrementAndGet(), + String.format(template, name)); + } +} diff --git a/src/tamagotchi-service/src/test/java/hello/GreetingControllerTests.java b/src/tamagotchi-service/src/test/java/hello/GreetingControllerTests.java new file mode 100644 index 0000000..4a3c64b --- /dev/null +++ b/src/tamagotchi-service/src/test/java/hello/GreetingControllerTests.java @@ -0,0 +1,54 @@ +/* + * Copyright 2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package hello; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; + +@RunWith(SpringRunner.class) +@SpringBootTest +@AutoConfigureMockMvc +public class GreetingControllerTests { + + @Autowired + private MockMvc mockMvc; + + @Test + public void noParamGreetingShouldReturnDefaultMessage() throws Exception { + + this.mockMvc.perform(get("/greeting")).andDo(print()).andExpect(status().isOk()) + .andExpect(jsonPath("$.content").value("Hello, World!")); + } + + @Test + public void paramGreetingShouldReturnTailoredMessage() throws Exception { + + this.mockMvc.perform(get("/greeting").param("name", "Spring Community")) + .andDo(print()).andExpect(status().isOk()) + .andExpect(jsonPath("$.content").value("Hello, Spring Community!")); + } + +}