Skip to content

Commit c279989

Browse files
committed
Add endpoint to check if model element exists in workspace
Introduces a new API endpoint in ModelController to verify if an Aspect Model element exists in a file other than the specified one. Implements the corresponding checkElementExists method in ModelService and adds related unit tests.
1 parent 9be397b commit c279989

File tree

3 files changed

+52
-21
lines changed

3 files changed

+52
-21
lines changed

aspect-model-editor-service/src/main/java/org/eclipse/esmf/ame/services/ModelService.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353

5454
import io.micronaut.http.multipart.CompletedFileUpload;
5555
import jakarta.inject.Singleton;
56+
import org.checkerframework.checker.nullness.qual.Nullable;
5657
import org.slf4j.Logger;
5758
import org.slf4j.LoggerFactory;
5859

@@ -64,7 +65,7 @@
6465
public class ModelService {
6566
private static final Logger LOG = LoggerFactory.getLogger( ModelService.class );
6667

67-
private static final String sammStructureInfo =
68+
private static final String SAMM_STRUCTURE_INFO =
6869
"Please check whether the SAMM structure has been followed in the workspace: " + "Namespace/Version/Aspect model.";
6970

7071
private final AspectModelValidator aspectModelValidator;
@@ -77,7 +78,7 @@ public ModelService( final AspectModelValidator aspectModelValidator, final Aspe
7778
this.modelPath = modelPath;
7879
}
7980

80-
public AspectModelResult getModel( final AspectModelUrn aspectModelUrn, final String filePath ) {
81+
public AspectModelResult getModel( final AspectModelUrn aspectModelUrn, final @Nullable String filePath ) {
8182
try {
8283
final AspectModel aspectModel = ( filePath != null ) ?
8384
ModelUtils.loadModelFromFile( modelPath, filePath, aspectModelLoader ) :
@@ -172,9 +173,9 @@ public Map<String, List<Version>> getAllNamespaces( final boolean onlyAspectMode
172173
return new ModelGroupingUtils( aspectModelLoader ).groupModelsByNamespaceAndVersion( aspectModelLoader.listContents(),
173174
onlyAspectModels );
174175
} catch ( final UnsupportedVersionException e ) {
175-
LOG.error( "{} There is a loose .ttl file somewhere — remove it along with any other non-standardized files.", sammStructureInfo,
176+
LOG.error( "{} There is a loose .ttl file somewhere — remove it along with any other non-standardized files.", SAMM_STRUCTURE_INFO,
176177
e );
177-
throw new FileReadException( sammStructureInfo + " Remove all non-standardized files." );
178+
throw new FileReadException( SAMM_STRUCTURE_INFO + " Remove all non-standardized files." );
178179
}
179180
}
180181

@@ -248,4 +249,15 @@ private void applyNamespaceVersionChange( final AspectModel aspectModel, final L
248249
throw new CreateFileException( "Cannot create file %s on workspace", e );
249250
}
250251
}
252+
253+
public boolean checkElementExists( final AspectModelUrn aspectModelUrn, final String fileName ) {
254+
try {
255+
256+
System.out.println( loadModelFromUrn( aspectModelUrn ).files() );
257+
return loadModelFromUrn( aspectModelUrn ).files().stream()
258+
.anyMatch( f -> !fileName.equals( f.filename().orElse( "" ) ) );
259+
} catch ( final ModelResolutionException e ) {
260+
return false;
261+
}
262+
}
251263
}

aspect-model-editor-service/src/test/java/org/eclipse/esmf/ame/services/ModelServiceTest.java

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,15 @@ void testGetModel() throws ModelResolutionException {
9595
assertTrue( result.filename().get().contains( "Movement.ttl" ) );
9696
}
9797

98+
@Test
99+
void testCheckElementExists() throws ModelResolutionException {
100+
assertTrue( modelService.checkElementExists( NAMESPACE.withName( TEST_MODEL_FOR_SERVICE ), "Example.ttl" ) );
101+
assertFalse( modelService.checkElementExists( NAMESPACE.withName( TEST_MODEL_FOR_SERVICE ), "Movement.ttl" ) );
102+
}
103+
98104
@Test()
99105
void testGetModelThrowsNotFoundException() {
100-
assertThrows( FileNotFoundException.class, () ->
101-
modelService.getModel( NAMESPACE.withName( TEST_MODEL_NOT_FOUND ), TEST_FILEPATH ) );
106+
assertThrows( FileNotFoundException.class, () -> modelService.getModel( NAMESPACE.withName( TEST_MODEL_NOT_FOUND ), TEST_FILEPATH ) );
102107
}
103108

104109
@Test
@@ -120,8 +125,8 @@ void testSaveModel() {
120125
final Path fileToReplace = Path.of( TEST_NAMESPACE_PATH.toString(), TEST_MODEL_FOR_SERVICE + FILE_EXTENSION );
121126
final String turtleData = Files.readString( fileToReplace, StandardCharsets.UTF_8 );
122127

123-
modelService.createOrSaveModel( turtleData, NAMESPACE.withName( TEST_MODEL_FOR_SERVICE ),
124-
TEST_MODEL_FOR_SERVICE + FILE_EXTENSION, RESOURCE_PATH.toAbsolutePath() );
128+
modelService.createOrSaveModel( turtleData, NAMESPACE.withName( TEST_MODEL_FOR_SERVICE ), TEST_MODEL_FOR_SERVICE + FILE_EXTENSION,
129+
RESOURCE_PATH.toAbsolutePath() );
125130
} );
126131
}
127132

@@ -194,15 +199,14 @@ void testMigrateWorkspaceWithoutVersionUpgrade() throws IOException {
194199

195200
assertTrue( migrationResult.success() );
196201

197-
final String migratedModelOne = Files.readString( new File( MIGRATE_WORKSPACE_ONE + File.separator + "ToMigrateOne.ttl" ).toPath()
198-
.toAbsolutePath(),
199-
StandardCharsets.UTF_8 );
202+
final String migratedModelOne = Files.readString(
203+
new File( MIGRATE_WORKSPACE_ONE + File.separator + "ToMigrateOne.ttl" ).toPath().toAbsolutePath(), StandardCharsets.UTF_8 );
200204
final String migratedModelTwo = Files.readString( new File( MIGRATE_WORKSPACE_ONE + File.separator + "ToMigrateTwo.ttl" ).toPath(),
201205
StandardCharsets.UTF_8 );
202-
final String migratedModelThree = Files.readString(
203-
new File( MIGRATE_WORKSPACE_TWO + File.separator + "ToMigrateOne.ttl" ).toPath(), StandardCharsets.UTF_8 );
204-
final String migratedModelFour = Files.readString(
205-
new File( MIGRATE_WORKSPACE_TWO + File.separator + "ToMigrateTwo.ttl" ).toPath(), StandardCharsets.UTF_8 );
206+
final String migratedModelThree = Files.readString( new File( MIGRATE_WORKSPACE_TWO + File.separator + "ToMigrateOne.ttl" ).toPath(),
207+
StandardCharsets.UTF_8 );
208+
final String migratedModelFour = Files.readString( new File( MIGRATE_WORKSPACE_TWO + File.separator + "ToMigrateTwo.ttl" ).toPath(),
209+
StandardCharsets.UTF_8 );
206210

207211
checkMigratedModel( migratedModelOne );
208212
checkMigratedModel( migratedModelTwo );
@@ -217,12 +221,10 @@ void testMigrateWorkspaceWithVersionUpgrade() throws IOException {
217221
assertTrue( migrationResult.success() );
218222

219223
final String migratedModelOne = Files.readString(
220-
new File( MIGRATE_WORKSPACE_ONE_NEW_VERSION + File.separator + "ToMigrateOne.ttl" ).toPath()
221-
.toAbsolutePath(),
224+
new File( MIGRATE_WORKSPACE_ONE_NEW_VERSION + File.separator + "ToMigrateOne.ttl" ).toPath().toAbsolutePath(),
222225
StandardCharsets.UTF_8 );
223226
final String migratedModelTwo = Files.readString(
224-
new File( MIGRATE_WORKSPACE_ONE_NEW_VERSION + File.separator + "ToMigrateTwo.ttl" ).toPath(),
225-
StandardCharsets.UTF_8 );
227+
new File( MIGRATE_WORKSPACE_ONE_NEW_VERSION + File.separator + "ToMigrateTwo.ttl" ).toPath(), StandardCharsets.UTF_8 );
226228
final String migratedModelThree = Files.readString(
227229
new File( MIGRATE_WORKSPACE_TWO_NEW_VERSION + File.separator + "ToMigrateOne.ttl" ).toPath(), StandardCharsets.UTF_8 );
228230
final String migratedModelFour = Files.readString(

aspect-model-editor-web/src/main/java/org/eclipse/esmf/ame/api/ModelController.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,23 @@ public HttpResponse<String> getModel( @Header( URN ) final Optional<String> urn,
8282
return HttpResponse.ok( modelService.getModel( aspectModelUrn, path ).content() );
8383
}
8484

85+
/**
86+
* Checks if an Aspect Model element exists in the workspace.
87+
* <p>
88+
* This endpoint verifies the existence of an Aspect Model element identified by its URN
89+
* and checks if it exists in a file with a name different from the provided file name.
90+
*
91+
* @param urn the Aspect Model URN header parameter in the format urn:samm:namespace:version#AspectModelElement
92+
* @param fileName the file name to exclude from the existence check
93+
* @return True if the element exists in a different file, false otherwise
94+
*/
95+
@Get( uri = "check-element", consumes = MediaType.APPLICATION_JSON )
96+
public HttpResponse<Boolean> checkElementExists( @Header( URN ) final Optional<String> urn,
97+
@QueryValue() final String fileName ) {
98+
final AspectModelUrn aspectModelUrn = parseAspectModelUrn( urn );
99+
return HttpResponse.ok( modelService.checkElementExists( aspectModelUrn, fileName ) );
100+
}
101+
85102
/**
86103
* Method used to return multiple turtle files in batch based on a list of Aspect Model URNs.
87104
* Each URN consists of urn:samm:namespace:version#AspectModelElement
@@ -96,8 +113,8 @@ public HttpResponse<List<FileInformation>> getModelsBatch( @Body final List<File
96113
final AspectModelUrn urn = parseAspectModelUrn( Optional.of( entry.aspectModelUrn() ) );
97114
final AspectModelResult aspectModelResult = modelService.getModel( urn, null );
98115

99-
final FileInformation fileInformation = new FileInformation( entry.absoluteName(), entry.aspectModelUrn(),
100-
entry.modelVersion(), aspectModelResult.content(), aspectModelResult.filename().orElse( "" ) );
116+
final FileInformation fileInformation = new FileInformation( entry.absoluteName(), entry.aspectModelUrn(), entry.modelVersion(),
117+
aspectModelResult.content(), aspectModelResult.filename().orElse( "" ) );
101118
fileInformations.add( fileInformation );
102119
} catch ( final Exception e ) {
103120
throw new FileNotFoundException(

0 commit comments

Comments
 (0)