From 49cd0e41b1acf715a5ce5ff41a25182619f73798 Mon Sep 17 00:00:00 2001 From: andrea999 <74648528+Andrea2000728@users.noreply.github.com> Date: Wed, 6 Dec 2023 09:42:02 +0800 Subject: [PATCH] ADM-655[backend]: Create a new API for getting version of our application (#783) * ADM-655:[backend]feat: add VersionController and test --------- Co-authored-by: Andrea --- .github/ISSUE_TEMPLATE/bug_report.yml | 8 ++++ .../controller/version/VersionController.java | 32 ++++++++++++++++ backend/src/main/resources/application.yml | 4 ++ .../version/VersionControllerTest.java | 38 +++++++++++++++++++ 4 files changed, 82 insertions(+) create mode 100644 backend/src/main/java/heartbeat/controller/version/VersionController.java create mode 100644 backend/src/test/java/heartbeat/controller/version/VersionControllerTest.java diff --git a/.github/ISSUE_TEMPLATE/bug_report.yml b/.github/ISSUE_TEMPLATE/bug_report.yml index 695c79c133..a4885deda0 100644 --- a/.github/ISSUE_TEMPLATE/bug_report.yml +++ b/.github/ISSUE_TEMPLATE/bug_report.yml @@ -27,6 +27,14 @@ body: validations: required: true +- type: input + id: version + attributes: + label: Version + description: What version of our software are you running? You can get the version by add "api/v1/version" after the host address you visit the heartbeat. If none, you can input "none" as well + validations: + required: true + - type: dropdown id: browsers attributes: diff --git a/backend/src/main/java/heartbeat/controller/version/VersionController.java b/backend/src/main/java/heartbeat/controller/version/VersionController.java new file mode 100644 index 0000000000..b54cab597a --- /dev/null +++ b/backend/src/main/java/heartbeat/controller/version/VersionController.java @@ -0,0 +1,32 @@ +package heartbeat.controller.version; + +import lombok.RequiredArgsConstructor; +import lombok.extern.log4j.Log4j2; +import org.springframework.http.ResponseEntity; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.beans.factory.annotation.Value; + +import java.util.HashMap; +import java.util.Map; + +@RestController +@RequiredArgsConstructor +@RequestMapping("/version") +@Validated +@Log4j2 +public class VersionController { + + @Value("${heartbeat.version}") + private String version; + + @GetMapping() + public ResponseEntity> getVersion() { + Map response = new HashMap<>(); + response.put("version", version); + return ResponseEntity.ok(response); + } + +} diff --git a/backend/src/main/resources/application.yml b/backend/src/main/resources/application.yml index 2b461422ec..7d6718302b 100644 --- a/backend/src/main/resources/application.yml +++ b/backend/src/main/resources/application.yml @@ -27,3 +27,7 @@ springdoc: path: /docs api-docs: path: /api-docs + +heartbeat: + version: 1.1.4 + diff --git a/backend/src/test/java/heartbeat/controller/version/VersionControllerTest.java b/backend/src/test/java/heartbeat/controller/version/VersionControllerTest.java new file mode 100644 index 0000000000..6370b79907 --- /dev/null +++ b/backend/src/test/java/heartbeat/controller/version/VersionControllerTest.java @@ -0,0 +1,38 @@ +package heartbeat.controller.version; + +import com.jayway.jsonpath.JsonPath; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.test.autoconfigure.json.AutoConfigureJsonTesters; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.test.context.junit.jupiter.SpringExtension; +import org.springframework.test.web.servlet.MockMvc; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +@WebMvcTest(VersionController.class) +@ExtendWith(SpringExtension.class) +@AutoConfigureJsonTesters +class VersionControllerTest { + + @Autowired + private MockMvc mockMvc; + + @Value("${heartbeat.version}") + private String version; + + @Test + void shouldReturnHeartBeatVersion() throws Exception { + + final var response = mockMvc.perform(get("/version")).andExpect(status().isOk()).andReturn().getResponse(); + final var content = response.getContentAsString(); + final var result = JsonPath.parse(content).read("$.version").toString(); + + assertThat(result).isEqualTo(version); + } + +}