Skip to content

Commit

Permalink
Merge pull request #90 from MeasureAuthoringTool/feature/mat-7870-add…
Browse files Browse the repository at this point in the history
…-model-change-on-draft

MAT-7870: Accept model value when creating a draft
  • Loading branch information
jkotanchik-SB authored Nov 1, 2024
2 parents 632d427 + 528a5fd commit f93f346
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 20 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
<dependency>
<groupId>gov.cms.madie</groupId>
<artifactId>madie-java-models</artifactId>
<version>0.6.58-SNAPSHOT</version>
<version>0.6.70-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>gov.cms.madie</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,9 @@ public ResponseEntity<CqlLibrary> createDraft(
@Validated(CqlLibrary.ValidationSequence.class) @RequestBody final CqlLibraryDraft cqlLibrary,
Principal principal) {
var output =
versionService.createDraft(id, cqlLibrary.getCqlLibraryName(), principal.getName());
log.info("output: {}", output);
versionService.createDraft(
id, cqlLibrary.getCqlLibraryName(), cqlLibrary.getModel(), principal.getName());
log.debug("output: {}", output);
return ResponseEntity.status(HttpStatus.CREATED).body(output);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
import gov.cms.madie.cqllibraryservice.repositories.CqlLibraryRepository;
import java.time.Instant;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -113,7 +116,7 @@ private void validateCqlLibrary(CqlLibrary cqlLibrary, String username) {
}
}

public CqlLibrary createDraft(String id, String cqlLibraryName, String username) {
public CqlLibrary createDraft(String id, String cqlLibraryName, String model, String username) {
CqlLibrary cqlLibrary = cqlLibraryService.findCqlLibraryById(id);

if (!Objects.equals(cqlLibraryName, cqlLibrary.getCqlLibraryName())) {
Expand Down Expand Up @@ -144,6 +147,10 @@ public CqlLibrary createDraft(String id, String cqlLibraryName, String username)
".*?[\n\r]",
"library " + cqlLibraryName + " version '" + cqlLibrary.getVersion() + "'\n"));
}
if (!model.equals(cqlLibrary.getModel())) {
clonedCqlLibrary.setModel(model);
clonedCqlLibrary.setCql(updateUsingStatement(model, cqlLibrary.getCql()));
}

var savedCqlLibrary = cqlLibraryRepository.save(clonedCqlLibrary);

Expand Down Expand Up @@ -190,4 +197,16 @@ public boolean isDraftable(CqlLibrary cqlLibrary) {
}
return !cqlLibraryRepository.existsByLibrarySetIdAndDraft(cqlLibrary.getLibrarySetId(), true);
}

private String updateUsingStatement(String model, final String cql) {
Pattern qicorePattern = Pattern.compile("using QICore .*version '[0-9]\\.[0-9](\\.[0-9])?'");
Matcher matcher = qicorePattern.matcher(cql);
String updatedCql = cql;
if (matcher.find()) {
updatedCql =
matcher.replaceAll(
"using QICore version '" + model.substring(model.lastIndexOf("v") + 1) + "'");
}
return updatedCql;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -947,7 +947,7 @@ public void testCreateDraftReturnsConflictWhenDraftAlreadyExists() throws Except
.cql("library Library1 version '1.0.000'")
.build());

when(versionService.createDraft(anyString(), anyString(), anyString()))
when(versionService.createDraft(anyString(), anyString(), anyString(), anyString()))
.thenThrow(new ResourceNotDraftableException("CQL Library"));
mockMvc
.perform(
Expand All @@ -959,7 +959,8 @@ public void testCreateDraftReturnsConflictWhenDraftAlreadyExists() throws Except
.andExpect(status().isConflict())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE));
verify(versionService, times(1))
.createDraft(eq("Library1_ID"), eq("Library1"), eq(TEST_USER_ID));
.createDraft(
eq("Library1_ID"), eq("Library1"), eq(ModelType.QI_CORE.getValue()), eq(TEST_USER_ID));
}

@Test
Expand All @@ -985,7 +986,7 @@ public void testCreateDraftReturnsNotFound() throws Exception {
.cql("library Library1 version '1.0.000'")
.build());

when(versionService.createDraft(anyString(), anyString(), anyString()))
when(versionService.createDraft(anyString(), anyString(), anyString(), anyString()))
.thenThrow(new ResourceNotFoundException("CQL Library", "Library1_ID"));
mockMvc
.perform(
Expand All @@ -997,7 +998,8 @@ public void testCreateDraftReturnsNotFound() throws Exception {
.andExpect(status().isNotFound())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE));
verify(versionService, times(1))
.createDraft(eq("Library1_ID"), eq("Library1"), eq(TEST_USER_ID));
.createDraft(
eq("Library1_ID"), eq("Library1"), eq(ModelType.QI_CORE.getValue()), eq(TEST_USER_ID));
}

@Test
Expand All @@ -1023,8 +1025,8 @@ public void testCreateDraftReturnsBadRequestForNameChangeNonUnique() throws Exce
.draft(false)
.version(new Version(2, 1, 0))
.build());

when(versionService.createDraft(anyString(), anyString(), anyString()))
System.out.println(json);
when(versionService.createDraft(anyString(), anyString(), anyString(), anyString()))
.thenThrow(new DuplicateKeyException("cqlLibraryName", "Library name must be unique."));
mockMvc
.perform(
Expand All @@ -1037,7 +1039,11 @@ public void testCreateDraftReturnsBadRequestForNameChangeNonUnique() throws Exce
.andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(jsonPath("$.message").value("Library name must be unique."));
verify(versionService, times(1))
.createDraft(eq("Library1_ID"), eq("ChangedName"), eq(TEST_USER_ID));
.createDraft(
eq("Library1_ID"),
eq("ChangedName"),
eq(ModelType.QI_CORE.getValue()),
eq(TEST_USER_ID));
}

@Test
Expand All @@ -1062,7 +1068,7 @@ public void testCreateDraftReturnsCreatedDraft() throws Exception {
.cql("library Library1 version '1.2.000'")
.build());

when(versionService.createDraft(anyString(), anyString(), anyString()))
when(versionService.createDraft(anyString(), anyString(), anyString(), anyString()))
.thenReturn(draftLibrary);
mockMvc
.perform(
Expand All @@ -1078,7 +1084,8 @@ public void testCreateDraftReturnsCreatedDraft() throws Exception {
.andExpect(jsonPath("$.draft").value(true))
.andExpect(jsonPath("$.version").value("1.2.000"));
verify(versionService, times(1))
.createDraft(eq("Library1_ID"), eq("Library1"), eq(TEST_USER_ID));
.createDraft(
eq("Library1_ID"), eq("Library1"), eq(ModelType.QI_CORE.getValue()), eq(TEST_USER_ID));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -411,13 +411,15 @@ public void testCreateDraftReturnsDraft() {
.createdBy("User1")
.lastModifiedBy("User1")
.build();
when(versionService.createDraft(anyString(), anyString(), anyString())).thenReturn(draft);
when(versionService.createDraft(anyString(), anyString(), anyString(), anyString()))
.thenReturn(draft);
when(principal.getName()).thenReturn("test.user");
ResponseEntity<CqlLibrary> output =
cqlLibraryController.createDraft(
"Library1_ID",
CqlLibraryDraft.builder()
.cqlLibraryName("Library1")
.model(ModelType.QI_CORE.getValue())
.cql("library Library1 version '1.0.000'")
.build(),
principal);
Expand All @@ -428,7 +430,7 @@ public void testCreateDraftReturnsDraft() {

@Test
public void testCreateDraftReturnsException() {
when(versionService.createDraft(anyString(), anyString(), anyString()))
when(versionService.createDraft(anyString(), anyString(), anyString(), anyString()))
.thenThrow(new ResourceNotDraftableException("CqlLibrary"));
when(principal.getName()).thenReturn("test.user");
assertThrows(
Expand All @@ -438,6 +440,7 @@ public void testCreateDraftReturnsException() {
"Library1_ID",
CqlLibraryDraft.builder()
.cqlLibraryName("Library1")
.model(ModelType.QI_CORE.getValue())
.cql("library Library1 version '1.0.000'")
.build(),
principal));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,9 @@ void testCreateDraftThrowsExceptionForResourceNotFound() {
.findCqlLibraryById(anyString());
assertThrows(
ResourceNotFoundException.class,
() -> versionService.createDraft("testCqlLibraryId", "Library1", "testUser"));
() ->
versionService.createDraft(
"testCqlLibraryId", "Library1", ModelType.QI_CORE.getValue(), "testUser"));
}

@Test
Expand Down Expand Up @@ -345,7 +347,9 @@ void testCreateDraftThrowsExceptionForAuthorization() {

assertThrows(
PermissionDeniedException.class,
() -> versionService.createDraft("testCqlLibraryId", "Library1", "randomUser"));
() ->
versionService.createDraft(
"testCqlLibraryId", "Library1", ModelType.QI_CORE.getValue(), "randomUser"));
}

@Test
Expand All @@ -358,6 +362,7 @@ void testCreateDraftSuccesfullyDraftsForSharedUser() {
CqlLibrary.builder()
.id("testCqlLibraryId")
.cqlLibraryName("testCqlLibraryName")
.model(ModelType.QI_CORE.getValue())
.createdBy("testUser")
.draft(false)
.cql("library testCql version '1.0.000'")
Expand All @@ -378,7 +383,8 @@ void testCreateDraftSuccesfullyDraftsForSharedUser() {
when(cqlLibraryRepository.existsByLibrarySetIdAndDraft(anyString(), anyBoolean()))
.thenReturn(false);

versionService.createDraft("testCqlLibraryId", "testNewCqlLibraryName", "sharedUser");
versionService.createDraft(
"testCqlLibraryId", "testNewCqlLibraryName", ModelType.QI_CORE.getValue(), "sharedUser");
verify(cqlLibraryRepository, times(1)).save(cqlLibraryArgumentCaptor.capture());
CqlLibrary savedValue = cqlLibraryArgumentCaptor.getValue();

Expand All @@ -395,6 +401,7 @@ void testCreateDraftSuccess() {
CqlLibrary.builder()
.id("testCqlLibraryId")
.cqlLibraryName("testCqlLibraryName")
.model(ModelType.QI_CORE.getValue())
.createdBy("testUser")
.cql("library testCql version '1.0.000'")
.draft(false)
Expand All @@ -411,7 +418,8 @@ void testCreateDraftSuccess() {
when(cqlLibraryRepository.existsByLibrarySetIdAndDraft(anyString(), anyBoolean()))
.thenReturn(false);

versionService.createDraft("testCqlLibraryId", "testNewCqlLibraryName", "testUser");
versionService.createDraft(
"testCqlLibraryId", "testNewCqlLibraryName", ModelType.QI_CORE.getValue(), "testUser");
verify(cqlLibraryRepository, times(1)).save(cqlLibraryArgumentCaptor.capture());
CqlLibrary savedValue = cqlLibraryArgumentCaptor.getValue();

Expand All @@ -431,6 +439,7 @@ void testCreateDraftThrowsExceptionWhenDraftAlreadyExists() {
CqlLibrary existingCqlLibrary =
CqlLibrary.builder()
.id("testCqlLibraryId")
.model(ModelType.QI_CORE.getValue())
.createdBy("testUser")
.draft(false)
.librarySetId("testLibrarySetId")
Expand All @@ -450,7 +459,12 @@ void testCreateDraftThrowsExceptionWhenDraftAlreadyExists() {

assertThrows(
ResourceNotDraftableException.class,
() -> versionService.createDraft("testCqlLibraryId", "testNewCqlLibraryName", "testUser"));
() ->
versionService.createDraft(
"testCqlLibraryId",
"testNewCqlLibraryName",
ModelType.QI_CORE_6_0_0.getValue(),
"testUser"));
}

@Test
Expand Down

0 comments on commit f93f346

Please sign in to comment.