-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from DiSSCo/feature/additional-unit-tests
Add unit tests and integration tests
- Loading branch information
Showing
11 changed files
with
355 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/test/java/eu/dissco/core/digitalspecimenprocessor/repository/BaseRepositoryIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package eu.dissco.core.digitalspecimenprocessor.repository; | ||
|
||
import static org.testcontainers.containers.PostgreSQLContainer.IMAGE; | ||
|
||
import com.zaxxer.hikari.HikariDataSource; | ||
import org.flywaydb.core.Flyway; | ||
import org.jooq.DSLContext; | ||
import org.jooq.SQLDialect; | ||
import org.jooq.impl.DefaultDSLContext; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.testcontainers.containers.PostgreSQLContainer; | ||
import org.testcontainers.junit.jupiter.Container; | ||
import org.testcontainers.junit.jupiter.Testcontainers; | ||
import org.testcontainers.utility.DockerImageName; | ||
|
||
@Testcontainers | ||
public class BaseRepositoryIT { | ||
|
||
private static final DockerImageName POSTGIS = | ||
DockerImageName.parse("postgres:13.7").asCompatibleSubstituteFor(IMAGE); | ||
|
||
@Container | ||
private static final PostgreSQLContainer<?> CONTAINER = new PostgreSQLContainer<>(POSTGIS); | ||
protected DSLContext context; | ||
private HikariDataSource dataSource; | ||
|
||
@BeforeEach | ||
void prepareDatabase() { | ||
dataSource = new HikariDataSource(); | ||
dataSource.setJdbcUrl(CONTAINER.getJdbcUrl()); | ||
dataSource.setUsername(CONTAINER.getUsername()); | ||
dataSource.setPassword(CONTAINER.getPassword()); | ||
dataSource.setMaximumPoolSize(1); | ||
dataSource.setConnectionInitSql(CONTAINER.getTestQueryString()); | ||
Flyway.configure().mixed(true).dataSource(dataSource).load().migrate(); | ||
context = new DefaultDSLContext(dataSource, SQLDialect.POSTGRES); | ||
} | ||
|
||
@AfterEach | ||
void disposeDataSource() { | ||
dataSource.close(); | ||
} | ||
} |
123 changes: 123 additions & 0 deletions
123
.../java/eu/dissco/core/digitalspecimenprocessor/repository/DigitalSpecimenRepositoryIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package eu.dissco.core.digitalspecimenprocessor.repository; | ||
|
||
import static eu.dissco.core.digitalspecimenprocessor.database.jooq.Tables.NEW_DIGITAL_SPECIMEN; | ||
import static eu.dissco.core.digitalspecimenprocessor.utils.TestUtils.HANDLE; | ||
import static eu.dissco.core.digitalspecimenprocessor.utils.TestUtils.MAPPER; | ||
import static eu.dissco.core.digitalspecimenprocessor.utils.TestUtils.PHYSICAL_SPECIMEN_ID; | ||
import static eu.dissco.core.digitalspecimenprocessor.utils.TestUtils.givenDigitalSpecimenRecord; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.mockito.Mockito.mockStatic; | ||
|
||
import java.sql.BatchUpdateException; | ||
import java.time.Instant; | ||
import java.util.List; | ||
import org.jooq.Record1; | ||
import org.jooq.exception.DataAccessException; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.MockedStatic; | ||
import org.postgresql.util.PSQLException; | ||
|
||
class DigitalSpecimenRepositoryIT extends BaseRepositoryIT { | ||
|
||
private static final Instant UPDATED_TIMESTAMP = Instant.parse("2022-11-02T13:05:24.00Z"); | ||
|
||
|
||
private DigitalSpecimenRepository repository; | ||
|
||
@BeforeEach | ||
void setup() { | ||
repository = new DigitalSpecimenRepository(context, MAPPER); | ||
} | ||
|
||
@AfterEach | ||
void destroy() { | ||
context.truncate(NEW_DIGITAL_SPECIMEN).execute(); | ||
} | ||
|
||
@Test | ||
void testGetDigitalSpecimensEmpty() { | ||
// Given | ||
|
||
// When | ||
var result = repository.getDigitalSpecimens(List.of(PHYSICAL_SPECIMEN_ID)); | ||
|
||
// Then | ||
assertThat(result).isEmpty(); | ||
} | ||
|
||
@Test | ||
void testGetDigitalSpecimens() { | ||
// Given | ||
repository.createDigitalSpecimenRecord( | ||
List.of( | ||
givenDigitalSpecimenRecord(), | ||
givenDigitalSpecimenRecord("20.5000.1025/XXX-XXX-XXX", "TEST_1"), | ||
givenDigitalSpecimenRecord("20.5000.1025/YYY-YYY-YYY", "TEST_2"))); | ||
|
||
// When | ||
var result = repository.getDigitalSpecimens(List.of(PHYSICAL_SPECIMEN_ID)); | ||
|
||
// Then | ||
assertThat(result.get(0)).isEqualTo(givenDigitalSpecimenRecord()); | ||
} | ||
|
||
@Test | ||
void testUpdateVersionSpecimens() { | ||
// Given | ||
repository.createDigitalSpecimenRecord( | ||
List.of( | ||
givenDigitalSpecimenRecord(), | ||
givenDigitalSpecimenRecord("20.5000.1025/XXX-XXX-XXX", "TEST_1"), | ||
givenDigitalSpecimenRecord("20.5000.1025/YYY-YYY-YYY", "TEST_2"))); | ||
|
||
// When | ||
var result = repository.createDigitalSpecimenRecord(List.of(givenDigitalSpecimenRecord(2))); | ||
|
||
// Then | ||
assertThat(result).isEqualTo(new int[]{1}); | ||
} | ||
|
||
@Test | ||
void testUpdateLastChecked() { | ||
// Given | ||
repository.createDigitalSpecimenRecord( | ||
List.of( | ||
givenDigitalSpecimenRecord(), | ||
givenDigitalSpecimenRecord("20.5000.1025/XXX-XXX-XXX", "TEST_1"), | ||
givenDigitalSpecimenRecord("20.5000.1025/YYY-YYY-YYY", "TEST_2"))); | ||
|
||
// When | ||
try (MockedStatic<Instant> mockedStatic = mockStatic(Instant.class)) { | ||
mockedStatic.when(Instant::now).thenReturn(UPDATED_TIMESTAMP); | ||
repository.updateLastChecked(List.of(HANDLE)); | ||
} | ||
|
||
// Then | ||
var result = context.select(NEW_DIGITAL_SPECIMEN.LAST_CHECKED) | ||
.from(NEW_DIGITAL_SPECIMEN) | ||
.where(NEW_DIGITAL_SPECIMEN.ID.eq(HANDLE)).fetchOne(Record1::value1); | ||
assertThat(result).isEqualTo(UPDATED_TIMESTAMP); | ||
} | ||
|
||
@Test | ||
void testInsertDuplicateSpecimens() { | ||
// Given | ||
var records = List.of( | ||
givenDigitalSpecimenRecord(), | ||
givenDigitalSpecimenRecord("20.5000.1025/XXX-XXX-XXX", "TEST_1"), | ||
givenDigitalSpecimenRecord("20.5000.1025/XXX-XXX-XXX", "TEST_2")); | ||
|
||
// When | ||
var exception = assertThrows(DataAccessException.class, () -> { | ||
repository.createDigitalSpecimenRecord(records); | ||
}); | ||
|
||
// Then | ||
assertThat(exception).hasCauseInstanceOf(BatchUpdateException.class).hasRootCauseInstanceOf( | ||
PSQLException.class); | ||
} | ||
|
||
} |
74 changes: 74 additions & 0 deletions
74
src/test/java/eu/dissco/core/digitalspecimenprocessor/repository/HandleRepositoryIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package eu.dissco.core.digitalspecimenprocessor.repository; | ||
|
||
import static eu.dissco.core.digitalspecimenprocessor.database.jooq.Tables.HANDLES; | ||
import static eu.dissco.core.digitalspecimenprocessor.utils.TestUtils.CREATED; | ||
import static eu.dissco.core.digitalspecimenprocessor.utils.TestUtils.HANDLE; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import eu.dissco.core.digitalspecimenprocessor.domain.HandleAttribute; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.List; | ||
import org.jooq.Record1; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class HandleRepositoryIT extends BaseRepositoryIT { | ||
|
||
private HandleRepository repository; | ||
|
||
@BeforeEach | ||
void setup() { | ||
repository = new HandleRepository(context); | ||
} | ||
|
||
@AfterEach | ||
void destroy() { | ||
context.truncate(HANDLES).execute(); | ||
} | ||
|
||
@Test | ||
void testCreateHandle() { | ||
// Given | ||
var handleAttributes = givenHandleAttributes(); | ||
|
||
// When | ||
repository.createHandle(HANDLE, CREATED, handleAttributes); | ||
|
||
// Then | ||
var handles = context.selectFrom(HANDLES).fetch(); | ||
assertThat(handles).hasSize(handleAttributes.size()); | ||
} | ||
|
||
@Test | ||
void testUpdateHandleAttributes(){ | ||
// Given | ||
var handleAttributes = givenHandleAttributes(); | ||
repository.createHandle(HANDLE, CREATED, handleAttributes); | ||
var updatedHandle = new HandleAttribute(11, "pidKernelMetadataLicense", | ||
"anotherLicenseType".getBytes(StandardCharsets.UTF_8)); | ||
|
||
// When | ||
repository.updateHandleAttributes(HANDLE, CREATED, List.of(updatedHandle)); | ||
|
||
// Then | ||
var result = context.select(HANDLES.DATA) | ||
.from(HANDLES) | ||
.where(HANDLES.HANDLE.eq(HANDLE.getBytes(StandardCharsets.UTF_8))) | ||
.and(HANDLES.TYPE.eq("issueNumber".getBytes(StandardCharsets.UTF_8))) | ||
.fetchOne(Record1::value1); | ||
assertThat(result).isEqualTo("2".getBytes(StandardCharsets.UTF_8)); | ||
} | ||
|
||
private List<HandleAttribute> givenHandleAttributes() { | ||
return List.of( | ||
new HandleAttribute(1, "pid", | ||
("https://hdl.handle.net/" + HANDLE).getBytes(StandardCharsets.UTF_8)), | ||
new HandleAttribute(11, "pidKernelMetadataLicense", | ||
"https://creativecommons.org/publicdomain/zero/1.0/".getBytes(StandardCharsets.UTF_8)), | ||
new HandleAttribute(7, "issueNumber", "1".getBytes(StandardCharsets.UTF_8)), | ||
new HandleAttribute(100, "HS_ADMIN", "TEST_ADMIN_STRING".getBytes(StandardCharsets.UTF_8)) | ||
); | ||
} | ||
|
||
} |
Oops, something went wrong.