Skip to content

Commit

Permalink
Merge pull request #7 from jecklgamis/next
Browse files Browse the repository at this point in the history
Build Info controller, app tests,
  • Loading branch information
jecklgamis authored Mar 19, 2024
2 parents d1ee8c0 + 1b1ad16 commit aaa6861
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ jobs:
restore-keys: ${{ runner.os }}-m2
- name: Generate keystore
run: ./generate-keystore.sh
- name: Generate build info
run: ./generate-build-info.sh
- name: Build with Maven
run: mvn --batch-mode --update-snapshots verify
- name: Extract metadata (tags, labels) for Docker
Expand Down
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ run-bash:
docker run -i -t $(IMAGE_NAME):$(IMAGE_TAG) /bin/bash
keystore:
@./generate-keystore.sh
build-info:
@./generate-build-info.sh
chart:
cd deployment/k8s/helm && make package
all: dist image chart
Expand Down
12 changes: 12 additions & 0 deletions generate-build-info.sh
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}
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.0</version>
<version>3.2.3</version>
</parent>

<repositories>
Expand Down Expand Up @@ -79,6 +79,11 @@
<artifactId>guava</artifactId>
<version>33.1.0-jre</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
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);
}

}
3 changes: 1 addition & 2 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ server:
enabled: true
port: 8443
ssl:
key-store: classpath:keystore.pfx
key-store-password: changeit
enabled: false

management:
endpoints:
Expand Down
16 changes: 16 additions & 0 deletions src/test/java/spring/boot/java/example/BaseAppTest.java
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;
}
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 src/test/java/spring/boot/java/example/ProbeControllerTest.java
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 src/test/java/spring/boot/java/example/RootControllerTest.java
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"));
}

}

0 comments on commit aaa6861

Please sign in to comment.