-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from jecklgamis/next
Build Info controller, app tests,
- Loading branch information
Showing
10 changed files
with
137 additions
and
3 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
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
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,12 @@ | ||
#!/usr/bin/env bash | ||
|
||
BRANCH=$(git rev-parse --abbrev-ref HEAD) | ||
VERSION=$(git rev-parse HEAD) | ||
BUILD_TIME=$(date +"%Y-%m-%dT%H:%M:%S%z") | ||
BUILD_INFO_FILE=./src/main/resources/build-info.json | ||
|
||
cat <<EOF > ${BUILD_INFO_FILE} | ||
{ "branch": "${BRANCH}", "build-time": "${BUILD_TIME}", "version": "${VERSION}" } | ||
EOF | ||
|
||
echo "Wrote $BUILD_INFO_FILE" && cat ${BUILD_INFO_FILE} |
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
33 changes: 33 additions & 0 deletions
33
src/main/java/spring/boot/java/example/controller/BuildInfoController.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,33 @@ | ||
package spring.boot.java.example.controller; | ||
|
||
import com.google.common.io.Resources; | ||
import io.micrometer.core.annotation.Timed; | ||
import org.springframework.http.CacheControl; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.io.IOException; | ||
import java.nio.charset.Charset; | ||
|
||
import static org.springframework.web.bind.annotation.RequestMethod.GET; | ||
|
||
@RestController | ||
public class BuildInfoController { | ||
private static String buildInfo = loadBuildInfo(); | ||
|
||
private static String loadBuildInfo() { | ||
try { | ||
return Resources.toString(Resources.getResource("build-info.json"), Charset.defaultCharset()); | ||
} catch (IOException e) { | ||
return "{}"; | ||
} | ||
} | ||
|
||
@RequestMapping(path = {"/buildInfo"}, method = {GET}, produces = {"application/json"}) | ||
@Timed | ||
public ResponseEntity<String> buildInfo() { | ||
return ResponseEntity.ok().cacheControl(CacheControl.noStore()).body(buildInfo); | ||
} | ||
|
||
} |
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
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,16 @@ | ||
package spring.boot.java.example; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.test.web.client.TestRestTemplate; | ||
import org.springframework.boot.test.web.server.LocalServerPort; | ||
import org.springframework.test.context.ActiveProfiles; | ||
|
||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) | ||
@ActiveProfiles({"dev"}) | ||
public class BaseAppTest { | ||
@Autowired | ||
protected TestRestTemplate restTemplate; | ||
@LocalServerPort | ||
protected int port = 0; | ||
} |
21 changes: 21 additions & 0 deletions
21
src/test/java/spring/boot/java/example/BuildInfoControllerTest.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,21 @@ | ||
package spring.boot.java.example; | ||
|
||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.Map; | ||
|
||
import static java.lang.String.format; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
|
||
public class BuildInfoControllerTest extends BaseAppTest { | ||
|
||
@Test | ||
public void testBuildInfo() { | ||
var response = restTemplate.getForObject(format("http://127.0.0.1:%d/buildInfo", port), Map.class); | ||
assertNotNull(response.get("version")); | ||
assertNotNull(response.get("branch")); | ||
assertNotNull(response.get("build-time")); | ||
} | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
src/test/java/spring/boot/java/example/ProbeControllerTest.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,25 @@ | ||
package spring.boot.java.example; | ||
|
||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.Map; | ||
|
||
import static java.lang.String.format; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
public class ProbeControllerTest extends BaseAppTest { | ||
|
||
@Test | ||
public void testLivenessProbe() { | ||
var response = restTemplate.getForObject(format("http://127.0.0.1:%d/probe/live", port), Map.class); | ||
assertEquals("I'm alive!", response.get("message")); | ||
} | ||
|
||
@Test | ||
public void testReadinessProbe() { | ||
var response = restTemplate.getForObject(format("http://127.0.0.1:%d/probe/ready", port), Map.class); | ||
assertEquals("I'm ready!", response.get("message")); | ||
} | ||
|
||
} |
19 changes: 19 additions & 0 deletions
19
src/test/java/spring/boot/java/example/RootControllerTest.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,19 @@ | ||
package spring.boot.java.example; | ||
|
||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.Map; | ||
|
||
import static java.lang.String.format; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
public class RootControllerTest extends BaseAppTest { | ||
|
||
@Test | ||
public void testRootEndPoint() { | ||
var response = restTemplate.getForObject(format("http://127.0.0.1:%d/", port), Map.class); | ||
assertEquals("It works on my machine!", response.get("message")); | ||
} | ||
|
||
} |