From 528a5fd31644f984997064b11ae8cd5b2948a0e6 Mon Sep 17 00:00:00 2001 From: Joseph Kotanchik Date: Fri, 1 Nov 2024 14:52:42 -0400 Subject: [PATCH] MAT-7870: Accept model value when creating a draft. --- pom.xml | 2 +- .../controllers/CqlLibraryController.java | 5 ++-- .../services/VersionService.java | 21 +++++++++++++++- .../CqlLibraryControllerMvcTest.java | 25 ++++++++++++------- .../controllers/CqlLibraryControllerTest.java | 7 ++++-- .../services/VersionServiceTest.java | 24 ++++++++++++++---- 6 files changed, 64 insertions(+), 20 deletions(-) diff --git a/pom.xml b/pom.xml index 62b467e..6962b86 100644 --- a/pom.xml +++ b/pom.xml @@ -102,7 +102,7 @@ gov.cms.madie madie-java-models - 0.6.58-SNAPSHOT + 0.6.70-SNAPSHOT gov.cms.madie diff --git a/src/main/java/gov/cms/madie/cqllibraryservice/controllers/CqlLibraryController.java b/src/main/java/gov/cms/madie/cqllibraryservice/controllers/CqlLibraryController.java index a5608ba..d8444c6 100644 --- a/src/main/java/gov/cms/madie/cqllibraryservice/controllers/CqlLibraryController.java +++ b/src/main/java/gov/cms/madie/cqllibraryservice/controllers/CqlLibraryController.java @@ -174,8 +174,9 @@ public ResponseEntity 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); } diff --git a/src/main/java/gov/cms/madie/cqllibraryservice/services/VersionService.java b/src/main/java/gov/cms/madie/cqllibraryservice/services/VersionService.java index 69c4286..eac6270 100644 --- a/src/main/java/gov/cms/madie/cqllibraryservice/services/VersionService.java +++ b/src/main/java/gov/cms/madie/cqllibraryservice/services/VersionService.java @@ -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; @@ -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())) { @@ -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); @@ -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; + } } diff --git a/src/test/java/gov/cms/madie/cqllibraryservice/controllers/CqlLibraryControllerMvcTest.java b/src/test/java/gov/cms/madie/cqllibraryservice/controllers/CqlLibraryControllerMvcTest.java index 827dbe1..6a47176 100644 --- a/src/test/java/gov/cms/madie/cqllibraryservice/controllers/CqlLibraryControllerMvcTest.java +++ b/src/test/java/gov/cms/madie/cqllibraryservice/controllers/CqlLibraryControllerMvcTest.java @@ -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( @@ -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 @@ -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( @@ -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 @@ -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( @@ -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 @@ -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( @@ -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 diff --git a/src/test/java/gov/cms/madie/cqllibraryservice/controllers/CqlLibraryControllerTest.java b/src/test/java/gov/cms/madie/cqllibraryservice/controllers/CqlLibraryControllerTest.java index fe0183f..335e214 100644 --- a/src/test/java/gov/cms/madie/cqllibraryservice/controllers/CqlLibraryControllerTest.java +++ b/src/test/java/gov/cms/madie/cqllibraryservice/controllers/CqlLibraryControllerTest.java @@ -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 output = cqlLibraryController.createDraft( "Library1_ID", CqlLibraryDraft.builder() .cqlLibraryName("Library1") + .model(ModelType.QI_CORE.getValue()) .cql("library Library1 version '1.0.000'") .build(), principal); @@ -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( @@ -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)); diff --git a/src/test/java/gov/cms/madie/cqllibraryservice/services/VersionServiceTest.java b/src/test/java/gov/cms/madie/cqllibraryservice/services/VersionServiceTest.java index 66275bf..c95cbaa 100644 --- a/src/test/java/gov/cms/madie/cqllibraryservice/services/VersionServiceTest.java +++ b/src/test/java/gov/cms/madie/cqllibraryservice/services/VersionServiceTest.java @@ -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 @@ -345,7 +347,9 @@ void testCreateDraftThrowsExceptionForAuthorization() { assertThrows( PermissionDeniedException.class, - () -> versionService.createDraft("testCqlLibraryId", "Library1", "randomUser")); + () -> + versionService.createDraft( + "testCqlLibraryId", "Library1", ModelType.QI_CORE.getValue(), "randomUser")); } @Test @@ -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'") @@ -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(); @@ -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) @@ -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(); @@ -431,6 +439,7 @@ void testCreateDraftThrowsExceptionWhenDraftAlreadyExists() { CqlLibrary existingCqlLibrary = CqlLibrary.builder() .id("testCqlLibraryId") + .model(ModelType.QI_CORE.getValue()) .createdBy("testUser") .draft(false) .librarySetId("testLibrarySetId") @@ -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