Skip to content

Commit

Permalink
Added unit test for DAO
Browse files Browse the repository at this point in the history
  • Loading branch information
janvanmansum committed Nov 8, 2024
1 parent 8daea6b commit d6bce7f
Show file tree
Hide file tree
Showing 10 changed files with 375 additions and 1 deletion.
53 changes: 53 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,59 @@
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-core</artifactId>
</dependency>
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-hibernate</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>nl.knaw.dans</groupId>
<artifactId>dans-dataverse-client-lib</artifactId>
</dependency>
<dependency>
<groupId>nl.knaw.dans</groupId>
<artifactId>dans-java-utils</artifactId>
</dependency>
<dependency>
<groupId>nl.knaw.dans</groupId>
<artifactId>dans-validation-lib</artifactId>
</dependency>

<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-testing</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
27 changes: 27 additions & 0 deletions src/main/java/nl/knaw/dans/dvingest/config/IngestConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright (C) 2024 DANS - Data Archiving and Networked Services ([email protected])
*
* 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;
}
60 changes: 60 additions & 0 deletions src/main/java/nl/knaw/dans/dvingest/core/ImportJob.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright (C) 2024 DANS - Data Archiving and Networked Services ([email protected])
*
* 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;
}
22 changes: 22 additions & 0 deletions src/main/java/nl/knaw/dans/dvingest/core/ImportTask.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright (C) 2024 DANS - Data Archiving and Networked Services ([email protected])
*
* 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 {



}
25 changes: 25 additions & 0 deletions src/main/java/nl/knaw/dans/dvingest/core/ImportTaskFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright (C) 2024 DANS - Data Archiving and Networked Services ([email protected])
*
* 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;
}
}
22 changes: 22 additions & 0 deletions src/main/java/nl/knaw/dans/dvingest/core/ImportTaskProcessor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright (C) 2024 DANS - Data Archiving and Networked Services ([email protected])
*
* 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 {



}
74 changes: 74 additions & 0 deletions src/main/java/nl/knaw/dans/dvingest/db/ImportJobDao.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright (C) 2024 DANS - Data Archiving and Networked Services ([email protected])
*
* 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<ImportJob> {
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<ImportJob> findById(Long id) {
return Optional.ofNullable(get(id));
}

public Optional<ImportJob> findByUuid(String uuid) {
CriteriaBuilder builder = currentSession().getCriteriaBuilder();
CriteriaQuery<ImportJob> query = builder.createQuery(ImportJob.class);
Root<ImportJob> root = query.from(ImportJob.class);
query.select(root).where(builder.equal(root.get("uuid"), uuid));

return Optional.ofNullable(currentSession().createQuery(query).uniqueResult());
}

public Optional<ImportJob> getNextJob() {
CriteriaBuilder builder = currentSession().getCriteriaBuilder();
CriteriaQuery<ImportJob> query = builder.createQuery(ImportJob.class);
Root<ImportJob> 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());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright (C) 2024 DANS - Data Archiving and Networked Services ([email protected])
*
* 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;
}
}
58 changes: 58 additions & 0 deletions src/test/java/nl/knaw/dans/dvingest/db/ImportJobDaoTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright (C) 2024 DANS - Data Archiving and Networked Services ([email protected])
*
* 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);
});
}

}

0 comments on commit d6bce7f

Please sign in to comment.