11package edu .kit .datamanager .ro_crate .writer ;
22
3+ import edu .kit .datamanager .ro_crate .Crate ;
4+ import edu .kit .datamanager .ro_crate .RoCrate ;
5+ import edu .kit .datamanager .ro_crate .entities .contextual .ContextualEntity ;
6+ import org .junit .jupiter .api .Test ;
7+
38import static org .junit .jupiter .api .Assertions .*;
9+
410class ProvenanceManagerTest {
11+ public final String OLD_VERSION = "1.0.0" ;
12+ private final ProvenanceManager OLD_PROV_MANAGER = new ProvenanceManager (() -> OLD_VERSION );
13+ private final String OLD_LIBRARY_ID = OLD_PROV_MANAGER .getLibraryId ();
14+
15+ public final String NEW_VERSION = "2.5.3" ;
16+ private final ProvenanceManager NEW_PROV_MANAGER = new ProvenanceManager (() -> NEW_VERSION );
17+ private final String NEW_LIBRARY_ID = NEW_PROV_MANAGER .getLibraryId ();
18+
19+ @ Test
20+ void should_CreateInitialEntities_WithCorrectVersion () {
21+ // Given
22+ Crate crate = new RoCrate .RoCrateBuilder ().build ();
23+
24+ // When
25+ OLD_PROV_MANAGER .addProvenanceInformation (crate );
26+
27+ // Then
28+ var entities = crate .getAllContextualEntities ();
29+ assertEquals (2 , entities .size (), "Should have created two entities" );
30+
31+ // Find ro-crate-java entity
32+ var roCrateJavaEntity = entities .stream ()
33+ .filter (e -> e .getId ().equals (OLD_LIBRARY_ID ))
34+ .findFirst ()
35+ .orElseThrow ();
36+
37+ assertEquals (OLD_VERSION , roCrateJavaEntity .getProperty ("version" ).asText ());
38+ assertEquals (OLD_VERSION , roCrateJavaEntity .getProperty ("softwareVersion" ).asText ());
39+
40+ // Find CreateAction and verify it points to correct version
41+ var createAction = entities .stream ()
42+ .filter (e -> e .getTypes ().contains ("CreateAction" ))
43+ .findFirst ()
44+ .orElseThrow ();
45+
46+ assertEquals (OLD_LIBRARY_ID , createAction .getIdProperty ("agent" ));
47+ }
48+
49+ @ Test
50+ void should_CreateDifferentEntities_WhenDifferentVersionsModifyCrate () {
51+ // Given
52+ Crate crate = new RoCrate .RoCrateBuilder ().build ();
53+
54+ // When creating with old version
55+ OLD_PROV_MANAGER .addProvenanceInformation (crate );
56+
57+ // And modifying with new version
58+ NEW_PROV_MANAGER .addProvenanceInformation (crate );
59+
60+ // Then
61+ var entities = crate .getAllContextualEntities ();
62+ assertEquals (4 , entities .size (), "Should have four entities (2 ro-crate-java + CreateAction + UpdateAction)" );
63+
64+ // Verify both version entities exist
65+ var oldVersionEntity = entities .stream ()
66+ .filter (e -> e .getId ().equals (OLD_LIBRARY_ID ))
67+ .findFirst ()
68+ .orElseThrow ();
69+ var newVersionEntity = entities .stream ()
70+ .filter (e -> e .getId ().equals (NEW_LIBRARY_ID ))
71+ .findFirst ()
72+ .orElseThrow ();
73+
74+ assertEquals (OLD_VERSION , oldVersionEntity .getProperty ("version" ).asText ());
75+ assertEquals (NEW_VERSION , newVersionEntity .getProperty ("version" ).asText ());
76+
77+ // Verify actions point to correct versions
78+ var createAction = entities .stream ()
79+ .filter (e -> e .getTypes ().contains ("CreateAction" ))
80+ .findFirst ()
81+ .orElseThrow ();
82+ var updateAction = entities .stream ()
83+ .filter (e -> e .getTypes ().contains ("UpdateAction" ))
84+ .findFirst ()
85+ .orElseThrow ();
86+
87+ assertEquals (OLD_LIBRARY_ID , createAction .getIdProperty ("agent" ),
88+ "CreateAction should point to old version" );
89+ assertEquals (NEW_LIBRARY_ID , updateAction .getIdProperty ("agent" ),
90+ "UpdateAction should point to new version" );
91+ }
92+
93+ @ Test
94+ void should_ReuseExistingVersionEntity_WhenSameVersionModifiesCrateMultipleTimes () {
95+ // Given
96+ Crate crate = new RoCrate .RoCrateBuilder ().build ();
97+
98+ // When modifying multiple times with same version
99+ OLD_PROV_MANAGER .addProvenanceInformation (crate );
100+ OLD_PROV_MANAGER .addProvenanceInformation (crate );
101+ OLD_PROV_MANAGER .addProvenanceInformation (crate );
102+
103+ // Then
104+ var entities = crate .getAllContextualEntities ();
105+
106+ // Should have one ro-crate-java entity and three actions
107+ long roCrateJavaCount = entities .stream ()
108+ .filter (e -> e .getId ().startsWith (ProvenanceManager .RO_CRATE_JAVA_ID_PREFIX .toString ()))
109+ .count ();
110+ assertEquals (1 , roCrateJavaCount , "Should have only one ro-crate-java entity" );
111+
112+ var actions = entities .stream ()
113+ .filter (e -> e .getTypes ().contains ("CreateAction" ) || e .getTypes ().contains ("UpdateAction" ))
114+ .toList ();
115+ assertEquals (3 , actions .size (), "Should have three actions" );
116+
117+ // All actions should point to the same version entity
118+ for (ContextualEntity action : actions ) {
119+ assertEquals (OLD_LIBRARY_ID , action .getIdProperty ("agent" ),
120+ "All actions should point to the same version entity" );
121+ }
122+ }
123+
124+ @ Test
125+ void should_PreserveVersionSpecificMetadata_WhenModifying () {
126+ // Given
127+ Crate crate = new RoCrate .RoCrateBuilder ().build ();
128+
129+ // When creating with old version
130+ new ProvenanceManager (() -> OLD_VERSION ).addProvenanceInformation (crate );
131+
132+ // And modifying with new version
133+ new ProvenanceManager (() -> NEW_VERSION ).addProvenanceInformation (crate );
134+
135+ // And modifying again with old version
136+ new ProvenanceManager (() -> OLD_VERSION ).addProvenanceInformation (crate );
137+
138+ // Then
139+ var entities = crate .getAllContextualEntities ();
140+
141+ // Should have exactly two ro-crate-java entities
142+ var roCrateJavaEntities = entities .stream ()
143+ .filter (e -> e .getId ().startsWith (ProvenanceManager .RO_CRATE_JAVA_ID_PREFIX .toString ()))
144+ .toList ();
145+ assertEquals (2 , roCrateJavaEntities .size (), "Should have exactly two ro-crate-java entities" );
146+
147+ // Each entity should maintain its complete metadata
148+ for (ContextualEntity entity : roCrateJavaEntities ) {
149+ assertNotNull (entity .getProperty ("name" ), "Should have name" );
150+ assertNotNull (entity .getProperty ("url" ), "Should have url" );
151+ assertNotNull (entity .getProperty ("license" ), "Should have license" );
152+ assertEquals (entity .getProperty ("version" ),
153+ entity .getProperty ("softwareVersion" ),
154+ "version and softwareVersion should match" );
155+ }
156+
157+ // Actions should point to appropriate versions
158+ var actions = entities .stream ()
159+ .filter (e -> e .getTypes ().contains ("CreateAction" ) || e .getTypes ().contains ("UpdateAction" ))
160+ .toList ();
161+ assertEquals (3 , actions .size (), "Should have three actions" );
162+
163+ // First action (CreateAction) should point to old version
164+ var createAction = actions .stream ()
165+ .filter (e -> e .getTypes ().contains ("CreateAction" ))
166+ .findFirst ()
167+ .orElseThrow ();
168+ assertEquals (OLD_LIBRARY_ID , createAction .getIdProperty ("agent" ),
169+ "CreateAction should point to old version" );
170+
171+ // Update actions should point to respective versions
172+ var updateActions = actions .stream ()
173+ .filter (e -> e .getTypes ().contains ("UpdateAction" ))
174+ .toList ();
175+ assertEquals (2 , updateActions .size (), "Should have two update actions" );
5176
6- private final String oldVersionId = new ProvenanceManager (() -> "1.0.0" ).getLibraryId ();
7- private final String newVersionId = new ProvenanceManager (() -> "2.5.3" ).getLibraryId ();
8-
177+ assertTrue (updateActions .stream ()
178+ .map (e -> e .getIdProperty ("agent" ))
179+ .allMatch (id -> id .equals (OLD_LIBRARY_ID ) || id .equals (NEW_LIBRARY_ID )),
180+ "Update actions should point to either old or new version" );
181+ }
9182}
0 commit comments