Skip to content
Snippets Groups Projects
Commit a70f3b3e authored by Samuel Eickelberg's avatar Samuel Eickelberg
Browse files

OP#18070 Removed MongoDB entirely. Cahcing is now also based on JPA and H2. We...

OP#18070 Removed MongoDB entirely. Cahcing is now also based on JPA and H2. We don't want to burden "standalone" GEMIMEG users to having to install a MOngoDB instance locally.
parent 3d99802f
Branches
No related tags found
No related merge requests found
Showing
with 94 additions and 55 deletions
...@@ -62,7 +62,7 @@ ...@@ -62,7 +62,7 @@
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId> <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.mapstruct</groupId> <groupId>org.mapstruct</groupId>
...@@ -197,12 +197,6 @@ ...@@ -197,12 +197,6 @@
<artifactId>xmlunit-matchers</artifactId> <artifactId>xmlunit-matchers</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>de.flapdoodle.embed</groupId>
<artifactId>de.flapdoodle.embed.mongo.spring3x</artifactId>
<version>${de.flapdoodle.embed.mongo.version}</version>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
<build> <build>
......
...@@ -43,7 +43,7 @@ public class CacheController { ...@@ -43,7 +43,7 @@ public class CacheController {
@Operation(description = "Gets the cached item by its ID.") @Operation(description = "Gets the cached item by its ID.")
@GetMapping(path = BASE_PATH_ID, produces = APPLICATION_JSON_VALUE) @GetMapping(path = BASE_PATH_ID, produces = APPLICATION_JSON_VALUE)
public ReadResponseDto findById(@PathVariable(name = ID_PARAM_NAME) String id) { public ReadResponseDto findById(@PathVariable(name = ID_PARAM_NAME) Long id) {
return service.findById(id) return service.findById(id)
.orElseThrow(() -> new NotFoundStatus("Cached file with ID " + id + " could not be found.")); .orElseThrow(() -> new NotFoundStatus("Cached file with ID " + id + " could not be found."));
} }
......
package de.ptb.common.dcc.data; package de.ptb.common.dcc.data;
import de.ptb.common.dcc.model.CacheItem; import de.ptb.common.dcc.model.CacheItem;
import org.springframework.data.mongodb.repository.MongoRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
@Repository @Repository
public interface CacheItemRepository extends MongoRepository<CacheItem, String> { public interface CacheItemRepository extends JpaRepository<CacheItem, Long> {
List<CacheItem> findByCreatedAtLessThan(Date createdAt); List<CacheItem> findByCreatedAtLessThan(Date createdAt);
} }
package de.ptb.common.dcc.data; package de.ptb.common.dcc.data;
import de.ptb.common.dcc.model.CalibrationCertificate; import de.ptb.common.dcc.model.CalibrationCertificate;
import org.springframework.data.mongodb.repository.MongoRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
@Repository @Repository
public interface CalibrationCertificateRepository extends MongoRepository<CalibrationCertificate, String> { public interface CalibrationCertificateRepository extends JpaRepository<CalibrationCertificate, String> {
List<CalibrationCertificate> findByCreatedAtLessThan(Date createdAt); List<CalibrationCertificate> findByCreatedAtLessThan(Date createdAt);
} }
package de.ptb.common.dcc.model; package de.ptb.common.dcc.model;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Lob;
import jakarta.persistence.SequenceGenerator;
import jakarta.persistence.Table;
import lombok.Data; import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import java.util.Date; import java.util.Date;
@Data @Data
@Document @Entity
@Table(name = "CACHE")
public class CacheItem { public class CacheItem {
@Id @Id
private String id; @Column(name = "ID", nullable = false, unique = true)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "cached_item_seq_gen")
@SequenceGenerator(name = "cached_item_seq_gen", sequenceName = "cached_item_seq")
private Long id;
@Column(name = "CALLBACK_URL")
private String callbackUrl; private String callbackUrl;
@Column(name = "FILE_NAME", nullable = false)
private String fileName; private String fileName;
@Column(name = "MIME_TYPE", nullable = false)
private String mimeType; private String mimeType;
@Lob
@Column(name = "DATA", nullable = false)
private byte[] fileContent; private byte[] fileContent;
@Column(name = "CREATED_AT", nullable = false)
private Date createdAt; private Date createdAt;
} }
package de.ptb.common.dcc.model; package de.ptb.common.dcc.model;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.Data; import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import java.util.Date; import java.util.Date;
@Data @Data
@Document @Entity
@Table(name = "DCC")
public class CalibrationCertificate { public class CalibrationCertificate {
@Id @Id
@Column(name = "ID", nullable = false, unique = true)
private String id; private String id;
@Column(name = "DCC_JSON", nullable = false)
private String dccJson; private String dccJson;
@Column(name = "CREATED_AT", nullable = false)
private Date createdAt; private Date createdAt;
} }
...@@ -38,12 +38,12 @@ public class CacheService { ...@@ -38,12 +38,12 @@ public class CacheService {
cacheItem.setMimeType(request.getMimeType()); cacheItem.setMimeType(request.getMimeType());
cacheItem = repository.save(cacheItem); cacheItem = repository.save(cacheItem);
StoreResponseDto response = new StoreResponseDto(); StoreResponseDto response = new StoreResponseDto();
response.setRetrievalUrl(BASE_PATH_ID.replace(ID_PLACEHOLDER, cacheItem.getId())); response.setRetrievalUrl(BASE_PATH_ID.replace(ID_PLACEHOLDER, cacheItem.getId().toString()));
return response; return response;
} }
@Nonnull @Nonnull
public Optional<ReadResponseDto> findById(@Nonnull String id) { public Optional<ReadResponseDto> findById(@Nonnull Long id) {
Optional<CacheItem> cacheItem = repository.findById(id); Optional<CacheItem> cacheItem = repository.findById(id);
if (cacheItem.isPresent()) { if (cacheItem.isPresent()) {
ReadResponseDto readResponse = new ReadResponseDto(); ReadResponseDto readResponse = new ReadResponseDto();
......
...@@ -13,18 +13,26 @@ spring: ...@@ -13,18 +13,26 @@ spring:
mvc: mvc:
pathmatch: pathmatch:
matching-strategy: ant_path_matcher matching-strategy: ant_path_matcher
datasource:
url: jdbc:h2:file:./${spring.application.name}
username: sa
password: password
driverClassName: org.h2.Driver
jpa:
database: default
hibernate:
ddl-auto: update
database-platform: org.hibernate.dialect.H2Dialect
open-in-view: true
h2:
console.enabled: true
console:
path: /h2-console
settings.trace: false
settings.web-allow-others: false
main: main:
lazy-initialization: true lazy-initialization: true
allow-bean-definition-overriding: true allow-bean-definition-overriding: true
data:
mongodb:
database: ${spring.application.name}
port: 27017
de:
flapdoodle:
mongodb:
embedded:
version: 6.0.4
common: common:
security: security:
enabled: ${server.ssl.enabled} enabled: ${server.ssl.enabled}
......
...@@ -7,7 +7,7 @@ import org.junit.jupiter.api.BeforeEach; ...@@ -7,7 +7,7 @@ import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.data.mongo.DataMongoTest; import org.springframework.boot.test.autoconfigure.data.mongo.DataMongoTest;
import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Date; import java.util.Date;
...@@ -15,7 +15,7 @@ import java.util.List; ...@@ -15,7 +15,7 @@ import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
@DataMongoTest @DataJpaTest
class CacheItemRepositoryTest { class CacheItemRepositoryTest {
private final static String XML = """ private final static String XML = """
...@@ -31,9 +31,6 @@ class CacheItemRepositoryTest { ...@@ -31,9 +31,6 @@ class CacheItemRepositoryTest {
@Autowired @Autowired
private CacheConfiguration configuration; private CacheConfiguration configuration;
@Autowired
private MongoTemplate mongoTemplate;
private final long currentTimeMillis = System.currentTimeMillis(); private final long currentTimeMillis = System.currentTimeMillis();
@BeforeEach @BeforeEach
...@@ -44,13 +41,13 @@ class CacheItemRepositoryTest { ...@@ -44,13 +41,13 @@ class CacheItemRepositoryTest {
expired.setFileName("FDH_expired.xml"); expired.setFileName("FDH_expired.xml");
expired.setFileContent(XML.getBytes(StandardCharsets.UTF_8)); expired.setFileContent(XML.getBytes(StandardCharsets.UTF_8));
expired.setCreatedAt(new Date(expiredTimeMillis)); expired.setCreatedAt(new Date(expiredTimeMillis));
mongoTemplate.insert(expired);
CacheItem notExpired = new CacheItem(); CacheItem notExpired = new CacheItem();
notExpired.setMimeType("application/xml"); notExpired.setMimeType("application/xml");
notExpired.setFileName("FDH_notExpired.xml"); notExpired.setFileName("FDH_notExpired.xml");
notExpired.setFileContent(XML.getBytes(StandardCharsets.UTF_8)); notExpired.setFileContent(XML.getBytes(StandardCharsets.UTF_8));
notExpired.setCreatedAt(new Date(currentTimeMillis)); notExpired.setCreatedAt(new Date(currentTimeMillis));
mongoTemplate.insert(notExpired); repository.saveAll(List.of(expired, notExpired));
assertEquals(2, repository.count());
} }
@Test @Test
...@@ -62,6 +59,7 @@ class CacheItemRepositoryTest { ...@@ -62,6 +59,7 @@ class CacheItemRepositoryTest {
@AfterEach @AfterEach
void tearDown() { void tearDown() {
mongoTemplate.getDb().drop(); repository.deleteAll();
assertEquals(0, repository.count());
} }
} }
\ No newline at end of file
...@@ -7,13 +7,14 @@ import org.junit.jupiter.api.BeforeEach; ...@@ -7,13 +7,14 @@ import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.data.mongo.DataMongoTest; import org.springframework.boot.test.autoconfigure.data.mongo.DataMongoTest;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
@DataMongoTest @DataJpaTest
class CalibrationCertificateRepositoryTest { class CalibrationCertificateRepositoryTest {
@Autowired @Autowired
......
...@@ -8,6 +8,7 @@ import de.ptb.common.dcc.config.CacheConfiguration; ...@@ -8,6 +8,7 @@ import de.ptb.common.dcc.config.CacheConfiguration;
import de.ptb.common.dcc.config.VersionConfiguration; import de.ptb.common.dcc.config.VersionConfiguration;
import de.ptb.common.dcc.data.CacheItemRepository; import de.ptb.common.dcc.data.CacheItemRepository;
import de.ptb.common.dcc.model.CacheItem; import de.ptb.common.dcc.model.CacheItem;
import org.apache.commons.lang3.RandomUtils;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -17,7 +18,6 @@ import org.springframework.boot.test.mock.mockito.MockBean; ...@@ -17,7 +18,6 @@ import org.springframework.boot.test.mock.mockito.MockBean;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Base64; import java.util.Base64;
import java.util.Optional; import java.util.Optional;
import java.util.UUID;
import static de.ptb.common.dcc.api.v1.CacheControllerRoutes.BASE_PATH_ID; import static de.ptb.common.dcc.api.v1.CacheControllerRoutes.BASE_PATH_ID;
import static de.ptb.common.dcc.api.v1.CacheControllerRoutes.ID_PLACEHOLDER; import static de.ptb.common.dcc.api.v1.CacheControllerRoutes.ID_PLACEHOLDER;
...@@ -49,13 +49,15 @@ class CacheServiceTest { ...@@ -49,13 +49,15 @@ class CacheServiceTest {
private CacheService service; private CacheService service;
private CacheItem cacheItem; private CacheItem cacheItem;
private final Long cacheItemId = RandomUtils.nextLong();
@BeforeEach @BeforeEach
void setUp() { void setUp() {
when(versionConfiguration.getArtifactId()).thenReturn("cache-service-test"); when(versionConfiguration.getArtifactId()).thenReturn("cache-service-test");
when(versionConfiguration.getVersion()).thenReturn("1.0.0"); when(versionConfiguration.getVersion()).thenReturn("1.0.0");
when(configuration.getPersistLifespan()).thenReturn(600); when(configuration.getPersistLifespan()).thenReturn(600);
cacheItem = new CacheItem(); cacheItem = new CacheItem();
cacheItem.setId(UUID.randomUUID().toString()); cacheItem.setId(cacheItemId);
cacheItem.setMimeType("text/plain"); cacheItem.setMimeType("text/plain");
cacheItem.setFileName("test.txt"); cacheItem.setFileName("test.txt");
cacheItem.setCallbackUrl("http://test/callbackAction"); cacheItem.setCallbackUrl("http://test/callbackAction");
...@@ -73,7 +75,7 @@ class CacheServiceTest { ...@@ -73,7 +75,7 @@ class CacheServiceTest {
when(repository.save(any())).thenReturn(cacheItem); when(repository.save(any())).thenReturn(cacheItem);
StoreResponseDto actual = service.store(request); StoreResponseDto actual = service.store(request);
assertNotNull(actual); assertNotNull(actual);
assertEquals(BASE_PATH_ID.replace(ID_PLACEHOLDER, cacheItem.getId()), actual.getRetrievalUrl()); assertEquals(BASE_PATH_ID.replace(ID_PLACEHOLDER, cacheItem.getId().toString()), actual.getRetrievalUrl());
} }
@Test @Test
...@@ -91,11 +93,11 @@ class CacheServiceTest { ...@@ -91,11 +93,11 @@ class CacheServiceTest {
@Test @Test
void findById_NotFound() { void findById_NotFound() {
when(repository.findById("missingId")).thenReturn(Optional.empty()); when(repository.findById(cacheItemId + 1)).thenReturn(Optional.empty());
Optional<ReadResponseDto> actual = service.findById("missingId"); Optional<ReadResponseDto> actual = service.findById(cacheItemId + 1);
assertNotNull(actual); assertNotNull(actual);
assertFalse(actual.isPresent()); assertFalse(actual.isPresent());
verify(repository).findById("missingId"); verify(repository).findById(cacheItemId + 1);
verifyNoMoreInteractions(repository); verifyNoMoreInteractions(repository);
} }
} }
\ No newline at end of file
...@@ -16,18 +16,26 @@ spring: ...@@ -16,18 +16,26 @@ spring:
mvc: mvc:
pathmatch: pathmatch:
matching-strategy: ant_path_matcher matching-strategy: ant_path_matcher
datasource:
url: jdbc:h2:file:./${spring.application.name}-test
username: sa
password: password
driverClassName: org.h2.Driver
jpa:
database: default
hibernate:
ddl-auto: update
database-platform: org.hibernate.dialect.H2Dialect
open-in-view: true
h2:
console.enabled: true
console:
path: /h2-console
settings.trace: false
settings.web-allow-others: false
main: main:
lazy-initialization: true lazy-initialization: true
allow-bean-definition-overriding: true allow-bean-definition-overriding: true
data:
mongodb:
database: ${spring.application.name}
port: 27017
de:
flapdoodle:
mongodb:
embedded:
version: 6.0.4
common: common:
security: security:
enabled: ${server.ssl.enabled} enabled: ${server.ssl.enabled}
......
...@@ -23,7 +23,6 @@ ...@@ -23,7 +23,6 @@
<properties> <properties>
<dcc-namespace-uri>https://ptb.de/dcc</dcc-namespace-uri> <dcc-namespace-uri>https://ptb.de/dcc</dcc-namespace-uri>
<byte-buddy.version>1.15.1</byte-buddy.version> <byte-buddy.version>1.15.1</byte-buddy.version>
<de.flapdoodle.embed.mongo.version>4.18.0</de.flapdoodle.embed.mongo.version>
</properties> </properties>
<!-- SCM settings --> <!-- SCM settings -->
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment