Skip to content

Commit

Permalink
Merge pull request #232 from AAFC-BICoE/34325_use_vocabulary_methods_…
Browse files Browse the repository at this point in the history
…from_dina-base

34325 use vocabulary methods from dina base
  • Loading branch information
brandonandre authored Jul 25, 2024
2 parents 76b114b + 3b35013 commit ef987c1
Show file tree
Hide file tree
Showing 14 changed files with 88 additions and 97 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
<checkstyle.version>10.17.0</checkstyle.version>

<asciidoctor-maven-plugin.version>2.1.0</asciidoctor-maven-plugin.version>
<dina-base-api.version>0.125</dina-base-api.version>
<dina-base-api.version>0.127</dina-base-api.version>

<postgresql.version>42.4.4</postgresql.version>
<snakeyaml.version>1.33</snakeyaml.version>
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/ca/gc/aafc/seqdb/api/dto/VocabularyDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@

import ca.gc.aafc.dina.vocabulary.VocabularyElementConfiguration;

import io.crnk.core.resource.annotations.JsonApiId;
import io.crnk.core.resource.annotations.JsonApiResource;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.List;

import com.toedter.spring.hateoas.jsonapi.JsonApiId;
import com.toedter.spring.hateoas.jsonapi.JsonApiTypeForClass;

@AllArgsConstructor
@Getter
@JsonApiResource(type = "vocabulary")
@JsonApiTypeForClass(VocabularyDto.TYPE)
public class VocabularyDto {

public static final String TYPE = "vocabulary";

@JsonApiId
private final String id;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,63 @@
package ca.gc.aafc.seqdb.api.repository;

import ca.gc.aafc.seqdb.api.SequenceVocabularyConfiguration;
import org.springframework.hateoas.CollectionModel;
import org.springframework.hateoas.RepresentationModel;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.toedter.spring.hateoas.jsonapi.JsonApiModelBuilder;

import ca.gc.aafc.dina.repository.ReadOnlyDinaRepositoryV2;
import ca.gc.aafc.seqdb.api.dto.VocabularyDto;
import io.crnk.core.queryspec.QuerySpec;
import io.crnk.core.repository.ReadOnlyResourceRepositoryBase;
import io.crnk.core.resource.list.ResourceList;
import lombok.NonNull;
import org.springframework.stereotype.Repository;
import ca.gc.aafc.seqdb.api.service.VocabularyService;

import static com.toedter.spring.hateoas.jsonapi.JsonApiModelBuilder.jsonApiModel;
import static com.toedter.spring.hateoas.jsonapi.MediaTypes.JSON_API_VALUE;

import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping(value = "/api", produces = JSON_API_VALUE)
public class VocabularyRepository extends ReadOnlyDinaRepositoryV2<String, VocabularyDto> {

protected VocabularyRepository(VocabularyService vocabularyService) {
super(vocabularyService);
}

@Repository
public class VocabularyRepository extends ReadOnlyResourceRepositoryBase<VocabularyDto, String> {
@GetMapping("vocabulary/{id}")
public ResponseEntity<RepresentationModel<?>> handleFindOne(@PathVariable String id) {

private final List<VocabularyDto> vocabulary;
VocabularyDto dto = findOne(id);

protected VocabularyRepository(
@NonNull SequenceVocabularyConfiguration collectionVocabularyConfiguration) {
super(VocabularyDto.class);
if (dto == null) {
return ResponseEntity.notFound().build();
}

vocabulary = collectionVocabularyConfiguration.getVocabulary()
.entrySet()
.stream()
.map(entry -> new VocabularyDto(entry.getKey(), entry.getValue()))
.collect(Collectors.toList());
JsonApiModelBuilder builder = jsonApiModel().model(RepresentationModel.of(dto));

return ResponseEntity.ok(builder.build());
}

@Override
public ResourceList<VocabularyDto> findAll(QuerySpec querySpec) {
return querySpec.apply(vocabulary);
@GetMapping("vocabulary")
public ResponseEntity<RepresentationModel<?>> handleFindAll(HttpServletRequest req) {

String queryString = URLDecoder.decode(req.getQueryString(), StandardCharsets.UTF_8);
List<VocabularyDto> dtos ;
try {
dtos = findAll(queryString);
} catch (IllegalArgumentException iaEx) {
return ResponseEntity.badRequest().build();
}

JsonApiModelBuilder builder = jsonApiModel().model(CollectionModel.of(dtos));

return ResponseEntity.ok(builder.build());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,4 @@ protected void preCreate(SeqSubmission entity) {
entity.setUuid(UUID.randomUUID());
}

// Fixes CT_CONSTRUCTOR_THROW
protected final void finalize() {
// no-op
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@

import java.util.UUID;

// CHECKSTYLE:OFF NoFinalizer
// CHECKSTYLE:OFF SuperFinalize
@Service
public class SequencingFacilityService extends DefaultDinaService<SequencingFacility> {

Expand All @@ -26,9 +24,4 @@ protected void preCreate(SequencingFacility entity) {
entity.setUuid(UUID.randomUUID());
}

// Fixes CT_CONSTRUCTOR_THROW
protected final void finalize() {
// no-op
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
import ca.gc.aafc.seqdb.api.entities.ThermocyclerProfile;
import lombok.NonNull;

// CHECKSTYLE:OFF NoFinalizer
// CHECKSTYLE:OFF SuperFinalize
@Service
public class ThermocyclerProfileService extends DefaultDinaService<ThermocyclerProfile> {

Expand All @@ -26,8 +24,4 @@ protected void preCreate(ThermocyclerProfile entity) {
entity.setUuid(UUID.randomUUID());
}

// Fixes CT_CONSTRUCTOR_THROW
protected final void finalize() {
// no-op
}
}
21 changes: 21 additions & 0 deletions src/main/java/ca/gc/aafc/seqdb/api/service/VocabularyService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package ca.gc.aafc.seqdb.api.service;

import java.util.stream.Collectors;

import org.springframework.stereotype.Service;

import ca.gc.aafc.dina.service.CollectionBackedReadOnlyDinaService;
import ca.gc.aafc.seqdb.api.SequenceVocabularyConfiguration;
import ca.gc.aafc.seqdb.api.dto.VocabularyDto;

@Service
public class VocabularyService extends CollectionBackedReadOnlyDinaService<String, VocabularyDto> {

public VocabularyService(SequenceVocabularyConfiguration sequenceVocabularyConfiguration) {
super(sequenceVocabularyConfiguration.getVocabulary()
.entrySet()
.stream()
.map(entry -> new VocabularyDto(entry.getKey(), entry.getValue()))
.collect(Collectors.toList()), VocabularyDto::getId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
import ca.gc.aafc.seqdb.api.entities.libraryprep.IndexSet;
import lombok.NonNull;

// CHECKSTYLE:OFF NoFinalizer
// CHECKSTYLE:OFF SuperFinalize
@Service
public class IndexSetService extends DefaultDinaService<IndexSet> {

Expand All @@ -26,8 +24,4 @@ protected void preCreate(IndexSet entity) {
entity.setUuid(UUID.randomUUID());
}

// Fixes CT_CONSTRUCTOR_THROW
protected final void finalize() {
// no-op
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,4 @@ protected void preCreate(LibraryPrepBatch entity) {
entity.setUuid(UUID.randomUUID());
}

// Fixes CT_CONSTRUCTOR_THROW
protected final void finalize() {
// no-op
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
import ca.gc.aafc.seqdb.api.validation.ContainerLocationValidator;
import lombok.NonNull;

// CHECKSTYLE:OFF NoFinalizer
// CHECKSTYLE:OFF SuperFinalize
@Service
public class LibraryPrepService extends DefaultDinaService<LibraryPrep> {

Expand Down Expand Up @@ -95,10 +93,4 @@ private void handleOverlap(LibraryPrep libraryPrep) {
}
}
}

// Fixes CT_CONSTRUCTOR_THROW
protected final void finalize() {
// no-op
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
import ca.gc.aafc.seqdb.api.entities.libraryprep.NgsIndex;
import lombok.NonNull;

// CHECKSTYLE:OFF NoFinalizer
// CHECKSTYLE:OFF SuperFinalize
@Service
public class NgsIndexService extends DefaultDinaService<NgsIndex> {

Expand All @@ -26,8 +24,4 @@ protected void preCreate(NgsIndex entity) {
entity.setUuid(UUID.randomUUID());
}

// Fixes CT_CONSTRUCTOR_THROW
protected final void finalize() {
// no-op
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
import ca.gc.aafc.seqdb.api.entities.pooledlibraries.LibraryPoolContent;
import lombok.NonNull;

// CHECKSTYLE:OFF NoFinalizer
// CHECKSTYLE:OFF SuperFinalize
@Service
public class LibraryPoolContentService extends DefaultDinaService<LibraryPoolContent> {

Expand Down Expand Up @@ -96,9 +94,4 @@ private List<LibraryPrepBatch> getBatches(LibraryPool pool) {

return batchs;
}

// Fixes CT_CONSTRUCTOR_THROW
protected final void finalize() {
// no-op
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
import ca.gc.aafc.seqdb.api.entities.pooledlibraries.LibraryPool;
import lombok.NonNull;

// CHECKSTYLE:OFF NoFinalizer
// CHECKSTYLE:OFF SuperFinalize
@Service
public class LibraryPoolService extends DefaultDinaService<LibraryPool> {

Expand All @@ -26,8 +24,4 @@ protected void preCreate(LibraryPool entity) {
entity.setUuid(UUID.randomUUID());
}

// Fixes CT_CONSTRUCTOR_THROW
protected final void finalize() {
// no-op
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
package ca.gc.aafc.seqdb.api.repository;

import javax.inject.Inject;
import org.junit.jupiter.api.Test;
import org.springframework.http.HttpStatus;

import ca.gc.aafc.seqdb.api.dto.VocabularyDto;
import org.junit.jupiter.api.Test;

import ca.gc.aafc.seqdb.api.entities.PcrPrimer.PrimerType;
import io.crnk.core.exception.MethodNotAllowedException;
import io.crnk.core.exception.ResourceNotFoundException;
import io.crnk.core.queryspec.QuerySpec;
import io.crnk.core.resource.list.ResourceList;
import static org.junit.jupiter.api.Assertions.assertEquals;

import static org.junit.jupiter.api.Assertions.*;
import java.util.List;
import javax.inject.Inject;

public class VocabularyRepositoryIT extends BaseRepositoryTest {

Expand All @@ -20,25 +17,19 @@ public class VocabularyRepositoryIT extends BaseRepositoryTest {

@Test
public void findAll_DefaultQuerySpec_AllDtosReturned() {
ResourceList<VocabularyDto> resultList = readOnlyRepo.findAll(new QuerySpec(VocabularyDto.class));
List<VocabularyDto> resultList = readOnlyRepo.findAll("");
assertEquals(2, resultList.size());
}

@Test
public void findOne_QueryPcrPrimerType_OnePrimerTypeDtoReturned() {
VocabularyDto resultDto = readOnlyRepo.findOne("pcrBatchType", new QuerySpec(VocabularyDto.class));
VocabularyDto resultDto = readOnlyRepo.findOne("pcrBatchType");
assertEquals("pcrBatchType", resultDto.getId());
}

@Test
public void findOne_QueryNonExistantID_ThrowResourceNotFoundException() {
assertThrows(ResourceNotFoundException.class,
() -> readOnlyRepo.findOne("mumbo jumbo", new QuerySpec(VocabularyDto.class)));
}

@Test
public void delete_ExistingDtoID_ThrowUnsupportedOperationException() {
assertThrows(MethodNotAllowedException.class, () -> readOnlyRepo.delete(PrimerType.class.getSimpleName()));
public void findOne_QueryNonExistantID_returnNotFound() {
assertEquals(HttpStatus.NOT_FOUND, readOnlyRepo.handleFindOne("mumbo jumbo").getStatusCode());
}

}

0 comments on commit ef987c1

Please sign in to comment.