Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Handle null SemanticModelStatus with 400 Bad Request in save and update APIs #290

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

import javax.annotation.Nullable;

import org.apache.commons.lang3.ObjectUtils;
import org.apache.jena.arq.querybuilder.UpdateBuilder;
import org.apache.jena.query.Query;
import org.apache.jena.query.QuerySolution;
Expand Down Expand Up @@ -106,7 +107,7 @@ public SemanticModel getModel( final AspectModelUrn urn ) {

@Override
public SemanticModel updateModel( String urn, SemanticModelStatus status ) {

validateStatusParameter(status);
SemanticModel semanticModel = Optional.ofNullable( findByUrn(
AspectModelUrn.fromUrn( urn ) ) ).orElseThrow( () -> new IllegalArgumentException(
String.format( "Invalid URN %s",
Expand All @@ -127,6 +128,7 @@ public SemanticModel updateModel( String urn, SemanticModelStatus status ) {

@Override
public SemanticModel save( SemanticModelType type, String newModel, SemanticModelStatus status ) {
validateStatusParameter(status);
final Model rdfModel = sdsSdk.load( newModel.getBytes( StandardCharsets.UTF_8 ) );
final AspectModelUrn modelUrn = sdsSdk.getAspectUrn( rdfModel );
Optional<ModelPackage> existsByPackage = findByPackageByUrn( ModelPackageUrn.fromUrn( modelUrn ) );
Expand Down Expand Up @@ -376,4 +378,9 @@ private static SemanticModelType determineModelType(String aspectUrn){
return SemanticModelType.SAMM;
}
}
private void validateStatusParameter(SemanticModelStatus status) {
if (ObjectUtils.allNull(status)) {
throw new IllegalArgumentException("SemanticModelStatus cannot be null. Valid values are: DRAFT, RELEASED, STANDARDIZED, DEPRECATED.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1069,5 +1069,25 @@ public void testUpdateModelStatusByInvalidURN() throws Exception {
.andExpect( status().isBadRequest() )
.andExpect( jsonPath( "$.error.message", containsString( "Invalid URN urn" ) ) );
}

}
@Test
public void testUpdateModelWithNullStatusExpectBadRequest() throws Exception {
String urnPrefix = "urn:samm:org.eclipse.tractusx.valid.save:2.0.0#";
mvc.perform(
put( TestUtils.createValidModelRequest(urnPrefix),null)
)
.andDo( MockMvcResultHandlers.print() )
.andExpect( status().isBadRequest() )
.andExpect( jsonPath( "$.error.message", containsString( "SemanticModelStatus cannot be null" ) ) );
}

@Test
public void testSaveModelWithNullStatusExpectBadRequest() throws Exception {
String urnPrefix = "urn:samm:org.eclipse.tractusx.valid.save:2.0.0#";
mvc.perform(
post( TestUtils.createValidModelRequest(urnPrefix),null)
)
.andDo( MockMvcResultHandlers.print() )
.andExpect( status().isBadRequest() )
.andExpect( jsonPath( "$.error.message", containsString( "SemanticModelStatus cannot be null" ) ) );
}
}