Skip to content

Commit

Permalink
MAT-6257: move libraryListDto
Browse files Browse the repository at this point in the history
  • Loading branch information
chubert-sb committed Apr 25, 2024
1 parent 8455f62 commit b4aa635
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package gov.cms.madie.cqllibraryservice.controllers;

import gov.cms.madie.cqllibraryservice.dto.LibraryListDTO;
import gov.cms.madie.cqllibraryservice.exceptions.InvalidIdException;
import gov.cms.madie.cqllibraryservice.exceptions.InvalidResourceStateException;
import gov.cms.madie.cqllibraryservice.services.ActionLogService;
import gov.cms.madie.cqllibraryservice.services.LibrarySetService;
import gov.cms.madie.cqllibraryservice.utils.AuthUtils;
import gov.cms.madie.models.common.ActionType;
import gov.cms.madie.models.dto.LibraryList;
import gov.cms.madie.models.library.CqlLibrary;
import gov.cms.madie.models.library.CqlLibraryDraft;
import gov.cms.madie.models.common.Version;
Expand Down Expand Up @@ -44,12 +44,12 @@ public class CqlLibraryController {
private final LibrarySetService librarySetService;

@GetMapping
public ResponseEntity<List<LibraryList>> getCqlLibraries(
public ResponseEntity<List<LibraryListDTO>> getCqlLibraries(
Principal principal,
@RequestParam(required = false, defaultValue = "false", name = "currentUser")
boolean filterByCurrentUser) {
final String username = principal.getName();
List<LibraryList> cqlLibraries =
List<LibraryListDTO> cqlLibraries =
filterByCurrentUser
? cqlLibraryRepository.findAllLibrariesByUser(username)
: cqlLibraryRepository.findAllProjected();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package gov.cms.madie.cqllibraryservice.dto;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import gov.cms.madie.models.common.ModelType;
import gov.cms.madie.models.common.Version;
import gov.cms.madie.models.library.CqlLibrary;
import gov.cms.madie.models.library.LibrarySet;
import gov.cms.madie.models.utils.VersionJsonSerializer;
import gov.cms.madie.models.validators.EnumValidator;
import jakarta.validation.constraints.NotBlank;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
import org.springframework.data.mongodb.core.mapping.DBRef;
import org.springframework.data.mongodb.core.mapping.Document;

@Data
@Document
@SuperBuilder(toBuilder = true)
@NoArgsConstructor
public class LibraryListDTO {

private String id;
private String librarySetId;

private String cqlLibraryName;

@NotBlank(message = "Model is required")
@EnumValidator(
enumClass = ModelType.class,
message = "Model must be one of the supported types in MADiE.",
groups = {CqlLibrary.ValidationOrder4.class})
private String model;

@JsonSerialize(using = VersionJsonSerializer.VersionSerializer.class)
@JsonDeserialize(using = VersionJsonSerializer.VersionDeserializer.class)
private Version version;

@DBRef private LibrarySet librarySet;

private boolean draft;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package gov.cms.madie.cqllibraryservice.repositories;

import gov.cms.madie.cqllibraryservice.dto.LibraryListDTO;
import gov.cms.madie.models.common.Version;
import gov.cms.madie.models.dto.LibraryList;
import gov.cms.madie.models.library.CqlLibrary;

import java.util.List;
Expand Down Expand Up @@ -40,5 +40,5 @@ List<CqlLibrary> findAllByCqlLibraryNameAndDraftAndVersionAndModel(
+ "foreignField: 'librarySetId',"
+ "as: 'librarySet'}}"
})
List<LibraryList> findAllProjected();
List<LibraryListDTO> findAllProjected();
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package gov.cms.madie.cqllibraryservice.repositories;

import gov.cms.madie.models.dto.LibraryList;
import gov.cms.madie.cqllibraryservice.dto.LibraryListDTO;

import java.util.List;

Expand All @@ -11,5 +11,5 @@ public interface LibraryAclRepository {
* @param userId- current user
* @return List of cqlLibraries
*/
List<LibraryList> findAllLibrariesByUser(String userId);
List<LibraryListDTO> findAllLibrariesByUser(String userId);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package gov.cms.madie.cqllibraryservice.repositories;

import gov.cms.madie.cqllibraryservice.dto.LibraryListDTO;
import gov.cms.madie.models.access.RoleEnum;
import gov.cms.madie.models.dto.LibraryList;
import gov.cms.madie.models.library.CqlLibrary;

import org.springframework.data.mongodb.core.MongoTemplate;
Expand Down Expand Up @@ -32,7 +32,7 @@ private LookupOperation getLookupOperation() {
}

@Override
public List<LibraryList> findAllLibrariesByUser(String userId) {
public List<LibraryListDTO> findAllLibrariesByUser(String userId) {
Criteria librarySetCriteria =
new Criteria()
.orOperator(
Expand All @@ -43,11 +43,11 @@ public List<LibraryList> findAllLibrariesByUser(String userId) {
.in(RoleEnum.SHARED_WITH));
MatchOperation matchOperation = match(librarySetCriteria);
Aggregation libraryAggregation =
newAggregation(getLookupOperation(), matchOperation, project(LibraryList.class));
newAggregation(getLookupOperation(), matchOperation, project(LibraryListDTO.class));

List<LibraryList> results =
List<LibraryListDTO> results =
mongoTemplate
.aggregate(libraryAggregation, CqlLibrary.class, LibraryList.class)
.aggregate(libraryAggregation, CqlLibrary.class, LibraryListDTO.class)
.getMappedResults();
return results;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.*;

import gov.cms.madie.cqllibraryservice.dto.LibraryListDTO;
import gov.cms.madie.cqllibraryservice.exceptions.DuplicateKeyException;
import gov.cms.madie.cqllibraryservice.exceptions.InvalidIdException;
import gov.cms.madie.cqllibraryservice.exceptions.InvalidResourceStateException;
Expand All @@ -21,7 +22,6 @@
import gov.cms.madie.cqllibraryservice.services.ActionLogService;
import gov.cms.madie.cqllibraryservice.services.LibrarySetService;
import gov.cms.madie.models.common.ActionType;
import gov.cms.madie.models.dto.LibraryList;
import gov.cms.madie.models.library.CqlLibrary;
import gov.cms.madie.models.library.CqlLibraryDraft;
import gov.cms.madie.models.common.ModelType;
Expand Down Expand Up @@ -73,7 +73,7 @@ class CqlLibraryControllerTest {
@Captor private ArgumentCaptor<String> targetIdArgumentCaptor;

private CqlLibrary cqlLibrary;
private LibraryList libraryList;
private LibraryListDTO libraryList;

@BeforeEach
public void setUp() {
Expand All @@ -85,7 +85,7 @@ public void setUp() {
.build();

libraryList =
LibraryList.builder()
LibraryListDTO.builder()
.id("testCqlLibraryId")
.cqlLibraryName("testCqlLibraryName")
.librarySetId("testCqlLibrarySetId")
Expand All @@ -94,11 +94,11 @@ public void setUp() {

@Test
void getCqlLibrariesWithoutCurrentUserFilter() {
List<LibraryList> cqlLibraries = List.of(libraryList);
List<LibraryListDTO> cqlLibraries = List.of(libraryList);
when(cqlLibraryRepository.findAllProjected()).thenReturn(cqlLibraries);
Principal principal = mock(Principal.class);
when(principal.getName()).thenReturn("test.user");
ResponseEntity<List<LibraryList>> response =
ResponseEntity<List<LibraryListDTO>> response =
cqlLibraryController.getCqlLibraries(principal, false);
verify(cqlLibraryRepository, times(1)).findAllProjected();
verifyNoMoreInteractions(cqlLibraryRepository);
Expand All @@ -109,12 +109,12 @@ void getCqlLibrariesWithoutCurrentUserFilter() {

@Test
void getCqlLibrariesWithCurrentUserFilter() {
List<LibraryList> cqlLibraries = List.of(libraryList);
List<LibraryListDTO> cqlLibraries = List.of(libraryList);
when(cqlLibraryRepository.findAllLibrariesByUser(anyString())).thenReturn(cqlLibraries);
Principal principal = mock(Principal.class);
when(principal.getName()).thenReturn("test.user");

ResponseEntity<List<LibraryList>> response =
ResponseEntity<List<LibraryListDTO>> response =
cqlLibraryController.getCqlLibraries(principal, true);
verify(cqlLibraryRepository, times(1)).findAllLibrariesByUser(eq("test.user"));
verifyNoMoreInteractions(cqlLibraryRepository);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package gov.cms.madie.cqllibraryservice.repositories;

import gov.cms.madie.cqllibraryservice.dto.LibraryListDTO;
import gov.cms.madie.models.common.Version;
import gov.cms.madie.models.dto.LibraryList;
import gov.cms.madie.models.library.LibrarySet;
import org.bson.Document;

Expand Down Expand Up @@ -31,18 +31,18 @@ public class LibraryAclRepositoryImplTest {

@InjectMocks LibraryAclRepositoryImpl libraryAclRepository;

private LibraryList library1;
private LibraryList library2;
private LibraryList library3;
private LibraryList library4;
private LibraryList library5;
private LibraryListDTO library1;
private LibraryListDTO library2;
private LibraryListDTO library3;
private LibraryListDTO library4;
private LibraryListDTO library5;

@BeforeEach
void setup() {
LibrarySet librarySet1 = LibrarySet.builder().owner("p1").librarySetId("id-1").build();
LibrarySet librarySet2 = LibrarySet.builder().owner("p2").librarySetId("id-1").build();
library1 =
LibraryList.builder()
LibraryListDTO.builder()
.id("1")
.cqlLibraryName("test measure 1")
.librarySetId("1-1")
Expand All @@ -51,7 +51,7 @@ void setup() {
.draft(true)
.build();
library2 =
LibraryList.builder()
LibraryListDTO.builder()
.id("2")
.cqlLibraryName("test measure 2")
.librarySetId("2-2")
Expand All @@ -60,7 +60,7 @@ void setup() {
.draft(true)
.build();
library3 =
LibraryList.builder()
LibraryListDTO.builder()
.id("3")
.cqlLibraryName("library3")
.librarySetId("id-2")
Expand All @@ -69,7 +69,7 @@ void setup() {
.draft(false)
.build();
library4 =
LibraryList.builder()
LibraryListDTO.builder()
.id("4")
.cqlLibraryName("library4")
.librarySetId("id-2")
Expand All @@ -78,7 +78,7 @@ void setup() {
.draft(false)
.build();
library5 =
LibraryList.builder()
LibraryListDTO.builder()
.id("5")
.cqlLibraryName("library5")
.librarySetId("id-2")
Expand All @@ -102,7 +102,7 @@ public void testfindAllMyLibraries() {
when(mongoTemplate.aggregate(any(Aggregation.class), (Class<?>) any(), any()))
.thenReturn(allResults);

List<LibraryList> list = libraryAclRepository.findAllLibrariesByUser("p1");
List<LibraryListDTO> list = libraryAclRepository.findAllLibrariesByUser("p1");
assertEquals(list.size(), 3);
}
}

0 comments on commit b4aa635

Please sign in to comment.