1
1
package de .filefighter .rest .domain .health .rest ;
2
2
3
+ import com .fasterxml .jackson .core .JsonProcessingException ;
4
+ import com .fasterxml .jackson .databind .JsonNode ;
5
+ import com .fasterxml .jackson .databind .ObjectMapper ;
6
+ import com .jayway .jsonpath .internal .filter .ValueNode ;
3
7
import de .filefighter .rest .domain .filesystem .data .persistance .FileSystemRepository ;
4
8
import de .filefighter .rest .domain .token .data .persistance .AccessTokenRepository ;
5
9
import de .filefighter .rest .domain .user .data .persistance .UserEntitiy ;
16
20
import org .springframework .boot .web .server .LocalServerPort ;
17
21
18
22
import static org .assertj .core .api .Assertions .assertThat ;
23
+ import static org .junit .Assert .assertEquals ;
19
24
import static org .junit .Assert .assertTrue ;
20
25
21
26
@ SpringBootTest (webEnvironment = WebEnvironment .RANDOM_PORT )
22
27
public class SystemHealthRestIntegrationTest {
23
28
24
- private final Logger LOG = LoggerFactory .getLogger (SystemHealthRestIntegrationTest .class );
25
-
26
29
@ LocalServerPort
27
30
private int port ;
28
31
29
- @ Autowired
30
- private TestRestTemplate restTemplate ;
31
-
32
- @ Autowired
33
- private UserRepository userRepository ;
34
-
35
- @ Autowired
36
- private FileSystemRepository fileSystemRepository ;
32
+ private final Logger LOG = LoggerFactory .getLogger (SystemHealthRestIntegrationTest .class );
33
+ private final ObjectMapper objectMapper ;
34
+ private final TestRestTemplate restTemplate ;
35
+ private final UserRepository userRepository ;
36
+ private final FileSystemRepository fileSystemRepository ;
37
+ private final AccessTokenRepository accessTokenRepository ;
37
38
38
39
@ Autowired
39
- private AccessTokenRepository accessTokenRepository ;
40
-
40
+ public SystemHealthRestIntegrationTest (TestRestTemplate restTemplate , UserRepository userRepository , FileSystemRepository fileSystemRepository , AccessTokenRepository accessTokenRepository ) {
41
+ this .objectMapper = new ObjectMapper ();
42
+ this .restTemplate = restTemplate ;
43
+ this .userRepository = userRepository ;
44
+ this .fileSystemRepository = fileSystemRepository ;
45
+ this .accessTokenRepository = accessTokenRepository ;
46
+ }
41
47
42
48
@ BeforeEach
43
- public void cleanDbs (){
49
+ public void cleanDbs () {
44
50
LOG .info ("Cleaning Databases." );
45
51
userRepository .deleteAll ();
46
52
fileSystemRepository .deleteAll ();
47
53
accessTokenRepository .deleteAll ();
48
54
}
49
55
50
56
@ Test
51
- public void healthCheckShouldReturnUptime () {
52
- LOG .info ("Running: healthCheckShouldReturnUptime" );
53
- String jsonString = this .restTemplate .getForObject ("http://localhost:" + port + "/health" , String .class );
54
- assertTrue (jsonString .contains ("uptimeInSeconds" ));
55
- }
57
+ public void healthCheckShouldContainVariablesAndCorrectValues () throws JsonProcessingException {
58
+ LOG .info ("Preloading default admin user: " + userRepository .save (new UserEntitiy (0L , "admin" , "admin" , "refreshToken1234" , 0 , 1 )));
56
59
57
- @ Test
58
- public void healthCheckShouldReturnUserCount () {
59
- LOG .info ("Running: healthCheckShouldReturnUserCount" );
60
60
String jsonString = this .restTemplate .getForObject ("http://localhost:" + port + "/health" , String .class );
61
- assertTrue (jsonString .contains ("userCount" ));
62
- }
63
61
64
- /*@Test
65
- public void healthCheckShouldReturnCorrectUserCount() {
66
- LOG.info("Running: healthCheckShouldReturnCorrectUserCount");
67
- LOG.info("Preloading default admin user: " + userRepository.save(new UserEntitiy(0L, "admin", "admin", "refreshToken1234", 0, 1)));
68
- String jsonString = this.restTemplate.getForObject("http://localhost:" + port + "/health", String.class);
69
- assertTrue(jsonString.contains("userCount") && jsonString.contains(":1"));
70
- }*/
62
+ // Note when a key does not exist, a NullPointerException will be thrown.
63
+ JsonNode root = objectMapper .readTree (jsonString );
64
+ String uptime = root .get ("uptimeInSeconds" ).asText ();
65
+ String userCount = root .get ("userCount" ).asText ();
66
+
67
+ assertTrue (Integer .parseInt (uptime ) > 0 );
68
+ assertEquals (1 , Integer .parseInt (userCount ));
69
+ }
71
70
}
0 commit comments