Skip to content

Commit

Permalink
ADM-655[backend]: Create a new API for getting version of our applica…
Browse files Browse the repository at this point in the history
…tion (#783)

* ADM-655:[backend]feat: add VersionController and test


---------

Co-authored-by: Andrea <Andrea2000728>
  • Loading branch information
Andrea2000728 authored Dec 6, 2023
1 parent 4a8b52c commit 49cd0e4
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .github/ISSUE_TEMPLATE/bug_report.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Map<String, String>> getVersion() {
Map<String, String> response = new HashMap<>();
response.put("version", version);
return ResponseEntity.ok(response);
}

}
4 changes: 4 additions & 0 deletions backend/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,7 @@ springdoc:
path: /docs
api-docs:
path: /api-docs

heartbeat:
version: 1.1.4

Original file line number Diff line number Diff line change
@@ -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);
}

}

0 comments on commit 49cd0e4

Please sign in to comment.