/
ScheduleControllerIntegrationTest.java
93 lines (78 loc) · 2.91 KB
/
ScheduleControllerIntegrationTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package org.openlmis.referencedata.web;
import com.jayway.restassured.RestAssured;
import guru.nidi.ramltester.RamlDefinition;
import guru.nidi.ramltester.RamlLoaders;
import guru.nidi.ramltester.junit.RamlMatchers;
import guru.nidi.ramltester.restassured.RestAssuredClient;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.openlmis.Application;
import org.openlmis.referencedata.domain.Period;
import org.openlmis.referencedata.domain.Schedule;
import org.openlmis.referencedata.repository.PeriodRepository;
import org.openlmis.referencedata.repository.ScheduleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.boot.test.WebIntegrationTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.time.LocalDate;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(Application.class)
@WebIntegrationTest("server.port:8080")
public class ScheduleControllerIntegrationTest {
private static final String BASE_URL = System.getenv("BASE_URL");
private static final String RAML_ASSERT_MESSAGE = "HTTP request/response should match RAML "
+ "definition.";
@Autowired
private ScheduleRepository scheduleRepository;
@Autowired
private PeriodRepository periodRepository;
private RamlDefinition ramlDefinition;
private RestAssuredClient restAssured;
private Schedule schedule;
private Period period;
/** Prepare the test environment. */
@Before
public void setUp() {
RestAssured.baseURI = BASE_URL;
ramlDefinition = RamlLoaders.fromClasspath().load("api-definition-raml.yaml");
restAssured = ramlDefinition.createRestAssured();
cleanup();
schedule = new Schedule();
schedule.setCode("code");
schedule.setName("schedule");
schedule.setDescription("Test schedule");
scheduleRepository.save(schedule);
period = new Period();
period.setName("period");
period.setProcessingSchedule(schedule);
period.setDescription("Test period");
period.setStartDate(LocalDate.of(2016, 1, 1));
period.setEndDate(LocalDate.of(2016, 2, 1));
periodRepository.save(period);
}
/**
* Cleanup the test environment.
*/
@After
public void cleanup() {
periodRepository.deleteAll();
scheduleRepository.deleteAll();
}
@Test
public void testGetTotalDifference() {
String response = restAssured.given()
.pathParam("id", schedule.getId())
.when()
.get("/api/schedules/{id}/difference")
.then()
.statusCode(200)
.extract().asString();
assertTrue(response.contains("Period lasts 1 months and 0 days"));
assertThat(RAML_ASSERT_MESSAGE , restAssured.getLastReport(), RamlMatchers.hasNoViolations());
}
}