From d6bce7f9ec581c841050deddb721c1c1afb745dc Mon Sep 17 00:00:00 2001 From: Jan van Mansum Date: Fri, 8 Nov 2024 15:10:08 +0100 Subject: [PATCH] Added unit test for DAO --- pom.xml | 53 +++++++++++++ .../DdDataverseIngestConfiguration.java | 8 +- .../dans/dvingest/config/IngestConfig.java | 27 +++++++ .../nl/knaw/dans/dvingest/core/ImportJob.java | 60 +++++++++++++++ .../knaw/dans/dvingest/core/ImportTask.java | 22 ++++++ .../dans/dvingest/core/ImportTaskFactory.java | 25 +++++++ .../dvingest/core/ImportTaskProcessor.java | 22 ++++++ .../knaw/dans/dvingest/db/ImportJobDao.java | 74 +++++++++++++++++++ .../dvingest/resources/IngestApiResource.java | 27 +++++++ .../dans/dvingest/db/ImportJobDaoTest.java | 58 +++++++++++++++ 10 files changed, 375 insertions(+), 1 deletion(-) create mode 100644 src/main/java/nl/knaw/dans/dvingest/config/IngestConfig.java create mode 100644 src/main/java/nl/knaw/dans/dvingest/core/ImportJob.java create mode 100644 src/main/java/nl/knaw/dans/dvingest/core/ImportTask.java create mode 100644 src/main/java/nl/knaw/dans/dvingest/core/ImportTaskFactory.java create mode 100644 src/main/java/nl/knaw/dans/dvingest/core/ImportTaskProcessor.java create mode 100644 src/main/java/nl/knaw/dans/dvingest/db/ImportJobDao.java create mode 100644 src/main/java/nl/knaw/dans/dvingest/resources/IngestApiResource.java create mode 100644 src/test/java/nl/knaw/dans/dvingest/db/ImportJobDaoTest.java diff --git a/pom.xml b/pom.xml index 5967310..acfe3a2 100644 --- a/pom.xml +++ b/pom.xml @@ -49,6 +49,59 @@ io.dropwizard dropwizard-core + + io.dropwizard + dropwizard-hibernate + + + org.projectlombok + lombok + + + nl.knaw.dans + dans-dataverse-client-lib + + + nl.knaw.dans + dans-java-utils + + + nl.knaw.dans + dans-validation-lib + + + + io.dropwizard + dropwizard-testing + test + + + junit + junit + + + + + com.h2database + h2 + test + + + org.assertj + assertj-core + test + + + org.junit.jupiter + junit-jupiter-engine + test + + + org.mockito + mockito-core + test + + diff --git a/src/main/java/nl/knaw/dans/dvingest/config/DdDataverseIngestConfiguration.java b/src/main/java/nl/knaw/dans/dvingest/config/DdDataverseIngestConfiguration.java index fcde703..391b018 100644 --- a/src/main/java/nl/knaw/dans/dvingest/config/DdDataverseIngestConfiguration.java +++ b/src/main/java/nl/knaw/dans/dvingest/config/DdDataverseIngestConfiguration.java @@ -17,7 +17,13 @@ package nl.knaw.dans.dvingest.config; import io.dropwizard.core.Configuration; +import lombok.Data; +import lombok.EqualsAndHashCode; +import nl.knaw.dans.lib.util.DataverseClientFactory; +@Data +@EqualsAndHashCode(callSuper = true) public class DdDataverseIngestConfiguration extends Configuration { - + private DataverseClientFactory dataverse; + private IngestConfig ingest; } diff --git a/src/main/java/nl/knaw/dans/dvingest/config/IngestConfig.java b/src/main/java/nl/knaw/dans/dvingest/config/IngestConfig.java new file mode 100644 index 0000000..e4f808f --- /dev/null +++ b/src/main/java/nl/knaw/dans/dvingest/config/IngestConfig.java @@ -0,0 +1,27 @@ +/* + * Copyright (C) 2024 DANS - Data Archiving and Networked Services (info@dans.knaw.nl) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package nl.knaw.dans.dvingest.config; + +import lombok.Data; + +import java.nio.file.Path; + +@Data +public class IngestConfig { + private Path inbox; + private Path outbox; + private Path tempDir; +} diff --git a/src/main/java/nl/knaw/dans/dvingest/core/ImportJob.java b/src/main/java/nl/knaw/dans/dvingest/core/ImportJob.java new file mode 100644 index 0000000..cf62a31 --- /dev/null +++ b/src/main/java/nl/knaw/dans/dvingest/core/ImportJob.java @@ -0,0 +1,60 @@ +/* + * Copyright (C) 2024 DANS - Data Archiving and Networked Services (info@dans.knaw.nl) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package nl.knaw.dans.dvingest.core; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; +import lombok.ToString; +import nl.knaw.dans.validation.Uuid; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.Table; +import java.nio.file.Path; + +/** + * Description of an import job. + */ +@Entity +@Table(name = "import_job") +@Getter +@Setter +@ToString +@NoArgsConstructor +@AllArgsConstructor +public class ImportJob { + @GeneratedValue + @Id + private Long id; + + @Column(name = "creation_time") + private Long creationTime; + + @Column(name = "uuid") + @Uuid + private String uuid; + + @Column(name = "status") + private String status; + + @Column(name = "location") + private String location; +} diff --git a/src/main/java/nl/knaw/dans/dvingest/core/ImportTask.java b/src/main/java/nl/knaw/dans/dvingest/core/ImportTask.java new file mode 100644 index 0000000..e68e4b5 --- /dev/null +++ b/src/main/java/nl/knaw/dans/dvingest/core/ImportTask.java @@ -0,0 +1,22 @@ +/* + * Copyright (C) 2024 DANS - Data Archiving and Networked Services (info@dans.knaw.nl) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package nl.knaw.dans.dvingest.core; + +public interface ImportTask { + + + +} diff --git a/src/main/java/nl/knaw/dans/dvingest/core/ImportTaskFactory.java b/src/main/java/nl/knaw/dans/dvingest/core/ImportTaskFactory.java new file mode 100644 index 0000000..9b5ad67 --- /dev/null +++ b/src/main/java/nl/knaw/dans/dvingest/core/ImportTaskFactory.java @@ -0,0 +1,25 @@ +/* + * Copyright (C) 2024 DANS - Data Archiving and Networked Services (info@dans.knaw.nl) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package nl.knaw.dans.dvingest.core; + +import nl.knaw.dans.dvingest.api.ImportCommandDto; + +public class ImportTaskFactory { + + public ImportTask createImportTask(ImportCommandDto importCommandDto) { + return null; + } +} diff --git a/src/main/java/nl/knaw/dans/dvingest/core/ImportTaskProcessor.java b/src/main/java/nl/knaw/dans/dvingest/core/ImportTaskProcessor.java new file mode 100644 index 0000000..3ab9ef1 --- /dev/null +++ b/src/main/java/nl/knaw/dans/dvingest/core/ImportTaskProcessor.java @@ -0,0 +1,22 @@ +/* + * Copyright (C) 2024 DANS - Data Archiving and Networked Services (info@dans.knaw.nl) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package nl.knaw.dans.dvingest.core; + +public class ImportTaskProcessor { + + + +} diff --git a/src/main/java/nl/knaw/dans/dvingest/db/ImportJobDao.java b/src/main/java/nl/knaw/dans/dvingest/db/ImportJobDao.java new file mode 100644 index 0000000..c0ad471 --- /dev/null +++ b/src/main/java/nl/knaw/dans/dvingest/db/ImportJobDao.java @@ -0,0 +1,74 @@ +/* + * Copyright (C) 2024 DANS - Data Archiving and Networked Services (info@dans.knaw.nl) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package nl.knaw.dans.dvingest.db; + +import io.dropwizard.hibernate.AbstractDAO; +import nl.knaw.dans.dvingest.core.ImportJob; +import org.hibernate.SessionFactory; +import org.hibernate.exception.ConstraintViolationException; + +import javax.persistence.OptimisticLockException; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; +import javax.persistence.criteria.Root; +import java.util.Optional; + +public class ImportJobDao extends AbstractDAO { + public ImportJobDao(SessionFactory sessionFactory) { + super(sessionFactory); + } + + public ImportJob save(ImportJob importJob) { + try { + if (importJob.getId() == null || get(importJob.getId()) == null) { + persist(importJob); + } + else { + currentSession().update(importJob); + } + return importJob; + } + catch (ConstraintViolationException e) { + throw new IllegalArgumentException(e.getSQLException().getMessage()); + } + catch (OptimisticLockException e) { + throw new IllegalStateException("Failed to update ImportJob due to concurrent modification", e); + } + } + + public Optional findById(Long id) { + return Optional.ofNullable(get(id)); + } + + public Optional findByUuid(String uuid) { + CriteriaBuilder builder = currentSession().getCriteriaBuilder(); + CriteriaQuery query = builder.createQuery(ImportJob.class); + Root root = query.from(ImportJob.class); + query.select(root).where(builder.equal(root.get("uuid"), uuid)); + + return Optional.ofNullable(currentSession().createQuery(query).uniqueResult()); + } + + public Optional getNextJob() { + CriteriaBuilder builder = currentSession().getCriteriaBuilder(); + CriteriaQuery query = builder.createQuery(ImportJob.class); + Root root = query.from(ImportJob.class); + query.select(root).where(builder.equal(root.get("status"), "PENDING")).orderBy(builder.desc(root.get("creationTime"))); + + return Optional.ofNullable(currentSession().createQuery(query).setMaxResults(1).uniqueResult()); + } + +} diff --git a/src/main/java/nl/knaw/dans/dvingest/resources/IngestApiResource.java b/src/main/java/nl/knaw/dans/dvingest/resources/IngestApiResource.java new file mode 100644 index 0000000..794eb95 --- /dev/null +++ b/src/main/java/nl/knaw/dans/dvingest/resources/IngestApiResource.java @@ -0,0 +1,27 @@ +/* + * Copyright (C) 2024 DANS - Data Archiving and Networked Services (info@dans.knaw.nl) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package nl.knaw.dans.dvingest.resources; + +import nl.knaw.dans.dvingest.api.ImportCommandDto; + +import javax.ws.rs.core.Response; + +public class IngestApiResource implements IngestApi { + @Override + public Response ingestPost(ImportCommandDto importCommandDto) { + return null; + } +} diff --git a/src/test/java/nl/knaw/dans/dvingest/db/ImportJobDaoTest.java b/src/test/java/nl/knaw/dans/dvingest/db/ImportJobDaoTest.java new file mode 100644 index 0000000..f58f4fa --- /dev/null +++ b/src/test/java/nl/knaw/dans/dvingest/db/ImportJobDaoTest.java @@ -0,0 +1,58 @@ +/* + * Copyright (C) 2024 DANS - Data Archiving and Networked Services (info@dans.knaw.nl) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package nl.knaw.dans.dvingest.db; + +import io.dropwizard.testing.junit5.DAOTestExtension; +import io.dropwizard.testing.junit5.DropwizardExtensionsSupport; +import nl.knaw.dans.dvingest.core.ImportJob; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; + +import java.nio.file.Path; + +import static org.assertj.core.api.Assertions.assertThat; + +@ExtendWith(DropwizardExtensionsSupport.class) +public class ImportJobDaoTest { + private final DAOTestExtension db = DAOTestExtension.newBuilder() + .addEntityClass(ImportJob.class) + .build(); + private final ImportJobDao dao = new ImportJobDao(db.getSessionFactory()); + + @Test + public void create_should_save_ImportJob() { + var uuid = "f797a6a9-2a85-4f26-af62-b3eaae724497"; + + db.inTransaction(() -> { + var importJob = new ImportJob(); + importJob.setLocation("path"); + importJob.setUuid(uuid); + importJob.setStatus("status"); + importJob.setCreationTime(123L); + dao.save(importJob); + }); + + db.inTransaction(() -> { + var result = dao.findByUuid(uuid); + assertThat(result).isPresent(); + assertThat(result.get().getLocation()).isEqualTo("path"); + assertThat(result.get().getUuid()).isEqualTo(uuid); + assertThat(result.get().getStatus()).isEqualTo("status"); + assertThat(result.get().getCreationTime()).isEqualTo(123L); + }); + } + +}