2626
2727import static org .assertj .core .api .Assertions .assertThat ;
2828
29+ import java .util .Arrays ;
30+ import java .util .List ;
31+
2932import org .bson .Document ;
33+ import org .eclipse .digitaltwin .basyx .core .pagination .CursorResult ;
34+ import org .eclipse .digitaltwin .basyx .core .pagination .PaginationInfo ;
35+ import org .eclipse .digitaltwin .basyx .submodelregistry .model .SubmodelDescriptor ;
36+ import org .eclipse .digitaltwin .basyx .submodelregistry .service .configuration .MongoDbConfiguration ;
37+ import org .eclipse .digitaltwin .basyx .submodelregistry .service .storage .SubmodelRegistryStorage ;
3038import org .junit .Test ;
3139import org .springframework .beans .factory .annotation .Autowired ;
3240import org .springframework .boot .autoconfigure .EnableAutoConfiguration ;
3341import org .springframework .data .mongodb .core .MongoTemplate ;
42+ import org .springframework .data .mongodb .core .query .Query ;
3443import org .springframework .test .context .ContextConfiguration ;
3544import org .springframework .test .context .TestPropertySource ;
3645
@@ -47,10 +56,60 @@ public class MongoDbSubmodelRegistryStorageTest extends SubmodelRegistryStorageT
4756 @ Autowired
4857 private MongoTemplate template ;
4958
59+ @ Autowired
60+ private SubmodelRegistryStorage storage ;
61+
62+ @ Autowired
63+ private MongoDbConfiguration configuration ;
64+
5065 @ Test
5166 public void whenGetById_NotAllDocumentsScannedButIndexUsed () {
5267 MongoCollection <Document > collection = template .getCollection ("submodeldescriptors" );
5368 Document doc = collection .find (new Document ("_id" , "11" )).explain (ExplainVerbosity .QUERY_PLANNER );
5469 assertThat (doc .toJson ()).doesNotContain ("\" COLLSCAN\" " );
5570 }
71+
72+ @ Test
73+ public void givenLegacySupplementalSemanticId_whenGetById_thenDescriptorContainsSupplementalSemanticField () {
74+ template .remove (new Query (), configuration .collectionName );
75+ template .save (createLegacyDocument ("legacy-get-1" ), configuration .collectionName );
76+
77+ SubmodelDescriptor descriptor = storage .getSubmodelDescriptor ("legacy-get-1" );
78+ Document writtenDescriptor = new Document ();
79+ template .getConverter ().write (descriptor , writtenDescriptor );
80+
81+ assertThat (extractSupplementalSemanticField (writtenDescriptor )).isNotNull ();
82+ assertThat (extractSupplementalSemanticField (writtenDescriptor )).isInstanceOf (List .class );
83+ assertThat ((List <?>) extractSupplementalSemanticField (writtenDescriptor )).hasSize (1 );
84+ }
85+
86+ @ Test
87+ public void givenLegacySupplementalSemanticId_whenGetAll_thenDescriptorContainsSupplementalSemanticField () {
88+ template .remove (new Query (), configuration .collectionName );
89+ template .save (createLegacyDocument ("legacy-getall-1" ), configuration .collectionName );
90+
91+ CursorResult <List <SubmodelDescriptor >> result = storage .getAllSubmodelDescriptors (PaginationInfo .NO_LIMIT );
92+ SubmodelDescriptor descriptor = result .getResult ().stream ().filter (x -> "legacy-getall-1" .equals (x .getId ())).findFirst ().orElse (null );
93+ assertThat (descriptor ).isNotNull ();
94+
95+ Document writtenDescriptor = new Document ();
96+ template .getConverter ().write (descriptor , writtenDescriptor );
97+
98+ assertThat (extractSupplementalSemanticField (writtenDescriptor )).isNotNull ();
99+ assertThat (extractSupplementalSemanticField (writtenDescriptor )).isInstanceOf (List .class );
100+ assertThat ((List <?>) extractSupplementalSemanticField (writtenDescriptor )).hasSize (1 );
101+ }
102+
103+ private Object extractSupplementalSemanticField (Document descriptorDocument ) {
104+ if (descriptorDocument .containsKey ("supplementalSemanticIds" )) {
105+ return descriptorDocument .get ("supplementalSemanticIds" );
106+ }
107+ return descriptorDocument .get ("supplementalSemanticId" );
108+ }
109+
110+ private Document createLegacyDocument (String id ) {
111+ Document key = new Document ("type" , "GlobalReference" ).append ("value" , "urn:test:" + id );
112+ Document reference = new Document ("type" , "ExternalReference" ).append ("keys" , Arrays .asList (key ));
113+ return new Document ("_id" , id ).append ("idShort" , "short-" + id ).append ("endpoints" , Arrays .asList ()).append ("supplementalSemanticId" , Arrays .asList (reference ));
114+ }
56115}
0 commit comments