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 AgendaItem testItem; private Body testBody; private Consultation testConsultation; private File testFile; private LegislativeTerm testTerm; private Links testLinks; private Location testLocation; private Meeting testMeeting; private Membership testMembership; private Organization testOrganization; private Organization testPartyOrganization; private Pagination testPagination; private Paper testPaper; 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(ObjectList.builder() .data(List.of(testBody)) .pagination(testPagination) .links(testLinks) .build()); mvc.perform(MockMvcRequestBuilders.get("/v1.1/bodies") .contentType(MediaType.APPLICATION_JSON)) .andExpect(MockMvcResultMatchers.status().isOk()) .andExpect(MockMvcResultMatchers.jsonPath("$", Matchers.aMapWithSize(3))) .andExpect(MockMvcResultMatchers.jsonPath("$.data", Matchers.hasSize(1))) .andExpect(MockMvcResultMatchers.jsonPath("$.data[0]", Matchers.aMapWithSize(24))) .andExpect(MockMvcResultMatchers.jsonPath("$.data[0].type", Matchers.equalTo("https://schema.oparl.org/1.1/Body"))) .andExpect(MockMvcResultMatchers.jsonPath("$.data[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(ObjectList.builder().data(List.of(testOrganization, testPartyOrganization)).pagination(testPagination).links(testLinks).build()); mvc.perform(MockMvcRequestBuilders.get("/v1.1/body/0/organizations") .contentType(MediaType.APPLICATION_JSON)) .andExpect(MockMvcResultMatchers.status().isOk()) .andExpect(MockMvcResultMatchers.jsonPath("$", Matchers.aMapWithSize(3))) .andExpect(MockMvcResultMatchers.jsonPath("$.data", Matchers.hasSize(2))) .andExpect(MockMvcResultMatchers.jsonPath("$.data[0]", Matchers.aMapWithSize(15))) .andExpect(MockMvcResultMatchers.jsonPath("$.data[0].type", Matchers.equalTo("https://schema.oparl.org/1.1/Organization"))) .andExpect(MockMvcResultMatchers.jsonPath("$.data[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(ObjectList.builder().data(List.of(testPerson)).pagination(testPagination).links(testLinks).build()); mvc.perform(MockMvcRequestBuilders.get("/v1.1/body/0/persons") .contentType(MediaType.APPLICATION_JSON)) .andExpect(MockMvcResultMatchers.status().isOk()) .andExpect(MockMvcResultMatchers.jsonPath("$", Matchers.aMapWithSize(3))) .andExpect(MockMvcResultMatchers.jsonPath("$.data", Matchers.hasSize(1))) .andExpect(MockMvcResultMatchers.jsonPath("$.data[0]", Matchers.aMapWithSize(13))) .andExpect(MockMvcResultMatchers.jsonPath("$.data[0].type", Matchers.equalTo("https://schema.oparl.org/1.1/Person"))) .andExpect(MockMvcResultMatchers.jsonPath("$.data[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(ObjectList.builder().data(List.of(testMembership)).pagination(testPagination).links(testLinks).build()); mvc.perform(MockMvcRequestBuilders.get("/v1.1/body/0/memberships") .contentType(MediaType.APPLICATION_JSON)) .andExpect(MockMvcResultMatchers.status().isOk()) .andExpect(MockMvcResultMatchers.jsonPath("$", Matchers.aMapWithSize(3))) .andExpect(MockMvcResultMatchers.jsonPath("$.data", Matchers.hasSize(1))) .andExpect(MockMvcResultMatchers.jsonPath("$.data[0]", Matchers.aMapWithSize(11))) .andExpect(MockMvcResultMatchers.jsonPath("$.data[0].type", Matchers.equalTo("https://schema.oparl.org/1.1/Membership"))) .andExpect(MockMvcResultMatchers.jsonPath("$.data[0].deleted", Matchers.equalTo(false))) .andReturn(); } @Test void shouldReturnMembershipsInOrganization() throws Exception { BDDMockito.given(service.getMembershipsInOrganization("0")) .willReturn(ObjectList.builder().data(List.of(testMembership)).pagination(testPagination).links(testLinks).build()); mvc.perform(MockMvcRequestBuilders.get("/v1.1/organization/0/memberships") .contentType(MediaType.APPLICATION_JSON)) .andExpect(MockMvcResultMatchers.status().isOk()) .andExpect(MockMvcResultMatchers.jsonPath("$", Matchers.aMapWithSize(3))) .andExpect(MockMvcResultMatchers.jsonPath("$.data", Matchers.hasSize(1))) .andExpect(MockMvcResultMatchers.jsonPath("$.data[0]", Matchers.aMapWithSize(11))) .andExpect(MockMvcResultMatchers.jsonPath("$.data[0].type", Matchers.equalTo("https://schema.oparl.org/1.1/Membership"))) .andExpect(MockMvcResultMatchers.jsonPath("$.data[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(ObjectList.builder().data(List.of(testMeeting)).pagination(testPagination).links(testLinks).build()); mvc.perform(MockMvcRequestBuilders.get("/v1.1/body/0/meetings") .contentType(MediaType.APPLICATION_JSON)) .andExpect(MockMvcResultMatchers.status().isOk()) .andExpect(MockMvcResultMatchers.jsonPath("$", Matchers.aMapWithSize(3))) .andExpect(MockMvcResultMatchers.jsonPath("$.data", Matchers.hasSize(1))) .andExpect(MockMvcResultMatchers.jsonPath("$.data[0]", Matchers.aMapWithSize(9))) .andExpect(MockMvcResultMatchers.jsonPath("$.data[0].type", Matchers.equalTo("https://schema.oparl.org/1.1/Meeting"))) .andExpect(MockMvcResultMatchers.jsonPath("$.data[0].deleted", Matchers.equalTo(false))) .andReturn(); } @Test void shouldReturnMeetingsInOrganization() throws Exception { BDDMockito.given(service.getMeetingsInOrganization("0")) .willReturn(ObjectList.builder().data(List.of(testMeeting)).pagination(testPagination).links(testLinks).build()); mvc.perform(MockMvcRequestBuilders.get("/v1.1/organization/0/meetings") .contentType(MediaType.APPLICATION_JSON)) .andExpect(MockMvcResultMatchers.status().isOk()) .andExpect(MockMvcResultMatchers.jsonPath("$", Matchers.aMapWithSize(3))) .andExpect(MockMvcResultMatchers.jsonPath("$.data", Matchers.hasSize(1))) .andExpect(MockMvcResultMatchers.jsonPath("$.data[0]", Matchers.aMapWithSize(9))) .andExpect(MockMvcResultMatchers.jsonPath("$.data[0].type", Matchers.equalTo("https://schema.oparl.org/1.1/Meeting"))) .andExpect(MockMvcResultMatchers.jsonPath("$.data[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(9))) .andExpect(MockMvcResultMatchers.jsonPath("$.type", Matchers.equalTo("https://schema.oparl.org/1.1/Meeting"))) .andExpect(MockMvcResultMatchers.jsonPath("$.deleted", Matchers.equalTo(false))) .andReturn(); } @Test void shouldReturnAgendaItem() throws Exception { BDDMockito.given(service.getAgendaItem("0")) .willReturn(testItem); mvc.perform(MockMvcRequestBuilders.get("/v1.1/agendaItem/0") .contentType(MediaType.APPLICATION_JSON)) .andExpect(MockMvcResultMatchers.status().isOk()) .andExpect(MockMvcResultMatchers.jsonPath("$", Matchers.aMapWithSize(11))) .andExpect(MockMvcResultMatchers.jsonPath("$.type", Matchers.equalTo("https://schema.oparl.org/1.1/AgendaItem"))) .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(); testItem = AgendaItem.builder() .id("/v1.1/agendaItem/0") .type("https://schema.oparl.org/1.1/AgendaItem") .created(ZonedDateTime.now()) .modified(ZonedDateTime.now()) .deleted(false) .meeting("/v1.1/meeting/0") .number("1") .order(0) .name("Formalia") .isPublic(true) .consultation("/v1.1/consultation/0") .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) .agendaItem(List.of(testItem)) .build(); testPagination = Pagination.builder().build(); testLinks = Links.builder() .next("") .build(); } }