Skip to content
This repository was archived by the owner on Apr 5, 2024. It is now read-only.

Commit 8f858a3

Browse files
committed
FF-37 finished adding unit tests.
1 parent 3a05ea9 commit 8f858a3

11 files changed

+105
-34
lines changed

src/main/java/de/filefighter/rest/health/business/SystemHealthModelAssembler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package de.filefighter.rest.health.business;
22

33
import de.filefighter.rest.health.data.SystemHealth;
4-
import de.filefighter.rest.health.rest.HealthRestController;
4+
import de.filefighter.rest.health.rest.SystemHealthRestController;
55
import org.jetbrains.annotations.NotNull;
66
import org.springframework.hateoas.EntityModel;
77
import org.springframework.hateoas.server.RepresentationModelAssembler;
@@ -16,6 +16,6 @@ public class SystemHealthModelAssembler implements RepresentationModelAssembler<
1616
@Override
1717
public @NotNull EntityModel<SystemHealth> toModel(@NotNull SystemHealth entity) {
1818
return EntityModel.of(entity,
19-
linkTo(methodOn(HealthRestController.class).getSystemHealthInfo()).withSelfRel());
19+
linkTo(methodOn(SystemHealthRestController.class).getSystemHealthInfo()).withSelfRel());
2020
}
2121
}
Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package de.filefighter.rest.health.data;
22

33
import lombok.Builder;
4-
import lombok.Data;
4+
import lombok.Getter;
55

6-
@Data
6+
/**
7+
* This class is a representation of the json model.
8+
*/
9+
10+
@Getter
711
@Builder(buildMethodName = "create")
812
public class SystemHealth {
9-
private long uptimeInSeconds;
13+
private final long uptimeInSeconds;
1014
}

src/main/java/de/filefighter/rest/health/rest/HealthRestController.java renamed to src/main/java/de/filefighter/rest/health/rest/SystemHealthRestController.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,15 @@
44
import io.swagger.annotations.Api;
55
import org.springframework.hateoas.EntityModel;
66
import org.springframework.web.bind.annotation.GetMapping;
7-
import org.springframework.web.bind.annotation.RequestMapping;
87
import org.springframework.web.bind.annotation.RestController;
98

109
@RestController
1110
@Api(value = "System Health", tags = { "SystemHealth" })
12-
public class HealthRestController {
11+
public class SystemHealthRestController {
1312

14-
private final HealthRestInterface healthRestService;
13+
private final SystemHealthRestInterface healthRestService;
1514

16-
public HealthRestController(HealthRestInterface healthRestService) {
15+
public SystemHealthRestController(SystemHealthRestInterface healthRestService) {
1716
this.healthRestService = healthRestService;
1817
}
1918

src/main/java/de/filefighter/rest/health/rest/HealthRestInterface.java renamed to src/main/java/de/filefighter/rest/health/rest/SystemHealthRestInterface.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
import de.filefighter.rest.health.data.SystemHealth;
44
import org.springframework.hateoas.EntityModel;
55

6-
public interface HealthRestInterface {
6+
public interface SystemHealthRestInterface {
77
EntityModel<SystemHealth> getSystemHealth();
88
}

src/main/java/de/filefighter/rest/health/rest/HealthRestService.java renamed to src/main/java/de/filefighter/rest/health/rest/SystemHealthRestService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
import org.springframework.stereotype.Service;
88

99
@Service
10-
public class HealthRestService implements HealthRestInterface{
10+
public class SystemHealthRestService implements SystemHealthRestInterface {
1111

1212
private final SystemHealthBusinessService systemHealthBusinessService;
1313
private final SystemHealthModelAssembler systemHealthModelAssembler;
1414

15-
public HealthRestService(SystemHealthBusinessService systemHealthBusinessService, SystemHealthModelAssembler systemHealthModelAssembler) {
15+
public SystemHealthRestService(SystemHealthBusinessService systemHealthBusinessService, SystemHealthModelAssembler systemHealthModelAssembler) {
1616
this.systemHealthBusinessService = systemHealthBusinessService;
1717
this.systemHealthModelAssembler = systemHealthModelAssembler;
1818
}

src/main/java/de/filefighter/rest/rest/ServerResponse.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package de.filefighter.rest.rest;
22

33
import lombok.Data;
4+
import lombok.Getter;
45
import org.springframework.hateoas.EntityModel;
56

6-
@Data
7+
@Getter
78
public class ServerResponse {
8-
private String message;
9-
private String status;
9+
private final String message;
10+
private final String status;
1011

1112
public ServerResponse(String status, String message) {
1213
this.status = status;

src/test/java/de/filefighter/rest/health/business/SystemHealthBusinessServiceUnitTest.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package de.filefighter.rest.health.business;
22

33
import de.filefighter.rest.health.data.SystemHealth;
4-
import org.junit.Before;
54
import org.junit.jupiter.api.BeforeAll;
65
import org.junit.jupiter.api.Test;
76

@@ -11,10 +10,10 @@
1110

1211
class SystemHealthBusinessServiceUnitTest {
1312

14-
private SystemHealthBusinessService systemHealthBusinessService;
13+
private static SystemHealthBusinessService systemHealthBusinessService;
1514

16-
@Before
17-
public void setUp(){
15+
@BeforeAll
16+
static void setUp() {
1817
systemHealthBusinessService = new SystemHealthBusinessService();
1918
}
2019

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package de.filefighter.rest.health.business;
22

33
import de.filefighter.rest.health.data.SystemHealth;
4-
import org.junit.Before;
54
import org.junit.jupiter.api.BeforeAll;
65
import org.junit.jupiter.api.Test;
76
import org.springframework.hateoas.EntityModel;
@@ -10,10 +9,10 @@
109

1110
class SystemHealthModelAssemblerUnitTest {
1211

13-
private SystemHealthModelAssembler systemHealthModelAssembler;
12+
private static SystemHealthModelAssembler systemHealthModelAssembler;
1413

15-
@Before
16-
public void setUp(){
14+
@BeforeAll
15+
static void setUp(){
1716
systemHealthModelAssembler = new SystemHealthModelAssembler();
1817
}
1918

@@ -24,6 +23,6 @@ void toModel() {
2423

2524
assertEquals(systemHealth, systemHealthEntityModel.getContent());
2625
assertTrue(systemHealthEntityModel.getLink("self").isPresent());
27-
assertTrue(systemHealthEntityModel.getLink("ugabuga").isEmpty());
26+
assertTrue(systemHealthEntityModel.getLink("thislinkdoesnotexist").isEmpty());
2827
}
2928
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package de.filefighter.rest.health.rest;
2+
3+
import de.filefighter.rest.health.business.SystemHealthBusinessService;
4+
import de.filefighter.rest.health.business.SystemHealthModelAssembler;
5+
import de.filefighter.rest.health.data.SystemHealth;
6+
import org.junit.jupiter.api.BeforeAll;
7+
import org.junit.jupiter.api.Test;
8+
import org.springframework.hateoas.EntityModel;
9+
10+
import static org.junit.jupiter.api.Assertions.*;
11+
import static org.mockito.Mockito.mock;
12+
import static org.mockito.Mockito.when;
13+
14+
class HealthRestServiceUnitTest {
15+
16+
private static final SystemHealthBusinessService systemHealthBusinessServiceMock = mock(SystemHealthBusinessService.class);
17+
private static final SystemHealthModelAssembler systemHealthModelAssemblerMock = mock(SystemHealthModelAssembler.class);
18+
private static SystemHealthRestService systemHealthRestService;
19+
20+
@BeforeAll
21+
public static void setUp() {
22+
systemHealthRestService = new SystemHealthRestService(systemHealthBusinessServiceMock, systemHealthModelAssemblerMock);
23+
}
24+
25+
@Test
26+
void getSystemHealth() {
27+
SystemHealth dummyHealth = SystemHealth.builder().uptimeInSeconds(420).create();
28+
EntityModel<SystemHealth> actualModell = EntityModel.of(dummyHealth);
29+
30+
when(systemHealthBusinessServiceMock.getCurrentSystemHealthInfo()).thenReturn(dummyHealth);
31+
when(systemHealthModelAssemblerMock.toModel(dummyHealth)).thenReturn(actualModell);
32+
33+
EntityModel<SystemHealth> expectedModel = systemHealthRestService.getSystemHealth();
34+
35+
assertEquals(expectedModel, actualModell);
36+
}
37+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package de.filefighter.rest.health.rest;
2+
3+
import de.filefighter.rest.rest.RestErrorController;
4+
import org.junit.jupiter.api.BeforeAll;
5+
import org.junit.jupiter.api.Test;
6+
import org.springframework.test.web.servlet.MockMvc;
7+
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
8+
9+
import static org.hamcrest.MatcherAssert.assertThat;
10+
import static org.junit.jupiter.api.Assertions.*;
11+
import static org.mockito.ArgumentMatchers.isA;
12+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
13+
import static org.mockito.Mockito.*;
14+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
15+
16+
class SystemHealthRestControllerUnitTest {
17+
18+
private static MockMvc mockMvc;
19+
private static SystemHealthRestService systemHealthRestServiceMock = mock(SystemHealthRestService.class);
20+
private static SystemHealthRestController systemHealthRestController;
21+
22+
@BeforeAll
23+
public static void setUp() {
24+
systemHealthRestController = new SystemHealthRestController(systemHealthRestServiceMock);
25+
mockMvc = MockMvcBuilders.standaloneSetup(systemHealthRestController).build();
26+
}
27+
28+
@Test
29+
void getSystemHealthInfo() throws Exception {
30+
mockMvc.perform(get("/health"))
31+
.andExpect(status().isOk())
32+
.andReturn();
33+
34+
verify(systemHealthRestServiceMock, times(1)).getSystemHealth();
35+
}
36+
}

0 commit comments

Comments
 (0)