Skip to content

Commit c25341c

Browse files
issue-2964 - fixed review comments and updated unit tests
Signed-off-by: Prasanna Hegde <prasanna.hegde1@ibm.com>
1 parent 0a4e3ec commit c25341c

2 files changed

Lines changed: 20 additions & 25 deletions

File tree

operation/fhir-operation-validate/src/main/java/org/linuxforhealth/fhir/operation/validate/ValidateOperation.java

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,11 @@ protected Parameters doInvoke(FHIROperationContext operationContext, Class<? ext
6767
Uri profileUri = profileParameter.getValue().as(Uri.class);
6868
String profile = profileUri == null ? null : profileUri.getValue();
6969
issues = FHIRValidator.validator().validate(resource, profile);
70-
} else if (modeType != null
71-
&& (modeType == ModeType.CREATE
72-
|| modeType == ModeType.UPDATE)) {
70+
} else if (modeType == ModeType.CREATE || modeType == ModeType.UPDATE) {
7371
// If the 'mode' parameter is specified and its value is 'create' or 'update', validate the resource
7472
// against the FHIR server config's profile properties and the resource's asserted profiles.
7573
issues = resourceHelper.validateResource(resource);
76-
} else if (modeType != null
77-
&& modeType == ModeType.DELETE
78-
&& operationContext.getType() == FHIROperationContext.Type.INSTANCE) {
74+
} else if (modeType == ModeType.DELETE && operationContext.getType() == FHIROperationContext.Type.INSTANCE) {
7975
// If the 'mode' parameter is specified and its value is 'delete' and delete is invoked at the resource-instance level,
8076
// validate if the persistence layer implementation supports the "delete" operation
8177
issues = validateDeleteResource(resourceTypeName, logicalId, operationContext);
@@ -120,12 +116,11 @@ private OperationOutcome buildResourceValidOperationOutcome(List<Issue> issues)
120116

121117

122118
/**
123-
* This method does the following validations.
124-
* 1. Validate if a resource validation mode code is valid.
125-
* 2. Validate an interaction for a specified resource type.
126-
* 3. Validate if persistence layer implementation supports update/create mode if mode = create.
127-
* 4. Validate if persistence layer implementation supports update/create mode if mode = update but the resource doesnot exist yet in the DB.
128-
* 5. Validate if modes update and delete are only be used when the operation is invoked at the resource instance level
119+
* This method does the following validations if modeType(mode parameter) is not null. If the mode parameter is null then the below validations are skipped.
120+
* 1. Validate an interaction for a specified resource type.
121+
* 2. Validate if persistence layer implementation supports update/create mode if mode = create.
122+
* 3. Validate if persistence layer implementation supports update/create mode if mode = update but the resource doesnot exist yet in the DB.
123+
* 4. Validate if modes update and delete are only be used when the operation is invoked at the resource instance level
129124
*
130125
* @param modeParameter resource validation mode code to be validated
131126
* @param resourceHelper Resource operation provider for loading related Library resources
@@ -177,14 +172,17 @@ private void validateModeParameter(ModeType modetype, FHIRResourceHelpers resour
177172
private void validateUpdateCreateEnabled(ModeType modetype, String resourceType, FHIROperationContext operationContext) throws FHIROperationException {
178173
FHIRPersistence persistence =
179174
(FHIRPersistence) operationContext.getProperty(FHIROperationContext.PROPNAME_PERSISTENCE_IMPL);
180-
if(!persistence.isUpdateCreateEnabled()) {
175+
if (!persistence.isUpdateCreateEnabled()) {
181176
throw buildExceptionWithIssue(IssueSeverity.ERROR, IssueType.NOT_SUPPORTED, "Resource " + modetype.value() + ", of type '"
182177
+ resourceType + "', is not supported.", null);
183178
}
184179
}
185180

186181
/**
187-
* Method to get a valid resource validation mode code from ModeType enum
182+
* If the mode parameter is not null, this method validates if a resource validation mode code is valid and
183+
* returns valid resource validation mode code from ModeType enum.
184+
* If mode parameter is null this method returns null.
185+
* If mode parameter is not valid this method throws FHIROperationException with issues.
188186
* @param modeParameter resource validation mode code
189187
* @return The corresponding ModeType or null if a null value was passed
190188
* @throws FHIROperationException
@@ -201,8 +199,7 @@ private ModeType getModeType(Parameter modeParameter) throws FHIROperationExcept
201199
return modetype;
202200

203201
}
204-
return null;
205-
202+
return null;
206203
}
207204

208205

@@ -260,7 +257,7 @@ private Resource validateResource(FHIROperationContext operationContext, String
260257
Resource resource = null;
261258
// if $validate is invoked at instance level and mode = create, check if the resource already exists.
262259
if (operationContext != null && operationContext.getType() == FHIROperationContext.Type.INSTANCE
263-
&& modeType != null && modeType == ModeType.CREATE) {
260+
&& modeType == ModeType.CREATE) {
264261
Resource existingResource = resourceHelper.doRead(resourceType, logicalId).getResource();
265262
if (existingResource != null) {
266263
throw buildExceptionWithIssue(IssueSeverity.ERROR, IssueType.NOT_SUPPORTED, resourceType + " with id '" + logicalId + "' already exists", null);
@@ -275,7 +272,7 @@ private Resource validateResource(FHIROperationContext operationContext, String
275272
}
276273
// if mode=profile AND no resource parameter value is provided then the resource at this id is read and validated against the nominated profile
277274
if (resourceParameter == null && operationContext != null && operationContext.getType() == FHIROperationContext.Type.INSTANCE
278-
&& (modeType != null && modeType == ModeType.PROFILE)) {
275+
&& modeType == ModeType.PROFILE) {
279276
resource = resourceHelper.doRead(resourceType, logicalId).getResource();
280277
if (resource == null) {
281278
throw buildExceptionWithIssue(IssueSeverity.ERROR, IssueType.INVALID, resourceType + " with id '" + logicalId + "' does not exist", null);

operation/fhir-operation-validate/src/test/java/org/linuxforhealth/fhir/operation/validate/ValidateOperationTest.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ public void testValidateOperationDeleteSupported() throws FHIROperationException
493493

494494

495495
/**
496-
* Test validate operation with create mode type code and "update/create" enabled.
496+
* Test validate operation with update mode type code and "update/create" enabled.
497497
* Validate the outcome when the persistence layer implementation supports the "update/create" operation
498498
* @throws Exception
499499
*
@@ -513,7 +513,7 @@ public void testValidateOperationWithUpdateCreateEnabled() throws Exception {
513513
.build(),
514514
Parameter.builder()
515515
.name("mode")
516-
.value(Code.of("create"))
516+
.value(Code.of("update"))
517517
.build())
518518
.build();
519519
FHIROperationContext operationContext =
@@ -546,12 +546,12 @@ public void testValidateOperationWithUpdateCreateEnabled() throws Exception {
546546
}
547547

548548
/**
549-
* Test validate operation with create mode type code and "update/create" disabled.
549+
* Test validate operation with update mode type code and "update/create" disabled.
550550
* Validate the outcome when the persistence layer implementation does not support the "update/create" operation
551551
* @throws Exception
552552
*
553553
*/
554-
@Test(expectedExceptions = { FHIROperationException.class } , expectedExceptionsMessageRegExp = ".*Resource create, of type 'Patient', is not supported.*")
554+
@Test(expectedExceptions = { FHIROperationException.class } , expectedExceptionsMessageRegExp = ".*Resource update, of type 'Patient', is not supported.*")
555555
public void testValidateOperationWithUpdateCreateDisabled() throws Exception {
556556
FHIRPersistence persistence = Mockito.mock(FHIRPersistence.class);
557557
Parameters input = Parameters.builder()
@@ -566,7 +566,7 @@ public void testValidateOperationWithUpdateCreateDisabled() throws Exception {
566566
.build(),
567567
Parameter.builder()
568568
.name("mode")
569-
.value(Code.of("create"))
569+
.value(Code.of("update"))
570570
.build())
571571
.build();
572572
FHIROperationContext operationContext =
@@ -732,8 +732,6 @@ public void testValidateOperationDisAllowedInteractionForCreateMode() throws Exc
732732
doThrow(ex).when(fhirResourceHelper).validateInteraction(any(), anyString());
733733

734734
validateOperation.doInvoke(operationContext, Patient.class, "1", null, input, fhirResourceHelper, null);
735-
736-
737735
}
738736

739737
}

0 commit comments

Comments
 (0)