Skip to content

Commit

Permalink
Merge pull request #89 from MeasureAuthoringTool/MAT-7763
Browse files Browse the repository at this point in the history
MAT-7763 endpoint to fetch libraries for a given set id
  • Loading branch information
adongare authored Oct 23, 2024
2 parents 54cfc02 + 62d6396 commit 632d427
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package gov.cms.madie.cqllibraryservice.controllers;

import gov.cms.madie.cqllibraryservice.dto.LibrarySetDTO;
import gov.cms.madie.cqllibraryservice.dto.LibraryListDTO;
import gov.cms.madie.cqllibraryservice.exceptions.InvalidIdException;
import gov.cms.madie.cqllibraryservice.exceptions.InvalidResourceStateException;
Expand Down Expand Up @@ -79,6 +80,11 @@ public ResponseEntity<CqlLibrary> getVersionedCqlLibrary(
cqlLibraryService.getVersionedCqlLibrary(name, version, model, includeElm, accessToken));
}

@GetMapping("/library-set/{setId}")
public ResponseEntity<LibrarySetDTO> getLibrarySetBySetId(@PathVariable String setId) {
return ResponseEntity.ok(cqlLibraryService.getLibrarySetBySetId(setId));
}

@PostMapping
public ResponseEntity<CqlLibrary> createCqlLibrary(
@Validated(CqlLibrary.ValidationSequence.class) @RequestBody CqlLibrary cqlLibrary,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package gov.cms.madie.cqllibraryservice.dto;

import gov.cms.madie.models.library.CqlLibrary;
import gov.cms.madie.models.library.LibrarySet;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
import org.springframework.data.mongodb.core.mapping.Document;

import java.util.List;

@Data
@Document
@SuperBuilder(toBuilder = true)
@NoArgsConstructor
public class LibrarySetDTO {
private LibrarySet librarySet;
private List<CqlLibrary> libraries;
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,7 @@ List<CqlLibrary> findAllByCqlLibraryNameAndDraftAndVersionAndModel(
"{'$sort': {'createdAt':1}}"
})
List<CqlLibrary> findByCqlLibrarySetId();

List<CqlLibrary> findByLibrarySetIdAndDraftAndActive(
String librarySetId, boolean draft, boolean active);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package gov.cms.madie.cqllibraryservice.services;

import gov.cms.madie.cqllibraryservice.dto.LibrarySetDTO;
import gov.cms.madie.cqllibraryservice.dto.LibraryListDTO;
import gov.cms.madie.cqllibraryservice.exceptions.*;
import gov.cms.madie.cqllibraryservice.repositories.LibrarySetRepository;
import gov.cms.madie.models.access.RoleEnum;
import gov.cms.madie.models.common.Version;
import gov.cms.madie.models.dto.LibraryUsage;
Expand All @@ -25,6 +27,7 @@
public class CqlLibraryService {

private final ElmTranslatorClient elmTranslatorClient;
private final LibrarySetRepository librarySetRepository;
private CqlLibraryRepository cqlLibraryRepository;
private LibrarySetService librarySetService;
private MeasureServiceClient measureServiceClient;
Expand Down Expand Up @@ -169,8 +172,8 @@ public boolean isLibraryBeinUsed(String name, String accessToken) {
* This method deletes cql library and its versions permanently, if none of the versions is being
* used either in measure or another library
*
* @param name
* @param apiKey
* @param name - library name
* @param accessToken - auth token
*/
public void deleteLibraryAlongWithVersions(String name, String accessToken) {
if (isLibraryBeinUsed(name, accessToken)) {
Expand All @@ -188,4 +191,24 @@ public List<LibraryListDTO> findLibrariesByNameAndModel(String libraryName, Stri
return cqlLibraryRepository.findLibrariesByNameAndModelOrderByNameAscAndVersionDsc(
libraryName, model);
}

/**
* Get the versioned libraries that belongs to given set id
*
* @param librarySetId - set id of a Library
* @return LibrarySetDTO - DTO containing all the versioned libraries for a set id and library set
* itself
*/
public LibrarySetDTO getLibrarySetBySetId(String librarySetId) {
if (StringUtils.isBlank(librarySetId)) {
throw new BadRequestObjectException("Please provide library set ID.");
}
List<CqlLibrary> libraries =
cqlLibraryRepository.findByLibrarySetIdAndDraftAndActive(librarySetId, false, true);
if (CollectionUtils.isEmpty(libraries)) {
return null;
}
LibrarySet librarySet = librarySetRepository.findByLibrarySetId(librarySetId).orElse(null);
return LibrarySetDTO.builder().libraries(libraries).librarySet(librarySet).build();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gov.cms.madie.cqllibraryservice.controllers;

import gov.cms.madie.cqllibraryservice.config.security.SecurityConfig;
import gov.cms.madie.cqllibraryservice.dto.LibrarySetDTO;
import gov.cms.madie.cqllibraryservice.dto.LibraryListDTO;
import gov.cms.madie.cqllibraryservice.exceptions.GeneralConflictException;
import gov.cms.madie.models.common.ModelType;
Expand Down Expand Up @@ -1615,4 +1616,36 @@ void testGetLibrariesByNameAndModel() throws Exception {
assertThat(
result.getResponse().getContentAsString(), containsString(l1.getVersion().toString()));
}

@Test
void testGetLibrarySetBySetId() throws Exception {
String librarySetId = "1-1-1-1";
String owner = "John";
LibrarySet librarySet = LibrarySet.builder().librarySetId(librarySetId).owner(owner).build();
CqlLibrary library =
CqlLibrary.builder()
.cqlLibraryName("Lib1")
.librarySetId(librarySetId)
.version(Version.parse("0.1.000"))
.build();
LibrarySetDTO librarySetDTO =
LibrarySetDTO.builder().librarySet(librarySet).libraries(List.of(library)).build();
when(cqlLibraryService.getLibrarySetBySetId(anyString())).thenReturn(librarySetDTO);
MvcResult result =
mockMvc
.perform(
get("/cql-libraries/library-set/" + librarySetId)
.with(user(TEST_USER_ID))
.with(csrf()))
.andReturn();
assertThat(
result.getResponse().getContentAsString(),
containsString(librarySetDTO.getLibrarySet().getLibrarySetId()));
assertThat(
result.getResponse().getContentAsString(),
containsString(librarySetDTO.getLibrarySet().getOwner()));
assertThat(
result.getResponse().getContentAsString(),
containsString(librarySetDTO.getLibraries().get(0).getCqlLibraryName()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import gov.cms.madie.cqllibraryservice.dto.LibrarySetDTO;
import gov.cms.madie.cqllibraryservice.dto.LibraryListDTO;
import gov.cms.madie.cqllibraryservice.exceptions.BadRequestObjectException;
import gov.cms.madie.cqllibraryservice.exceptions.DuplicateKeyException;
import gov.cms.madie.cqllibraryservice.exceptions.GeneralConflictException;
import gov.cms.madie.cqllibraryservice.exceptions.PermissionDeniedException;
import gov.cms.madie.cqllibraryservice.exceptions.ResourceNotFoundException;
import gov.cms.madie.cqllibraryservice.repositories.LibrarySetRepository;
import gov.cms.madie.models.common.Version;
import gov.cms.madie.models.dto.LibraryUsage;
import gov.cms.madie.models.library.CqlLibrary;
Expand All @@ -40,7 +42,7 @@ class CqlLibraryServiceTest {
@Mock private CqlLibraryRepository cqlLibraryRepository;
@Mock private LibrarySetService librarySetService;
@Mock private MeasureServiceClient measureServiceClient;

@Mock private LibrarySetRepository librarySetRepository;
@Mock private ElmTranslatorClient elmTranslatorClient;

@Test
Expand Down Expand Up @@ -405,4 +407,69 @@ void testFindLibrariesByNameAndModelIfLibraryNameMissing() {
() -> cqlLibraryService.findLibrariesByNameAndModel(null, "QDM"));
assertThat(ex.getMessage(), is(equalTo("Please provide library name and model.")));
}

@Test
void testGetLibrarySetBySetId() {
String librarySetId = "1-1-1-1";
String libraryVersion = "1.0.000";
String owner = "John";
LibrarySet librarySet = LibrarySet.builder().librarySetId(librarySetId).owner(owner).build();
CqlLibrary lib1 =
CqlLibrary.builder()
.cqlLibraryName("Lib1")
.librarySetId(librarySetId)
.version(Version.parse("0.1.000"))
.build();
CqlLibrary lib2 =
CqlLibrary.builder()
.cqlLibraryName("Lib1")
.librarySetId(librarySetId)
.version(Version.parse(libraryVersion))
.build();
when(cqlLibraryRepository.findByLibrarySetIdAndDraftAndActive(
anyString(), anyBoolean(), anyBoolean()))
.thenReturn(List.of(lib1, lib2));
when(librarySetRepository.findByLibrarySetId(anyString()))
.thenReturn(Optional.ofNullable(librarySet));
LibrarySetDTO libraryDTO = cqlLibraryService.getLibrarySetBySetId(librarySetId);
assertThat(libraryDTO.getLibrarySet().getLibrarySetId(), equalTo(librarySetId));
assertThat(libraryDTO.getLibrarySet().getOwner(), equalTo(owner));
assertThat(libraryDTO.getLibraries().size(), equalTo(2));
}

@Test
void testGetLibrarySetBySetIdIfNoLibraryExistsWithSetId() {
String librarySetId = "1-1-1-1";
when(cqlLibraryRepository.findByLibrarySetIdAndDraftAndActive(
anyString(), anyBoolean(), anyBoolean()))
.thenReturn(List.of());
LibrarySetDTO libraryDTO = cqlLibraryService.getLibrarySetBySetId(librarySetId);
assertThat(libraryDTO, equalTo(null));
}

@Test
void testGetLibrarySetBySetIdIfLibrarySetNotFound() {
String librarySetId = "1-1-1-1";
CqlLibrary lib1 =
CqlLibrary.builder()
.cqlLibraryName("Lib1")
.librarySetId(librarySetId)
.version(Version.parse("0.1.000"))
.build();
when(cqlLibraryRepository.findByLibrarySetIdAndDraftAndActive(
anyString(), anyBoolean(), anyBoolean()))
.thenReturn(List.of(lib1));
when(librarySetRepository.findByLibrarySetId(anyString())).thenReturn(Optional.empty());
LibrarySetDTO libraryDTO = cqlLibraryService.getLibrarySetBySetId(librarySetId);
assertThat(libraryDTO.getLibrarySet(), equalTo(null));
assertThat(libraryDTO.getLibraries().size(), equalTo(1));
}

@Test
void testGetLibrarySetBySetIdIfSetIdNotProvided() {
Exception exception =
assertThrows(
BadRequestObjectException.class, () -> cqlLibraryService.getLibrarySetBySetId(null));
assertThat(exception.getMessage(), equalTo("Please provide library set ID."));
}
}

0 comments on commit 632d427

Please sign in to comment.