15
15
16
16
import static org .assertj .core .api .Assertions .assertThat ;
17
17
18
+ import java .io .File ;
19
+ import java .io .FileInputStream ;
20
+ import java .io .FileNotFoundException ;
21
+ import java .net .URISyntaxException ;
22
+ import java .nio .file .Path ;
23
+ import java .nio .file .Paths ;
24
+ import java .util .List ;
18
25
import java .util .Map ;
19
26
import java .util .Optional ;
20
27
import java .util .function .Function ;
21
28
import java .util .stream .Collectors ;
22
29
30
+ import org .eclipse .esmf .aspectmodel .resolver .FileSystemStrategy ;
31
+ import org .eclipse .esmf .aspectmodel .resolver .ResolutionStrategy ;
23
32
import org .eclipse .esmf .metamodel .AbstractEntity ;
33
+ import org .eclipse .esmf .metamodel .AspectModel ;
24
34
import org .eclipse .esmf .metamodel .ComplexType ;
35
+ import org .eclipse .esmf .samm .KnownVersion ;
25
36
import org .eclipse .esmf .test .TestAspect ;
26
37
import org .eclipse .esmf .test .TestResources ;
27
38
28
39
import org .junit .jupiter .api .Test ;
29
40
30
41
class AspectModelLoaderTest {
31
42
@ Test
32
- public void testOfAbstractEntityCyclomaticCreation () {
43
+ void testOfAbstractEntityCyclomaticCreation () {
33
44
final Map <String , ComplexType > entities =
34
45
TestResources .load ( TestAspect .ASPECT_WITH_MULTIPLE_ENTITIES_SAME_EXTEND ).elements ().stream ()
35
46
.filter ( ComplexType .class ::isInstance )
@@ -43,4 +54,134 @@ public void testOfAbstractEntityCyclomaticCreation() {
43
54
assertThat ( entities ).extracting ( "testEntityTwo" ).isInstanceOfSatisfying ( ComplexType .class , type ->
44
55
assertThat ( type ).extracting ( ComplexType ::getExtends ).extracting ( Optional ::get ).isSameAs ( abstractEntity ) );
45
56
}
57
+
58
+ @ Test
59
+ void testLoadAspectModelFromZipArchiveFile () {
60
+ final Path archivePath = getPackage ( "namespaces.zip" );
61
+ final AspectModel aspectModel = new AspectModelLoader ().loadNamespacePackage ( new File ( archivePath .toString () ) );
62
+
63
+ assertThat ( aspectModel .namespaces () ).hasSize ( 2 );
64
+ assertThat ( aspectModel .namespaces ().get ( 0 ).getName () ).contains ( "urn:samm:org.eclipse.examples:1.1.0" );
65
+ assertThat ( aspectModel .namespaces ().get ( 1 ).getName () ).contains ( "urn:samm:org.eclipse.examples:1.0.0" );
66
+
67
+ final List <String > aspectsNames = List .of ( "Movement2" , "Movement3" , "Movement" , "SimpleAspect" );
68
+
69
+ assertThat ( aspectModel .files () ).hasSize ( 4 );
70
+ assertThat ( aspectModel .files () )
71
+ .anySatisfy ( aspectModelFile -> {
72
+ assertThat ( aspectsNames ).contains ( aspectModelFile .aspect ().getName () );
73
+ } );
74
+ }
75
+
76
+ @ Test
77
+ void testLoadAspectModelFromZipArchiveInputStream () throws FileNotFoundException {
78
+ final Path archivePath = getPackage ( "namespaces.zip" );
79
+ final AspectModel aspectModel = new AspectModelLoader ().loadNamespacePackage ( new FileInputStream ( archivePath .toString () ) );
80
+
81
+ assertThat ( aspectModel .namespaces () ).hasSize ( 2 );
82
+ assertThat ( aspectModel .namespaces ().get ( 0 ).getName () ).contains ( "urn:samm:org.eclipse.examples:1.1.0" );
83
+ assertThat ( aspectModel .namespaces ().get ( 1 ).getName () ).contains ( "urn:samm:org.eclipse.examples:1.0.0" );
84
+
85
+ final List <String > aspectsNames = List .of ( "Movement2" , "Movement3" , "Movement" , "SimpleAspect" );
86
+
87
+ assertThat ( aspectModel .files () ).hasSize ( 4 );
88
+ assertThat ( aspectModel .files () )
89
+ .anySatisfy ( aspectModelFile -> {
90
+ assertThat ( aspectsNames ).contains ( aspectModelFile .aspect ().getName () );
91
+ } );
92
+ }
93
+
94
+ /**
95
+ * Test migration to the latest version of Aspect Model in Archive
96
+ */
97
+ @ Test
98
+ void testLoadAspectModelFromZipArchive2_0_0 () throws FileNotFoundException {
99
+ final Path archivePath = getPackage ( "namespaces_with_old_version.zip" );
100
+ final AspectModel aspectModel = new AspectModelLoader ().loadNamespacePackage ( new FileInputStream ( archivePath .toString () ) );
101
+
102
+ assertThat ( aspectModel .namespaces () ).hasSize ( 2 );
103
+ assertThat ( aspectModel .namespaces ().get ( 0 ).getName () ).contains ( "urn:samm:org.eclipse.examples:1.1.0" );
104
+ assertThat ( aspectModel .namespaces ().get ( 1 ).getName () ).contains ( "urn:samm:org.eclipse.examples:1.0.0" );
105
+
106
+ final List <String > aspectsNames = List .of ( "Movement2" , "Movement3" , "Movement4" , "Movement" , "SimpleAspect" );
107
+
108
+ assertThat ( aspectModel .files () ).hasSize ( 5 );
109
+ assertThat ( aspectModel .files () )
110
+ .anySatisfy ( aspectModelFile -> {
111
+ assertThat ( aspectsNames ).contains ( aspectModelFile .aspect ().getName () );
112
+ } );
113
+ }
114
+
115
+ @ Test
116
+ void testLoadAspectModelFromZipArchiveAspectModelsRoot () throws FileNotFoundException {
117
+ final Path archivePath = getPackage ( "namespaces-aspect-models-root.zip" );
118
+ final AspectModel aspectModel = new AspectModelLoader ().loadNamespacePackage ( new FileInputStream ( archivePath .toString () ) );
119
+
120
+ assertThat ( aspectModel .namespaces () ).hasSize ( 2 );
121
+ assertThat ( aspectModel .namespaces ().get ( 0 ).getName () ).contains ( "urn:samm:org.eclipse.examples:1.1.0" );
122
+ assertThat ( aspectModel .namespaces ().get ( 1 ).getName () ).contains ( "urn:samm:org.eclipse.examples:1.0.0" );
123
+
124
+ final List <String > aspectsNames = List .of ( "Movement2" , "Movement3" , "Movement" , "SimpleAspect" );
125
+
126
+ assertThat ( aspectModel .files () ).hasSize ( 4 );
127
+ assertThat ( aspectModel .files () )
128
+ .anySatisfy ( aspectModelFile -> {
129
+ assertThat ( aspectsNames ).contains ( aspectModelFile .aspect ().getName () );
130
+ } );
131
+ }
132
+
133
+ @ Test
134
+ void testLoadAspectModelFromZipArchiveAspectModelsSubfolder () throws FileNotFoundException {
135
+ final Path archivePath = getPackage ( "namespaces-aspect-models-subfolder.zip" );
136
+ final AspectModel aspectModel = new AspectModelLoader ().loadNamespacePackage ( new FileInputStream ( archivePath .toString () ) );
137
+
138
+ assertThat ( aspectModel .namespaces () ).hasSize ( 2 );
139
+ assertThat ( aspectModel .namespaces ().get ( 0 ).getName () ).contains ( "urn:samm:org.eclipse.examples:1.1.0" );
140
+ assertThat ( aspectModel .namespaces ().get ( 1 ).getName () ).contains ( "urn:samm:org.eclipse.examples:1.0.0" );
141
+
142
+ final List <String > aspectsNames = List .of ( "Movement2" , "Movement3" , "Movement" , "SimpleAspect" );
143
+
144
+ assertThat ( aspectModel .files () ).hasSize ( 4 );
145
+ assertThat ( aspectModel .files () )
146
+ .anySatisfy ( aspectModelFile -> {
147
+ assertThat ( aspectsNames ).contains ( aspectModelFile .aspect ().getName () );
148
+ } );
149
+ }
150
+
151
+ @ Test
152
+ void testLoadAspectModelFromZipArchiveWithSharedProperty () throws FileNotFoundException , URISyntaxException {
153
+ final Path archivePath = getPackage ( "namespace-with-shared-property.zip" );
154
+
155
+ final File aspectModelsRootDirectory = new File (
156
+ AspectModelLoaderTest .class .getClassLoader ()
157
+ .getResource ( KnownVersion .getLatest ().toString ().toLowerCase () )
158
+ .toURI ().getPath () );
159
+
160
+ final ResolutionStrategy urnStrategy = new FileSystemStrategy ( aspectModelsRootDirectory .toPath () );
161
+
162
+ final AspectModel aspectModel = new AspectModelLoader ( urnStrategy ).loadNamespacePackage (
163
+ new FileInputStream ( archivePath .toString () ) );
164
+
165
+ assertThat ( aspectModel .namespaces () ).hasSize ( 1 );
166
+ assertThat ( aspectModel .namespaces ().get ( 0 ).getName () ).contains ( "urn:samm:org.eclipse.esmf.test:1.0.0" );
167
+
168
+ final List <String > aspectsNames = List .of ( "Movement" );
169
+
170
+ assertThat ( aspectModel .files () ).hasSize ( 2 );
171
+ assertThat ( aspectModel .files () )
172
+ .anySatisfy ( aspectModelFile -> {
173
+ if ( !aspectModelFile .aspects ().isEmpty () ) {
174
+ assertThat ( aspectsNames ).contains ( aspectModelFile .aspect ().getName () );
175
+ }
176
+ } );
177
+ }
178
+
179
+ /**
180
+ * Returns the File object for a test model file
181
+ */
182
+ private Path getPackage ( final String packageName ) {
183
+ return Paths .get ( String .format (
184
+ "%s/../../core/esmf-test-aspect-models/src/main/resources/packages/%s" ,
185
+ System .getProperty ( "user.dir" ), packageName ) );
186
+ }
46
187
}
0 commit comments