diff --git a/.gitignore b/.gitignore index 444c0c56..a3d34b35 100644 --- a/.gitignore +++ b/.gitignore @@ -42,3 +42,4 @@ build/ .run target/ Thumbs.db +run-app.sh \ No newline at end of file diff --git a/database/spring-cli/catalog/project-catalog.yml b/database/spring-cli/catalog/project-catalog.yml deleted file mode 100644 index 308c2568..00000000 --- a/database/spring-cli/catalog/project-catalog.yml +++ /dev/null @@ -1,11 +0,0 @@ ---- -project-repositories: -- name: "obaas" - description: "RESTful Hello World service for Oracle Backend for Spring Boot and Microservices" - url: "https://github.com/oracle/spring-cloud-oci/tree/main/database/spring-cli/projects/obaas" - tags: - - "obaas" - - "java-17" - - "rest" - - "web" - - "boot-3.2.x" diff --git a/database/spring-cli/projects/obaas/.mvn/wrapper/maven-wrapper.jar b/database/spring-cli/projects/obaas/.mvn/wrapper/maven-wrapper.jar deleted file mode 100644 index c1dd12f1..00000000 Binary files a/database/spring-cli/projects/obaas/.mvn/wrapper/maven-wrapper.jar and /dev/null differ diff --git a/database/spring-cli/projects/obaas/.mvn/wrapper/maven-wrapper.properties b/database/spring-cli/projects/obaas/.mvn/wrapper/maven-wrapper.properties deleted file mode 100644 index b7cb93e7..00000000 --- a/database/spring-cli/projects/obaas/.mvn/wrapper/maven-wrapper.properties +++ /dev/null @@ -1,2 +0,0 @@ -distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.8.4/apache-maven-3.8.4-bin.zip -wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.1.0/maven-wrapper-3.1.0.jar diff --git a/database/spring-cli/projects/obaas/README.adoc b/database/spring-cli/projects/obaas/README.adoc deleted file mode 100644 index 41c55517..00000000 --- a/database/spring-cli/projects/obaas/README.adoc +++ /dev/null @@ -1,72 +0,0 @@ -== Basic Spring Web Application for Oracle Backend for Spring Boot and Microservices - -This project contains a web service that can be deployed on link:https://bit.ly/oraclespringboot[Oracle Backend for Spring Boot and Microservices] -and includes configuration for the well-known endpoints provided by the platform. -The web service will accept HTTP GET requests at `http://localhost:8080/greeting`. - -It will respond with a JSON representation of a greeting, as the following listing shows: - -==== -[source,json] ----- -{"id":1,"content":"Hello, World!"} ----- -==== - -You can customize the greeting with an optional `name` parameter in the query string, as -the following listing shows: - -==== -[source,text] ----- -http://localhost:8080/greeting?name=User ----- -==== - -The `name` parameter value overrides the default value of `World` and is reflected in the -response, as the following listing shows: - -==== -[source,json] ----- -{"id":1,"content":"Hello, User!"} ----- -==== - -=== Building and running locally - -[source,bash] ----- -./mvnw spring-boot:run -Dspring-boot.run.profiles=local ----- - -Then access the endpoint - -[source,bash] ----- -curl http://localhost:8080/greeting?name=User ----- - -== About the sample code - -The link:./pom.xml[Maven POM file] contains dependencies for commonly used features. -You should review the POM file and remove any dependencies that you do not need. -Note that there may be dependency configuration in the link:./src/main/resources/application.yaml[Spring application configuration file] -as well, which you should remove if you remove the matching dependency. - -The link:./src/main/resources/application.yaml[Spring application configuration file] contains -comments that explain what the various configuration included is for. -You can use this as an example starting point and modify it to suit your needs. - -There are example link:./src/main/resources/db/changelog[Liquibase] configuration files -provided. If you want to use Liquibase, you will need to modify these to meet -your needs. Note that the use of Liquibase is optional. If you do not wish to -use Liquibase you can safely remove the `src/main/resources/db` directory, the -Liquibase dependency in the POM, the Liquibase build plugin in the POM, and the -Liquibase configuration in the Spring application configuration file. - -This example uses Oracle Universal Connection Pool (UCP) and also includes the -dependencies for Oracle Wallet authentication and mTLS. These are optional. -If you prefer to use HikariCP, you can remove the dependencies from the POM -and the `oracleucp` section in the datasource configuration in the Spring -application configuration file. \ No newline at end of file diff --git a/database/spring-cli/projects/obaas/src/main/java/com/example/restservice/greeting/Greeting.java b/database/spring-cli/projects/obaas/src/main/java/com/example/restservice/greeting/Greeting.java deleted file mode 100644 index 88fcd4a3..00000000 --- a/database/spring-cli/projects/obaas/src/main/java/com/example/restservice/greeting/Greeting.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.example.restservice.greeting; - -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/database/spring-cli/projects/obaas/src/test/java/com/example/restservice/greeting/GreetingControllerTests.java b/database/spring-cli/projects/obaas/src/test/java/com/example/restservice/greeting/GreetingControllerTests.java deleted file mode 100644 index 5b20e2ca..00000000 --- a/database/spring-cli/projects/obaas/src/test/java/com/example/restservice/greeting/GreetingControllerTests.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * 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 com.example.restservice.greeting; - -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.jupiter.api.Test; - -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.web.servlet.MockMvc; - -@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!")); - } - -} diff --git a/spring-cli/README .md b/spring-cli/README .md new file mode 100644 index 00000000..c9e32c63 --- /dev/null +++ b/spring-cli/README .md @@ -0,0 +1,56 @@ +# Spring CLI integration + +Spring CLI integration with a project catalog to help you create Spring Boot projects using Oracle and [Oracle Backend for Microservices and AI](https://bit.ly/OracleAI-microservices). + +## Install Spring CLI + +The goal of the Spring CLI is to increase your productivity when you create new projects and when you add functionality to existing projects. [Spring CLI documentation](https://docs.spring.io/spring-cli/reference/index.html) describes how to install the CLI on various platforms. + +## Add to Spring CLI + +Execute the following command to add the `project-catalog` for [Oracle Backend for Microservices and AI](https://bit.ly/OracleAI-microservices). + +```shell +spring project-catalog add --name obaas --url https://github.com/oracle/spring-cloud-oracle/tree/main/spring-cli/catalog +``` + +The Spring CLI is now aware of the [Oracle Backend for Microservices and AI](https://bit.ly/OracleAI-microservices) integration. + +## Create project + +To create a new project use the following command (there are more options available): + +```shell +spring boot new --from obaas --name myproject +``` + +## Run the Spring CLI application locally + +To run the application locally you need access to an Oracle Database (remotely or locally). Create a shell script with the following content called `run-app.sh` and set the values for the variables to reflect your environment. + +```shell +#!/bin/bash +export spring_datasource_url= +export liquibase_datasource_username= +export liquibase_datasource_password= +export spring_datasource_username= +export spring_datasource_password= +export otel_exporter_otlp_endpoint=http://localhost:8080 # Dummy URL +./mvnw spring-boot:run -Dspring-boot.run.profiles=local +``` + +The Spring profile `local` turns off Eureka discovery and OTLP tracing. + +Execute the shell script running the following command: + +```shell +source run-app.sh +``` + +You can see a few `WARNINGS` with this message which can be ignored: + +```log +WARNING: + +Liquibase detected the following invalid LIQUIBASE_* environment variables: +``` diff --git a/spring-cli/catalog/project-catalog.yml b/spring-cli/catalog/project-catalog.yml new file mode 100644 index 00000000..954c6471 --- /dev/null +++ b/spring-cli/catalog/project-catalog.yml @@ -0,0 +1,11 @@ +--- +project-repositories: +- name: "obaas" + description: "RESTful Greeting service for Oracle Backend Microservices and AI" + url: "https://github.com/oracle/spring-cloud-oci/tree/main/spring-cli/projects/obaas" + tags: + - "obaas" + - "java-17" + - "rest" + - "web" + - "boot-3.4.x" diff --git a/database/spring-cli/projects/obaas/.gitignore b/spring-cli/projects/obaas/.gitignore similarity index 100% rename from database/spring-cli/projects/obaas/.gitignore rename to spring-cli/projects/obaas/.gitignore diff --git a/database/spring-cli/projects/obaas/LICENSE b/spring-cli/projects/obaas/LICENSE similarity index 100% rename from database/spring-cli/projects/obaas/LICENSE rename to spring-cli/projects/obaas/LICENSE diff --git a/spring-cli/projects/obaas/README.adoc b/spring-cli/projects/obaas/README.adoc new file mode 100644 index 00000000..d3bb5137 --- /dev/null +++ b/spring-cli/projects/obaas/README.adoc @@ -0,0 +1,55 @@ +== Basic Spring Web Application for Oracle Backend for Spring Boot and Microservices + +This project contains a web service that can be deployed on link:https://bit.ly/OracleAI-microservices[Oracle Backend for Microservices and AI] and includes configuration for the well-known endpoints provided by the platform. The web service will accept HTTP GET requests at `http://localhost:8080/greeting`. + +It will respond with a JSON representation of a greeting, as the following listing shows: + +==== +[source,json] +---- +{"id":1,"content":"Hello, World!"} +---- +==== + +You can customize the greeting with an optional `name` parameter in the query string, as the following listing shows: + +==== +[source,text] +---- +http://localhost:8080/greeting?name=User +---- +==== + +The `name` parameter value overrides the default value of `World` and is reflected in the response, as the following listing shows: + +==== +[source,json] +---- +{"id":1,"content":"Hello, User!"} +---- +==== + +=== Building and running locally + +[source,bash] +---- +./mvnw spring-boot:run -Dspring-boot.run.profiles=local +---- + +Then access the endpoint + +[source,bash] +---- +curl http://localhost:8080/greeting?name=User +---- + +== About the sample code + +The link:./pom.xml[Maven POM file] contains dependencies for commonly used features. You should review the POM file and remove any dependencies that you do not need. +Note that there may be dependency configuration in the link:./src/main/resources/application.yaml[Spring application configuration file] as well, which you should remove if you remove the matching dependency. + +The link:./src/main/resources/application.yaml[Spring application configuration file] contains comments that explain what the various configuration included is for. You can use this as an example starting point and modify it to suit your needs. + +There are example link:./src/main/resources/db/changelog[Liquibase] configuration files provided. If you want to use Liquibase, you will need to modify these to meet your needs. Note that the use of Liquibase is optional. If you do not wish to use Liquibase you can safely remove the `src/main/resources/db` directory, the Liquibase dependency in the POM, the Liquibase build plugin in the POM, and the Liquibase configuration in the Spring application configuration file. + +This example uses Oracle Universal Connection Pool (UCP) and also includes the dependencies for Oracle Wallet authentication and mTLS. These are optional. If you prefer to use HikariCP, you can remove the dependencies from the POM and the `oracleucp` section in the datasource configuration in the Spring application configuration file. diff --git a/database/spring-cli/projects/obaas/mvnw b/spring-cli/projects/obaas/mvnw similarity index 100% rename from database/spring-cli/projects/obaas/mvnw rename to spring-cli/projects/obaas/mvnw diff --git a/database/spring-cli/projects/obaas/mvnw.cmd b/spring-cli/projects/obaas/mvnw.cmd similarity index 100% rename from database/spring-cli/projects/obaas/mvnw.cmd rename to spring-cli/projects/obaas/mvnw.cmd diff --git a/database/spring-cli/projects/obaas/pom.xml b/spring-cli/projects/obaas/pom.xml similarity index 65% rename from database/spring-cli/projects/obaas/pom.xml rename to spring-cli/projects/obaas/pom.xml index 59559dee..198630ff 100644 --- a/database/spring-cli/projects/obaas/pom.xml +++ b/spring-cli/projects/obaas/pom.xml @@ -5,7 +5,7 @@ org.springframework.boot spring-boot-starter-parent - 3.2.11 + 3.4.2 @@ -14,27 +14,71 @@ 0.0.1-SNAPSHOT obaas-rest-service - RESTful web application for Oracle Backend for Spring Boot and Microservices + RESTful web application for Oracle Backend for Microservices and AI - 17 + 21 + 2024.0.0 24.4.0 - 4.30.0 - 1.0.5 + 4.31.0 + 1.0.6 - + org.springframework.boot spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-actuator + + + + + io.micrometer + micrometer-core + + + + + io.micrometer + micrometer-registry-prometheus + + + + + io.micrometer + micrometer-tracing-bridge-otel + + + + + io.opentelemetry + opentelemetry-exporter-otlp + + + + + io.micrometer + micrometer-tracing + + + + + org.springframework.cloud + spring-cloud-starter-netflix-eureka-client + + org.springframework.boot spring-boot-starter-data-jpa + @@ -44,6 +88,7 @@ ${oracle-springboot-starter.version} pom + com.oracle.database.spring @@ -54,28 +99,22 @@ - + org.liquibase liquibase-core - ${liquibase.version} - - - org.springframework.boot - spring-boot-starter-actuator - - + net.ttddyy.observation datasource-micrometer-spring-boot ${datasource-micrometer-spring-boot.version} - + org.springframework.boot spring-boot-starter-test @@ -83,6 +122,18 @@ + + + + org.springframework.cloud + spring-cloud-dependencies + ${spring-cloud.version} + pom + import + + + + @@ -98,7 +149,6 @@ org.liquibase liquibase-maven-plugin - ${liquibase.version} diff --git a/database/spring-cli/projects/obaas/src/main/java/com/example/restservice/Application.java b/spring-cli/projects/obaas/src/main/java/com/example/restservice/Application.java similarity index 100% rename from database/spring-cli/projects/obaas/src/main/java/com/example/restservice/Application.java rename to spring-cli/projects/obaas/src/main/java/com/example/restservice/Application.java diff --git a/database/spring-cli/projects/obaas/src/main/java/com/example/restservice/greeting/GreetingController.java b/spring-cli/projects/obaas/src/main/java/com/example/restservice/controller/GreetingController.java similarity index 51% rename from database/spring-cli/projects/obaas/src/main/java/com/example/restservice/greeting/GreetingController.java rename to spring-cli/projects/obaas/src/main/java/com/example/restservice/controller/GreetingController.java index 64e4290c..6456eba1 100644 --- a/database/spring-cli/projects/obaas/src/main/java/com/example/restservice/greeting/GreetingController.java +++ b/spring-cli/projects/obaas/src/main/java/com/example/restservice/controller/GreetingController.java @@ -1,4 +1,4 @@ -package com.example.restservice.greeting; +package com.example.restservice.controller; import java.util.concurrent.atomic.AtomicLong; @@ -6,14 +6,23 @@ import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; +import com.example.restservice.model.Greeting; + @RestController public class GreetingController { private static final String template = "Hello, %s!"; private final AtomicLong counter = new AtomicLong(); + /** + * Create and return greeting with a counter. + * + * @param name String name + * @return Returns Greeting record with optional name + */ @GetMapping("/greeting") - public Greeting greeting(@RequestParam(value = "name", defaultValue = "World") String name) { - return new Greeting(counter.incrementAndGet(), String.format(template, name)); + public Greeting greeting(@RequestParam(defaultValue = "World") String name) { + return new Greeting(counter.incrementAndGet(), template.formatted(name)); } + } diff --git a/spring-cli/projects/obaas/src/main/java/com/example/restservice/model/Greeting.java b/spring-cli/projects/obaas/src/main/java/com/example/restservice/model/Greeting.java new file mode 100644 index 00000000..5c91ef95 --- /dev/null +++ b/spring-cli/projects/obaas/src/main/java/com/example/restservice/model/Greeting.java @@ -0,0 +1,4 @@ +package com.example.restservice.model; + +public record Greeting (Long id, String Content){ +} diff --git a/spring-cli/projects/obaas/src/main/resources/application-local.yaml b/spring-cli/projects/obaas/src/main/resources/application-local.yaml new file mode 100644 index 00000000..e58251c6 --- /dev/null +++ b/spring-cli/projects/obaas/src/main/resources/application-local.yaml @@ -0,0 +1,98 @@ +# +# This is an example Spring application.yaml configuration file for a +# Spring Boot application that can be deployed to Oracle Backend for +# Spring Boot and Microservices. +# +spring: + application: + name: restservice + threads: + virtual: + enabled: true + jpa: + hibernate: + ddl-auto: validate + properties: + hibernate: + dialect: org.hibernate.dialect.OracleDialect + format_sql: true + show-sql: true + + # This section configures Liquibase to automate the schema and reference + # data based on the definitions under src/main/resources/db/changelog. + # Comment out this section if you do not need Liquibase. + # Note that the variables used here will be injected into the deployment + # automatically when you deploy with oractl or the IDE plugins. + liquibase: + change-log: classpath:db/changelog/controller.yaml + url: ${spring.datasource.url} + user: ${liquibase.datasource.username} + password: ${liquibase.datasource.password} + enabled: ${LIQUIBASE_ENABLED:true} + + # This section configures an Oracle datasource using UCP. You can adjust + # the pool sizing parameters below in the oracleucp section. + # Note that the variables used here will be injected into the deployment + # automatically when you deploy with oractl or the IDE plugins. + datasource: + url: ${spring.datasource.url} + username: ${spring.datasource.username} + password: ${spring.datasource.password} + driver-class-name: oracle.jdbc.OracleDriver + type: oracle.ucp.jdbc.PoolDataSource + oracleucp: + connection-factory-class-name: oracle.jdbc.pool.OracleDataSource + connection-pool-name: SpringCLIConnectionPool + initial-pool-size: 15 + min-pool-size: 10 + max-pool-size: 30 + shared: true + +# This section configures service discovery using the Spring Cloud Eureka Service +# Registry that is automatically installed and configured in Oracle Backend for +# Spring Boot and Microservices. Note that it must be configured to use IP address +# not hostname since the deployment is in Kubernetes and the hostname allocated to +# pod will not match. +eureka: + instance: + hostname: ${spring.application.name} + preferIpAddress: true + client: + service-url: + defaultZone: ${eureka.service-url} + fetch-registry: false + register-with-eureka: false + enabled: false + +# This section configures tracing and metrics +management: + endpoint: + health: + show-details: always + endpoints: + web: + exposure: + include: "*" + metrics: + tags: + application: ${spring.application.name} + tracing: + sampling: + probability: 1.0 + info: + os: + enabled: true + env: + enabled: true + java: + enabled: true + otlp: + tracing: + endpoint: ${otel.exporter.otlp.endpoint} + export: + enabled: false + +logging: + level: + root: INFO + com.example: INFO \ No newline at end of file diff --git a/database/spring-cli/projects/obaas/src/main/resources/application.yaml b/spring-cli/projects/obaas/src/main/resources/application.yaml similarity index 96% rename from database/spring-cli/projects/obaas/src/main/resources/application.yaml rename to spring-cli/projects/obaas/src/main/resources/application.yaml index 32914dcb..ad8ed0d5 100644 --- a/database/spring-cli/projects/obaas/src/main/resources/application.yaml +++ b/spring-cli/projects/obaas/src/main/resources/application.yaml @@ -3,7 +3,6 @@ # Spring Boot application that can be deployed to Oracle Backend for # Spring Boot and Microservices. # - spring: application: name: restservice @@ -19,7 +18,6 @@ spring: format_sql: true show-sql: true - # This section configures Liquibase to automate the schema and reference # data based on the definitions under src/main/resources/db/changelog. # Comment out this section if you do not need Liquibase. @@ -44,7 +42,7 @@ spring: type: oracle.ucp.jdbc.PoolDataSource oracleucp: connection-factory-class-name: oracle.jdbc.pool.OracleDataSource - connection-pool-name: CustomerConnectionPool + connection-pool-name: SpringCLIConnectionPool initial-pool-size: 15 min-pool-size: 10 max-pool-size: 30 @@ -66,7 +64,7 @@ eureka: register-with-eureka: true enabled: true -# This section confiugres +# This section configures tracing and metrics management: endpoint: health: diff --git a/database/spring-cli/projects/obaas/src/main/resources/db/changelog/controller.yaml b/spring-cli/projects/obaas/src/main/resources/db/changelog/controller.yaml similarity index 100% rename from database/spring-cli/projects/obaas/src/main/resources/db/changelog/controller.yaml rename to spring-cli/projects/obaas/src/main/resources/db/changelog/controller.yaml diff --git a/database/spring-cli/projects/obaas/src/main/resources/db/changelog/data.sql b/spring-cli/projects/obaas/src/main/resources/db/changelog/data.sql similarity index 77% rename from database/spring-cli/projects/obaas/src/main/resources/db/changelog/data.sql rename to spring-cli/projects/obaas/src/main/resources/db/changelog/data.sql index de4c9ba4..6bab8e44 100644 --- a/database/spring-cli/projects/obaas/src/main/resources/db/changelog/data.sql +++ b/spring-cli/projects/obaas/src/main/resources/db/changelog/data.sql @@ -1,6 +1,6 @@ -- liquibase formatted sql --- changeset restservice:1 runAlways:true +-- changeset springcliapp:1 runAlways:true truncate table mytable; insert into mytable (a,b,c,d) diff --git a/database/spring-cli/projects/obaas/src/main/resources/db/changelog/table.sql b/spring-cli/projects/obaas/src/main/resources/db/changelog/table.sql similarity index 76% rename from database/spring-cli/projects/obaas/src/main/resources/db/changelog/table.sql rename to spring-cli/projects/obaas/src/main/resources/db/changelog/table.sql index 4484290d..0cfddef2 100644 --- a/database/spring-cli/projects/obaas/src/main/resources/db/changelog/table.sql +++ b/spring-cli/projects/obaas/src/main/resources/db/changelog/table.sql @@ -1,11 +1,11 @@ -- liquibase formatted sql ---changeset restservice:1 ---preconditions onfail:restservice_ran onerror:restservice_ran +--changeset springcliapp:1 +--preconditions onfail:MARK_RAN onerror:MARK_RAN --precondition-sql-check expectedresult:0 select count(*) from mytable where 1=2 drop table mytable; ---changeset customer:2 +--changeset springcliapp:2 create table mytable ( a varchar2 (20), b varchar2 (40), diff --git a/spring-cli/projects/obaas/src/test/java/com/example/restservice/greeting/GreetingControllerTests.java b/spring-cli/projects/obaas/src/test/java/com/example/restservice/greeting/GreetingControllerTests.java new file mode 100644 index 00000000..734b7305 --- /dev/null +++ b/spring-cli/projects/obaas/src/test/java/com/example/restservice/greeting/GreetingControllerTests.java @@ -0,0 +1,40 @@ +package com.example.restservice.greeting; + +import org.junit.jupiter.api.Test; +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.web.servlet.MockMvc; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +@SpringBootTest +@AutoConfigureMockMvc +public class GreetingControllerTests { + + @Autowired + private MockMvc mockMvc; + + @Test + public void noParamGreetingShouldReturnDefaultMessage() throws Exception { + mockMvc.perform(get("/greeting")) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.id").value(1)) + .andExpect(jsonPath("$.Content").value("Hello, World!")); + + } + + @Test + public void paramGreetingShouldReturnTailoredMessage() throws Exception { + String name = "Spring Community"; + String expectedGreeting = "Hello, " + name + "!"; + + mockMvc.perform(get("/greeting").param("name", name)) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.id").value(2)) + .andExpect(jsonPath("$.Content").value(expectedGreeting)); + } + +}