Browse Source

Extended controller and created test

master
Jim Martens 10 months ago
parent
commit
6774035fcd
  1. 109
      oparl-server/src/main/java/de/twomartens/oparlservice/control/OParlController.java
  2. 50
      oparl-server/src/main/java/de/twomartens/oparlservice/service/OParlService.java
  3. 382
      oparl-server/src/test/java/de/twomartens/oparlservice/control/OParlControllerTest.java

109
oparl-server/src/main/java/de/twomartens/oparlservice/control/OParlController.java

@ -1,8 +1,6 @@
package de.twomartens.oparlservice.control;
import de.twomartens.oparlservice.configs.OParlServiceProperties;
import de.twomartens.oparlservice.entity.Body;
import de.twomartens.oparlservice.entity.Organization;
import de.twomartens.oparlservice.entity.*;
import de.twomartens.oparlservice.entity.System;
import de.twomartens.oparlservice.service.OParlService;
import io.swagger.v3.oas.annotations.Operation;
@ -13,7 +11,6 @@ import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Collections;
import java.util.List;
@Slf4j
@ -50,13 +47,113 @@ public class OParlController {
return service.getBody(id);
}
@GetMapping("/term/{id}")
@Operation(summary = "information about legislative term", description = "returns information about the requested legislative term")
public LegislativeTerm legislativeTerm(
@PathVariable
@Parameter(description = "legislative term ID", example = "21")
String id) {
log.info("method invoked /v1.1/term/{}", id);
return service.getLegislativeTerm(id);
}
@GetMapping("/body/{id}/organizations")
@Operation(summary = "List of all organizations in body", description = "returns a list of all organizations in requested body")
public List<Organization> organizations(
public List<Organization> organizationsInBody(
@PathVariable
@Parameter(description = "body ID", example = "0")
String id) {
log.info("invoked method /v1.1/body/{}/organizations", id);
return Collections.emptyList();
return service.getOrganizationsInBody(id);
}
@GetMapping("/organization/{id}")
@Operation(summary = "information about organization", description = "returns the requested organization")
public Organization organization(
@PathVariable
@Parameter(description = "organization ID", example = "0")
String id) {
log.info("invoked method /v1.1/organization/{}", id);
return service.getOrganization(id);
}
@GetMapping("/body/{id}/persons")
@Operation(summary = "List of all persons in body", description = "returns a list of all persons in requested body")
public List<Person> persons(
@PathVariable
@Parameter(description = "body ID", example = "0")
String id) {
log.info("invoked method /v1.1/body/{}/persons", id);
return service.getPersonsInBody(id);
}
@GetMapping("/person/{id}")
@Operation(summary = "information about person", description = "returns the requested person")
public Person person(
@PathVariable
@Parameter(description = "person ID", example = "0")
String id) {
log.info("invoked method /v1.1/person/{}", id);
return service.getPerson(id);
}
@GetMapping("/body/{id}/memberships")
@Operation(summary = "List of all memberships in body", description = "returns a list of all memberships in requested body")
public List<Membership> membershipsInBody(
@PathVariable
@Parameter(description = "body ID", example = "0")
String id) {
log.info("invoked method /v1.1/body/{}/memberships", id);
return service.getMembershipsInBody(id);
}
@GetMapping("/organization/{id}/memberships")
@Operation(summary = "List of all memberships in organization", description = "returns a list of all memberships in requested organization")
public List<Membership> membershipsInOrganization(
@PathVariable
@Parameter(description = "organization ID", example = "0")
String id) {
log.info("invoked method /v1.1/organization/{}/memberships", id);
return service.getMembershipsInOrganization(id);
}
@GetMapping("/membership/{id}")
@Operation(summary = "information about membership", description = "returns the requested membership")
public Membership membership(
@PathVariable
@Parameter(description = "membership ID", example = "0")
String id) {
log.info("invoked method /v1.1/membership/{}", id);
return service.getMembership(id);
}
@GetMapping("/body/{id}/meetings")
@Operation(summary = "List of all meetings in body", description = "returns a list of all meetings in requested body")
public List<Meeting> meetingsInBody(
@PathVariable
@Parameter(description = "body ID", example = "0")
String id) {
log.info("invoked method /v1.1/body/{}/meetings", id);
return service.getMeetingsInBody(id);
}
@GetMapping("/organization/{id}/meetings")
@Operation(summary = "List of all meetings in organization", description = "returns a list of all meetings in requested organization")
public List<Meeting> meetingsInOrganization(
@PathVariable
@Parameter(description = "organization ID", example = "0")
String id) {
log.info("invoked method /v1.1/organization/{}/meetings", id);
return service.getMeetingsInOrganization(id);
}
@GetMapping("/meeting/{id}")
@Operation(summary = "information about meeting", description = "returns the requested meeting")
public Meeting meeting(
@PathVariable
@Parameter(description = "meeting ID", example = "0")
String id) {
log.info("invoked method /v1.1/meeting/{}", id);
return service.getMeeting(id);
}
}

50
oparl-server/src/main/java/de/twomartens/oparlservice/service/OParlService.java

@ -1,19 +1,19 @@
package de.twomartens.oparlservice.service;
import de.twomartens.oparlservice.configs.OParlServiceProperties;
import de.twomartens.oparlservice.entity.Body;
import de.twomartens.oparlservice.entity.LegislativeTerm;
import de.twomartens.oparlservice.entity.*;
import de.twomartens.oparlservice.entity.System;
import org.springframework.stereotype.Service;
import java.time.LocalDate;
import java.time.ZonedDateTime;
import java.util.Collections;
import java.util.List;
@Service
public class OParlService {
private OParlServiceProperties properties;
private final OParlServiceProperties properties;
private final Body exampleBody;
private final LegislativeTerm exampleTerm;
private final System system;
@ -81,6 +81,50 @@ public class OParlService {
return exampleBody;
}
public LegislativeTerm getLegislativeTerm(String id) {
return exampleTerm;
}
public Meeting getMeeting(String id) {
return null;
}
public List<Meeting> getMeetingsInBody(String bodyID) {
return Collections.emptyList();
}
public List<Meeting> getMeetingsInOrganization(String organizationID) {
return Collections.emptyList();
}
public Membership getMembership(String id) {
return null;
}
public List<Membership> getMembershipsInBody(String bodyID) {
return Collections.emptyList();
}
public List<Membership> getMembershipsInOrganization(String organizationID) {
return Collections.emptyList();
}
public List<Organization> getOrganizationsInBody(String bodyID) {
return Collections.emptyList();
}
public Organization getOrganization(String id) {
return null;
}
public List<Person> getPersonsInBody(String bodyID) {
return Collections.emptyList();
}
public Person getPerson(String id) {
return null;
}
public System getSystem() {
return system;
}

382
oparl-server/src/test/java/de/twomartens/oparlservice/control/OParlControllerTest.java

@ -0,0 +1,382 @@
package de.twomartens.oparlservice.control;
import de.twomartens.oparlservice.entity.System;
import de.twomartens.oparlservice.entity.*;
import de.twomartens.oparlservice.service.OParlService;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.BDDMockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import java.time.LocalDate;
import java.time.ZonedDateTime;
import java.util.Collections;
import java.util.List;
@WebMvcTest(OParlController.class)
class OParlControllerTest {
@Autowired
private MockMvc mvc;
@MockBean
private OParlService service;
private Body testBody;
private LegislativeTerm testTerm;
private Meeting testMeeting;
private Membership testMembership;
private Organization testOrganization;
private Organization testPartyOrganization;
private Person testPerson;
private System testSystem;
@BeforeEach
void setUp() {
initializeTestValues();
}
@Test
void shouldReturnSystemInfo() throws Exception {
BDDMockito.given(service.getSystem())
.willReturn(testSystem);
mvc.perform(MockMvcRequestBuilders.get("/v1.1/")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("$", Matchers.aMapWithSize(14)))
.andExpect(MockMvcResultMatchers.jsonPath("$.id", Matchers.equalTo("/v1.1/")))
.andExpect(MockMvcResultMatchers.jsonPath("$.type", Matchers.equalTo("https://schema.oparl.org/1.1/System")))
.andExpect(MockMvcResultMatchers.jsonPath("$.deleted", Matchers.equalTo(false)))
.andReturn();
}
@Test
void shouldReturnListOfBodies() throws Exception {
BDDMockito.given(service.getBodies())
.willReturn(List.of(testBody));
mvc.perform(MockMvcRequestBuilders.get("/v1.1/bodies")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("$", Matchers.hasSize(1)))
.andExpect(MockMvcResultMatchers.jsonPath("$[0]", Matchers.aMapWithSize(24)))
.andExpect(MockMvcResultMatchers.jsonPath("$[0].type", Matchers.equalTo("https://schema.oparl.org/1.1/Body")))
.andExpect(MockMvcResultMatchers.jsonPath("$[0].deleted", Matchers.equalTo(false)))
.andReturn();
}
@Test
void shouldReturnBody() throws Exception {
BDDMockito.given(service.getBody("0"))
.willReturn(testBody);
mvc.perform(MockMvcRequestBuilders.get("/v1.1/body/0")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("$", Matchers.aMapWithSize(24)))
.andExpect(MockMvcResultMatchers.jsonPath("$.type", Matchers.equalTo("https://schema.oparl.org/1.1/Body")))
.andExpect(MockMvcResultMatchers.jsonPath("$.deleted", Matchers.equalTo(false)))
.andReturn();
}
@Test
void shouldReturnLegislativeTerm() throws Exception {
BDDMockito.given(service.getLegislativeTerm("21"))
.willReturn(testTerm);
mvc.perform(MockMvcRequestBuilders.get("/v1.1/term/21")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("$", Matchers.aMapWithSize(9)))
.andExpect(MockMvcResultMatchers.jsonPath("$", Matchers.hasKey("body")))
.andExpect(MockMvcResultMatchers.jsonPath("$.type", Matchers.equalTo("https://schema.oparl.org/1.1/LegislativeTerm")))
.andExpect(MockMvcResultMatchers.jsonPath("$.deleted", Matchers.equalTo(false)))
.andReturn();
}
@Test
void shouldReturnOrganizationsInBody() throws Exception {
BDDMockito.given(service.getOrganizationsInBody("0"))
.willReturn(List.of(testOrganization, testPartyOrganization));
mvc.perform(MockMvcRequestBuilders.get("/v1.1/body/0/organizations")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("$", Matchers.hasSize(2)))
.andExpect(MockMvcResultMatchers.jsonPath("$[0]", Matchers.aMapWithSize(15)))
.andExpect(MockMvcResultMatchers.jsonPath("$[0].type", Matchers.equalTo("https://schema.oparl.org/1.1/Organization")))
.andExpect(MockMvcResultMatchers.jsonPath("$[0].deleted", Matchers.equalTo(false)))
.andReturn();
}
@Test
void shouldReturnOrganization() throws Exception {
BDDMockito.given(service.getOrganization("0"))
.willReturn(testOrganization);
mvc.perform(MockMvcRequestBuilders.get("/v1.1/organization/0")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("$", Matchers.aMapWithSize(15)))
.andExpect(MockMvcResultMatchers.jsonPath("$.type", Matchers.equalTo("https://schema.oparl.org/1.1/Organization")))
.andExpect(MockMvcResultMatchers.jsonPath("$.deleted", Matchers.equalTo(false)))
.andReturn();
}
@Test
void shouldReturnPersonsInBody() throws Exception {
BDDMockito.given(service.getPersonsInBody("0"))
.willReturn(List.of(testPerson));
mvc.perform(MockMvcRequestBuilders.get("/v1.1/body/0/persons")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("$", Matchers.hasSize(1)))
.andExpect(MockMvcResultMatchers.jsonPath("$[0]", Matchers.aMapWithSize(13)))
.andExpect(MockMvcResultMatchers.jsonPath("$[0].type", Matchers.equalTo("https://schema.oparl.org/1.1/Person")))
.andExpect(MockMvcResultMatchers.jsonPath("$[0].deleted", Matchers.equalTo(false)))
.andReturn();
}
@Test
void shouldReturnPerson() throws Exception {
BDDMockito.given(service.getPerson("0"))
.willReturn(testPerson);
mvc.perform(MockMvcRequestBuilders.get("/v1.1/person/0")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("$", Matchers.aMapWithSize(13)))
.andExpect(MockMvcResultMatchers.jsonPath("$.type", Matchers.equalTo("https://schema.oparl.org/1.1/Person")))
.andExpect(MockMvcResultMatchers.jsonPath("$.deleted", Matchers.equalTo(false)))
.andReturn();
}
@Test
void shouldReturnMembershipsInBody() throws Exception {
BDDMockito.given(service.getMembershipsInBody("0"))
.willReturn(List.of(testMembership));
mvc.perform(MockMvcRequestBuilders.get("/v1.1/body/0/memberships")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("$", Matchers.hasSize(1)))
.andExpect(MockMvcResultMatchers.jsonPath("$[0]", Matchers.aMapWithSize(11)))
.andExpect(MockMvcResultMatchers.jsonPath("$[0].type", Matchers.equalTo("https://schema.oparl.org/1.1/Membership")))
.andExpect(MockMvcResultMatchers.jsonPath("$[0].deleted", Matchers.equalTo(false)))
.andReturn();
}
@Test
void shouldReturnMembershipsInOrganization() throws Exception {
BDDMockito.given(service.getMembershipsInOrganization("0"))
.willReturn(List.of(testMembership));
mvc.perform(MockMvcRequestBuilders.get("/v1.1/organization/0/memberships")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("$", Matchers.hasSize(1)))
.andExpect(MockMvcResultMatchers.jsonPath("$[0]", Matchers.aMapWithSize(11)))
.andExpect(MockMvcResultMatchers.jsonPath("$[0].type", Matchers.equalTo("https://schema.oparl.org/1.1/Membership")))
.andExpect(MockMvcResultMatchers.jsonPath("$[0].deleted", Matchers.equalTo(false)))
.andReturn();
}
@Test
void shouldReturnMembership() throws Exception {
BDDMockito.given(service.getMembership("0"))
.willReturn(testMembership);
mvc.perform(MockMvcRequestBuilders.get("/v1.1/membership/0")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("$", Matchers.aMapWithSize(11)))
.andExpect(MockMvcResultMatchers.jsonPath("$", Matchers.hasKey("person")))
.andExpect(MockMvcResultMatchers.jsonPath("$.type", Matchers.equalTo("https://schema.oparl.org/1.1/Membership")))
.andExpect(MockMvcResultMatchers.jsonPath("$.deleted", Matchers.equalTo(false)))
.andReturn();
}
@Test
void shouldReturnMeetingsInBody() throws Exception {
BDDMockito.given(service.getMeetingsInBody("0"))
.willReturn(List.of(testMeeting));
mvc.perform(MockMvcRequestBuilders.get("/v1.1/body/0/meetings")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("$", Matchers.hasSize(1)))
.andExpect(MockMvcResultMatchers.jsonPath("$[0]", Matchers.aMapWithSize(8)))
.andExpect(MockMvcResultMatchers.jsonPath("$[0].type", Matchers.equalTo("https://schema.oparl.org/1.1/Meeting")))
.andExpect(MockMvcResultMatchers.jsonPath("$[0].deleted", Matchers.equalTo(false)))
.andReturn();
}
@Test
void shouldReturnMeetingsInOrganization() throws Exception {
BDDMockito.given(service.getMeetingsInOrganization("0"))
.willReturn(List.of(testMeeting));
mvc.perform(MockMvcRequestBuilders.get("/v1.1/organization/0/meetings")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("$", Matchers.hasSize(1)))
.andExpect(MockMvcResultMatchers.jsonPath("$[0]", Matchers.aMapWithSize(8)))
.andExpect(MockMvcResultMatchers.jsonPath("$[0].type", Matchers.equalTo("https://schema.oparl.org/1.1/Meeting")))
.andExpect(MockMvcResultMatchers.jsonPath("$[0].deleted", Matchers.equalTo(false)))
.andReturn();
}
@Test
void shouldReturnMeeting() throws Exception {
BDDMockito.given(service.getMeeting("0"))
.willReturn(testMeeting);
mvc.perform(MockMvcRequestBuilders.get("/v1.1/meeting/0")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("$", Matchers.aMapWithSize(8)))
.andExpect(MockMvcResultMatchers.jsonPath("$.type", Matchers.equalTo("https://schema.oparl.org/1.1/Meeting")))
.andExpect(MockMvcResultMatchers.jsonPath("$.deleted", Matchers.equalTo(false)))
.andReturn();
}
void initializeTestValues() {
testSystem = System.builder()
.id("/v1.1/")
.type("https://schema.oparl.org/1.1/System")
.license("https://www.govdata.de/dl-de/by-2-0")
.created(ZonedDateTime.now())
.modified(ZonedDateTime.now())
.oparlVersion("https://schema.oparl.org/1.1/")
.name("OParl interface for ALLRIS of Hamburg Eimsbüttel")
.contactEmail("github@2martens.de")
.contactName("Jim Martens")
.website("https://sitzungsdienst-eimsbuettel.hamburg.de/bi/")
.vendor("https://2martens.de")
.product("https://git.2martens.de/2martens/oparl-service")
.body("/bodies")
.deleted(false)
.build();
testTerm = LegislativeTerm.builder()
.id("/v1.1/term/21")
.type("https://schema.oparl.org/1.1/LegislativeTerm")
.body("/v1.1/body/0")
.name("21. Wahlperiode")
.startDate(LocalDate.parse("2019-05-27"))
.endDate(LocalDate.parse("2024-05-26"))
.created(ZonedDateTime.now())
.modified(ZonedDateTime.now())
.deleted(false)
.build();
testBody = Body.builder()
.id("/v1.1/body/0")
.type("https://schema.oparl.org/1.1/Body")
.created(ZonedDateTime.now())
.modified(ZonedDateTime.now())
.shortName("Hamburg")
.name("Bezirk Eimsbüttel")
.system("/v1.1")
.legislativeTerm(List.of(testTerm))
.website("https://www.hamburg.de/eimsbuettel")
.ags("02000000")
.rgs("020000000000")
.equivalent(List.of("http://dbpedia.org/page/Eimsb%C3%BCttel", "http://d-nb.info/1208293575"))
.organization("/v1.1/organizations/0")
.person("/v1.1/persons/0")
.meeting("/v1.1/meetings/0")
.paper("/v1.1/papers/0")
.agendaItem("/v1.1/agendaItems/0")
.consultation("/v1.1/consultations/0")
.file("/v1.1/files/0")
.locationList("/v1.1/locations/0")
.legislativeTermList("/v1.1/terms")
.membership("/v1.1/memberships/0")
.classification("Bezirk")
.deleted(false)
.build();
testOrganization = Organization.builder()
.id("/v1.1/organization/0")
.type("https://schema.oparl.org/1.1/Organization")
.created(ZonedDateTime.now())
.modified(ZonedDateTime.now())
.body("/v1.1/body/0")
.name("Bezirksversammlung Eimsbüttel")
.shortName("Bezirksversammlung")
.membership(Collections.emptyList())
.meeting("/v1.1/organization/0/meetings")
.consultation("/v1.1/organization/0/consultations")
.post(List.of(
"Vorsitzende/r der Bezirksversammlung",
"stellvertretende/r Vorsitzende/r der Bezirksversammlung",
"Mitglied der Bezirksversammlung"
))
.organizationType(OrganizationType.BODY)
.classification("Parlament")
.startDate(LocalDate.parse("2019-06-06"))
.deleted(false)
.build();
testPartyOrganization = Organization.builder()
.id("/v1.1/organization/1")
.type("https://schema.oparl.org/1.1/Organization")
.created(ZonedDateTime.now())
.modified(ZonedDateTime.now())
.deleted(false)
.body("/v1.1/body/0")
.name("GRÜNE Hamburg")
.shortName("GRÜNE")
.membership(Collections.emptyList())
.meeting("/v1.1/organization/1/meetings")
.consultation("/v1.1/organization/1/consultations")
.post(Collections.emptyList())
.organizationType(OrganizationType.PARTY)
.classification("Partei")
.build();
testPerson = Person.builder()
.id("/v1.1/person/0")
.type("https://schema.oparl.org/1.1/Person")
.created(ZonedDateTime.now())
.modified(ZonedDateTime.now())
.body("/v1.1/body/0")
.name("Erika Mustermann")
.familyName("Mustermann")
.givenName("Erika")
.formOfAddress("Frau")
.gender("female")
.status(List.of("Mitglied in der Bezirksversammlung"))
.membership(Collections.emptyList())
.deleted(false)
.build();
testMembership = Membership.builder()
.id("/v1.1/membership/0")
.type("https://schema.oparl.org/1.1/Membership")
.created(ZonedDateTime.now())
.modified(ZonedDateTime.now())
.deleted(false)
.person("/v1.1/person/0")
.organization("/v1.1/organization/0")
.role("Mitglied der Bezirksversammlung")
.votingRight(true)
.startDate(LocalDate.parse("2019-06-06"))
.onBehalfOf("/v1.1/organization/1")
.build();
testMeeting = Meeting.builder()
.id("/v1.1/meeting/0")
.type("https://schema.oparl.org/1.1/Meeting")
.created(ZonedDateTime.now())
.modified(ZonedDateTime.now())
.deleted(false)
.name("Konstituierende Sitzung der Bezirksversammlung")
.meetingState(MeetingState.FINISHED)
.cancelled(false)
.build();
}
}
Loading…
Cancel
Save