Browse Source

Implemented getSystem for oparl service

master
Jim Martens 10 months ago
parent
commit
df412e6659
  1. 1
      oparl-server/oparl-server.gradle
  2. 3
      oparl-server/src/main/java/de/twomartens/oparlservice/entity/dto/System.java
  3. 24
      oparl-server/src/main/java/de/twomartens/oparlservice/entity/internal/System.java
  4. 65
      oparl-server/src/main/java/de/twomartens/oparlservice/repository/SystemRepository.java
  5. 35
      oparl-server/src/main/java/de/twomartens/oparlservice/service/OParlService.java
  6. 121
      oparl-server/src/test/java/de/twomartens/oparlservice/service/OParlServiceTest.java

1
oparl-server/oparl-server.gradle

@ -26,6 +26,7 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.boot:spring-boot-starter-websocket'
implementation 'org.springframework.boot:spring-boot-starter-log4j2'
implementation 'org.springframework.data:spring-data-jdbc'
implementation 'io.micrometer:micrometer-registry-prometheus'
implementation 'org.springdoc:springdoc-openapi-ui:1.3.9'

3
oparl-server/src/main/java/de/twomartens/oparlservice/entity/dto/System.java

@ -37,9 +37,6 @@ public class System {
@Schema(description = "URL to the license")
private String license;
@Schema(description = "Categorization of the object")
private String keyword;
@Schema(description = "URL of a website that presents the object in the browser")
private String web;

24
oparl-server/src/main/java/de/twomartens/oparlservice/entity/internal/System.java

@ -0,0 +1,24 @@
package de.twomartens.oparlservice.entity.internal;
import lombok.*;
import java.time.ZonedDateTime;
@Getter
@ToString
@EqualsAndHashCode
@RequiredArgsConstructor
public class System {
private final int id;
private final String license;
private final String name;
private final String contactEmail;
private final String contactName;
private final String website;
private final String vendor;
private final String product;
private final String web;
private final ZonedDateTime created;
private final ZonedDateTime modified;
private final boolean deleted;
}

65
oparl-server/src/main/java/de/twomartens/oparlservice/repository/SystemRepository.java

@ -0,0 +1,65 @@
package de.twomartens.oparlservice.repository;
import de.twomartens.oparlservice.entity.internal.System;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import java.util.Optional;
@Repository
public class SystemRepository implements CrudRepository<System, Integer> {
@Override
public <S extends System> S save(S entity) {
return null;
}
@Override
public <S extends System> Iterable<S> saveAll(Iterable<S> entities) {
return null;
}
@Override
public Optional<System> findById(Integer integer) {
return Optional.empty();
}
@Override
public boolean existsById(Integer integer) {
return false;
}
@Override
public Iterable<System> findAll() {
return null;
}
@Override
public Iterable<System> findAllById(Iterable<Integer> integers) {
return null;
}
@Override
public long count() {
return 0;
}
@Override
public void deleteById(Integer integer) {
}
@Override
public void delete(System entity) {
}
@Override
public void deleteAll(Iterable<? extends System> entities) {
}
@Override
public void deleteAll() {
}
}

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

@ -3,17 +3,25 @@ package de.twomartens.oparlservice.service;
import de.twomartens.oparlservice.configs.OParlServiceProperties;
import de.twomartens.oparlservice.entity.dto.*;
import de.twomartens.oparlservice.entity.dto.System;
import de.twomartens.oparlservice.repository.SystemRepository;
import org.springframework.stereotype.Service;
import java.time.ZonedDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.StreamSupport;
@Service
public class OParlService {
private final OParlServiceProperties properties;
private final SystemRepository systemRepository;
public OParlService(OParlServiceProperties properties) {
public OParlService(OParlServiceProperties properties,
SystemRepository systemRepository) {
this.properties = properties;
this.systemRepository = systemRepository;
}
public Optional<ObjectList<AgendaItem>> getAgendaItemsInBody(String bodyID, int pageNumber) {
@ -113,6 +121,29 @@ public class OParlService {
}
public System getSystem() {
return null;
Iterable<de.twomartens.oparlservice.entity.internal.System> systems = systemRepository.findAll();
de.twomartens.oparlservice.entity.internal.System firstNotDeletedSystem = StreamSupport
.stream(systems.spliterator(), false)
.filter(system -> !system.isDeleted())
.findFirst().orElseThrow();
return System.builder()
.id(String.valueOf(firstNotDeletedSystem.getId()))
.type("https://schema.oparl.org/1.1/Schema")
.created(firstNotDeletedSystem.getCreated())
.modified(firstNotDeletedSystem.getModified())
.oparlVersion("https://schema.oparl.org/1.1/")
.body(properties.getUrl() + "/bodies")
.license(firstNotDeletedSystem.getLicense())
.name(firstNotDeletedSystem.getName())
.contactEmail(firstNotDeletedSystem.getContactEmail())
.contactName(firstNotDeletedSystem.getContactName())
.deleted(firstNotDeletedSystem.isDeleted())
.product(firstNotDeletedSystem.getProduct())
.vendor(firstNotDeletedSystem.getVendor())
.web(firstNotDeletedSystem.getWeb())
.website(firstNotDeletedSystem.getWebsite())
.otherOparlVersions(Collections.emptyList())
.build();
}
}

121
oparl-server/src/test/java/de/twomartens/oparlservice/service/OParlServiceTest.java

@ -0,0 +1,121 @@
package de.twomartens.oparlservice.service;
import de.twomartens.oparlservice.configs.OParlServiceProperties;
import de.twomartens.oparlservice.entity.internal.System;
import de.twomartens.oparlservice.repository.SystemRepository;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.time.ZonedDateTime;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
class OParlServiceTest {
private OParlServiceProperties properties;
private OParlService service;
private SystemRepository systemRepository;
@BeforeEach
void setUp() {
properties = new OParlServiceProperties();
properties.setUrl("http://localhost");
systemRepository = mock(SystemRepository.class);
service = new OParlService(properties, systemRepository);
}
@Test
void shouldAnnotateSystemWithURLs() {
System internalSystem = new System(0, "https://creativecommons.org/cc-by",
"OParl-Schnittstelle", "admin@2martens.de", "Jim Martens", null, null,
null, null, ZonedDateTime.now(), ZonedDateTime.now(), false);
when(systemRepository.findAll()).thenReturn(List.of(internalSystem));
de.twomartens.oparlservice.entity.dto.System result = service.getSystem();
assertNotNull(result);
assertEquals(String.valueOf(0), result.getId());
assertEquals("https://schema.oparl.org/1.1/Schema", result.getType());
assertEquals("https://schema.oparl.org/1.1/", result.getOparlVersion());
assertEquals("http://localhost/bodies", result.getBody());
}
@Test
void shouldPickFirstSystem_whenMultipleSystemsAvailable() {
System internalSystem1 = new System(0, "https://creativecommons.org/cc-by",
"OParl-Schnittstelle", "admin@2martens.de", "Jim Martens", null, null,
null, null, ZonedDateTime.now(), ZonedDateTime.now(), false);
System internalSystem2 = new System(1, "https://creativecommons.org/cc-by-sa",
"OParl-Baustelle", "admin@2martens.de", "Jim Martens", null, null,
null, null, ZonedDateTime.now(), ZonedDateTime.now(), false);
when(systemRepository.findAll()).thenReturn(List.of(internalSystem1, internalSystem2));
de.twomartens.oparlservice.entity.dto.System result = service.getSystem();
assertNotNull(result);
assertEquals(String.valueOf(0), result.getId());
assertEquals("https://creativecommons.org/cc-by", result.getLicense());
assertEquals("OParl-Schnittstelle", result.getName());
}
@Test
void shouldPickFirstNotDeletedSystem() {
System internalSystem1 = new System(0, "https://creativecommons.org/cc-by",
"OParl-Schnittstelle", "admin@2martens.de", "Jim Martens", null, null,
null, null, ZonedDateTime.now(), ZonedDateTime.now(), true);
System internalSystem2 = new System(1, "https://creativecommons.org/cc-by-sa",
"OParl-Baustelle", "admin@2martens.de", "Jim Martens", null, null,
null, null, ZonedDateTime.now(), ZonedDateTime.now(), false);
when(systemRepository.findAll()).thenReturn(List.of(internalSystem1, internalSystem2));
de.twomartens.oparlservice.entity.dto.System result = service.getSystem();
assertNotNull(result);
assertEquals(String.valueOf(1), result.getId());
assertEquals("https://creativecommons.org/cc-by-sa", result.getLicense());
assertEquals("OParl-Baustelle", result.getName());
assertFalse(result.isDeleted());
}
@Test
void shouldUseCorrectUrl() {
System internalSystem = new System(1, "https://creativecommons.org/cc-by-sa",
"OParl-Baustelle", "admin@2martens.de", "Jim Martens", null, null,
null, null, ZonedDateTime.now(), ZonedDateTime.now(), false);
when(systemRepository.findAll()).thenReturn(List.of(internalSystem));
de.twomartens.oparlservice.entity.dto.System result1 = service.getSystem();
properties.setUrl("http://test.com");
de.twomartens.oparlservice.entity.dto.System result2 = service.getSystem();
assertNotNull(result1);
assertNotNull(result2);
assertEquals(String.valueOf(1), result1.getId());
assertEquals(String.valueOf(1), result2.getId());
assertEquals("http://localhost/bodies", result1.getBody());
assertEquals("http://test.com/bodies", result2.getBody());
}
@Test
void shouldReturnNonValuesOfInternalEntity() {
System internalSystem = new System(1, "https://creativecommons.org/cc-by-sa",
"OParl-Baustelle", "admin@2martens.de", "Jim Martens",
"https://sitzungsdienst-eimsbuettel.hamburg.de/bi/allris.net.asp", "https://2martens.de",
"https://git.2martens.com/2martens/oparl-service", "http://example.com", ZonedDateTime.now(),
ZonedDateTime.now(),
false);
when(systemRepository.findAll()).thenReturn(List.of(internalSystem));
de.twomartens.oparlservice.entity.dto.System result = service.getSystem();
assertEquals("OParl-Baustelle", result.getName());
assertEquals("admin@2martens.de", result.getContactEmail());
assertEquals("Jim Martens", result.getContactName());
assertEquals("https://2martens.de", result.getVendor());
assertEquals("https://sitzungsdienst-eimsbuettel.hamburg.de/bi/allris.net.asp", result.getWebsite());
assertEquals("https://git.2martens.com/2martens/oparl-service", result.getProduct());
assertEquals("http://example.com", result.getWeb());
}
}
Loading…
Cancel
Save