forked from thoughtworks/HeartBeat
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ADM-655[backend]: Create a new API for getting version of our applica…
…tion (#783) * ADM-655:[backend]feat: add VersionController and test --------- Co-authored-by: Andrea <Andrea2000728>
- Loading branch information
1 parent
4a8b52c
commit 49cd0e4
Showing
4 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
backend/src/main/java/heartbeat/controller/version/VersionController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,3 +27,7 @@ springdoc: | |
path: /docs | ||
api-docs: | ||
path: /api-docs | ||
|
||
heartbeat: | ||
version: 1.1.4 | ||
|
38 changes: 38 additions & 0 deletions
38
backend/src/test/java/heartbeat/controller/version/VersionControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |