2525package org .eclipse .basyx .regression .AASServer ;
2626
2727import static org .junit .Assert .assertEquals ;
28+ import static org .junit .Assert .assertNotEquals ;
2829import static org .junit .Assert .assertTrue ;
2930
31+ import java .io .File ;
32+ import java .io .IOException ;
33+ import java .net .URISyntaxException ;
34+ import java .net .URL ;
3035import java .util .Collection ;
3136import java .util .Iterator ;
3237import java .util .Map ;
3641import javax .ws .rs .client .WebTarget ;
3742import javax .ws .rs .core .Response ;
3843
44+ import org .apache .http .HttpEntity ;
45+ import org .apache .http .client .methods .CloseableHttpResponse ;
46+ import org .apache .http .client .methods .HttpPost ;
47+ import org .apache .http .entity .ContentType ;
48+ import org .apache .http .entity .mime .MultipartEntityBuilder ;
49+ import org .apache .http .impl .client .CloseableHttpClient ;
50+ import org .apache .http .impl .client .HttpClients ;
51+ import org .eclipse .basyx .aas .aggregator .restapi .AASAggregatorProvider ;
3952import org .eclipse .basyx .aas .manager .ConnectedAssetAdministrationShellManager ;
4053import org .eclipse .basyx .aas .metamodel .connected .ConnectedAssetAdministrationShell ;
4154import org .eclipse .basyx .aas .metamodel .map .descriptor .AASDescriptor ;
4457import org .eclipse .basyx .aas .metamodel .map .descriptor .SubmodelDescriptor ;
4558import org .eclipse .basyx .aas .registration .api .IAASRegistry ;
4659import org .eclipse .basyx .aas .registration .memory .InMemoryRegistry ;
60+ import org .eclipse .basyx .components .configuration .BaSyxContextConfiguration ;
4761import org .eclipse .basyx .submodel .metamodel .api .ISubmodel ;
4862import org .eclipse .basyx .submodel .metamodel .api .submodelelement .ISubmodelElement ;
4963import org .eclipse .basyx .submodel .metamodel .api .submodelelement .ISubmodelElementCollection ;
5064import org .eclipse .basyx .submodel .metamodel .api .submodelelement .dataelement .IFile ;
5165import org .eclipse .basyx .submodel .metamodel .connected .submodelelement .ConnectedSubmodelElementCollection ;
5266import org .eclipse .basyx .submodel .metamodel .connected .submodelelement .dataelement .ConnectedFile ;
67+ import org .eclipse .basyx .vab .modelprovider .VABPathTools ;
5368import org .eclipse .basyx .vab .protocol .api .IConnectorFactory ;
5469import org .eclipse .basyx .vab .protocol .http .connector .HTTPConnectorFactory ;
5570import org .glassfish .jersey .client .JerseyClientBuilder ;
5671import org .junit .Before ;
5772import org .junit .Test ;
5873import org .slf4j .Logger ;
5974import org .slf4j .LoggerFactory ;
75+ import org .springframework .http .HttpStatus ;
6076
6177/**
6278 * Suite for testing that the XMLAAS servlet is set up correctly. The tests here
6379 * can be used by the servlet test itself and the integration test
6480 *
65- * @author schnicke, espen
81+ * @author schnicke, espen, mateusmolina
6682 *
6783 */
6884public abstract class AASXSuite {
@@ -86,6 +102,7 @@ public abstract class AASXSuite {
86102 protected static final String fileShortIdPath = "file" ;
87103
88104 // Has to be individualized by each test inheriting from this suite
105+ // Default configuration is provided by buildEnpoints method
89106 protected static String aasEndpoint ;
90107 protected static String smEndpoint ;
91108 protected static String aasAEndpoint ;
@@ -137,37 +154,26 @@ public void testGetSingleSubmodel() throws Exception {
137154
138155 @ Test
139156 public void testGetSingleModule () throws Exception {
140- final String FILE_ENDING = "basyx-temp/aasx0/files/aasx/Nameplate/marking_rcm.jpg" ;
141- final String FILE_PATH = rootEndpoint + "basyx-temp/aasx0/files/aasx/Nameplate/marking_rcm.jpg" ;
142- checkFile (FILE_PATH );
157+ final String FILE_ENDING = VABPathTools .buildPath (new String [] { "basyx-temp" , "aasx0" , "files" , "aasx" , "Nameplate" , "marking_rcm.jpg" }, 0 );
158+ checkFile (VABPathTools .concatenatePaths (rootEndpoint , FILE_ENDING ));
143159
144160 // Get the submdoel nameplate
145161 ISubmodel nameplate = manager .retrieveSubmodel (aasId , smId );
146162 // Get the submodel element collection marking_rcm
147163 ConnectedSubmodelElementCollection marking_rcm = (ConnectedSubmodelElementCollection ) nameplate .getSubmodelElements ().get ("Marking_RCM" );
148- Collection <ISubmodelElement > values = marking_rcm .getValue ();
149164
150- // navigate to the File element
151- Iterator <ISubmodelElement > iter = values .iterator ();
152- while (iter .hasNext ()) {
153- ISubmodelElement element = iter .next ();
154- if (element instanceof ConnectedFile ) {
155- ConnectedFile connectedFile = (ConnectedFile ) element ;
156- // get value of the file element
165+ ConnectedFile fileSE = retrieveFileSEFromCollection (marking_rcm );
157166
158- String fileurl = connectedFile .getValue ();
159- assertTrue (fileurl .endsWith (FILE_ENDING ));
160- }
161- }
167+ assertTrue (fileSE .getValue ().endsWith (FILE_ENDING ));
162168 }
163-
169+
164170 @ Test
165171 public void testCollidingFiles () throws Exception {
166- final String FILE_ENDING_A = "basyx-temp/ aasx1/ files/ aasx/ files/ text.txt" ;
167- final String FILE_ENDING_B = "basyx-temp/ aasx2/ files/ aasx/ files/ text.txt" ;
172+ final String FILE_ENDING_A = VABPathTools . buildPath ( new String [] { "basyx-temp" , " aasx1" , " files" , " aasx" , " files" , " text.txt" }, 0 ) ;
173+ final String FILE_ENDING_B = VABPathTools . buildPath ( new String [] { "basyx-temp" , " aasx2" , " files" , " aasx" , " files" , " text.txt" }, 0 ) ;
168174
169- checkFile (rootEndpoint + FILE_ENDING_A );
170- checkFile (rootEndpoint + FILE_ENDING_B );
175+ checkFile (VABPathTools . concatenatePaths ( rootEndpoint , FILE_ENDING_A ) );
176+ checkFile (VABPathTools . concatenatePaths ( rootEndpoint , FILE_ENDING_B ) );
171177
172178 ISubmodel smA = manager .retrieveSubmodel (aasAId , smAId );
173179 ISubmodel smB = manager .retrieveSubmodel (aasBId , smBId );
@@ -194,6 +200,63 @@ public void testAllFiles() throws Exception {
194200
195201 }
196202
203+ @ Test
204+ public void fileValueIsCorrectlyUpdated_whenFileIsUpdated () throws Exception {
205+ final String UPLOAD_ENDPOINT = VABPathTools .concatenatePaths (smEndpoint , "submodelElements" , "Marking_CRUUS" , "File" , "upload" );
206+
207+ ISubmodel nameplate = manager .retrieveSubmodel (aasId , smId );
208+ ConnectedSubmodelElementCollection marking_cruus = (ConnectedSubmodelElementCollection ) nameplate .getSubmodelElements ().get ("Marking_CRUUS" );
209+ ConnectedFile fileSE = retrieveFileSEFromCollection (marking_cruus );
210+
211+ String fileEndpointBefore = fileSE .getValue ();
212+ checkFile (fileEndpointBefore );
213+
214+ CloseableHttpResponse response = uploadDummyFileToSubmodelElement (UPLOAD_ENDPOINT , getFileFromResources ("BaSyx.png" ), ContentType .IMAGE_PNG );
215+ try {
216+ int statusCode = response .getStatusLine ().getStatusCode ();
217+
218+ assertEquals (HttpStatus .CREATED .value (), statusCode );
219+
220+ String fileEndpointAfter = fileSE .getValue ();
221+
222+ assertNotEquals (fileEndpointBefore , fileEndpointAfter );
223+ checkFile (fileEndpointAfter );
224+
225+ } finally {
226+ response .close ();
227+ }
228+ }
229+
230+ private ConnectedFile retrieveFileSEFromCollection (ConnectedSubmodelElementCollection marking_rcm ) throws Exception {
231+ Collection <ISubmodelElement > values = marking_rcm .getValue ();
232+
233+ Iterator <ISubmodelElement > iter = values .iterator ();
234+ while (iter .hasNext ()) {
235+ ISubmodelElement element = iter .next ();
236+ if (element instanceof ConnectedFile ) {
237+ return (ConnectedFile ) element ;
238+ }
239+ }
240+ throw new RuntimeException ("No File SubmodelElement found in " + marking_rcm .getIdShort ());
241+ }
242+
243+ protected static void buildEndpoints (BaSyxContextConfiguration contextConfig ) {
244+ rootEndpoint = VABPathTools .stripSlashes (contextConfig .getUrl ());
245+
246+ aasEndpoint = VABPathTools .concatenatePaths (rootEndpoint , AASAggregatorProvider .PREFIX , aasId .getEncodedURN (), "aas" );
247+ smEndpoint = VABPathTools .concatenatePaths (aasEndpoint , "submodels" , smIdShort , "submodel" );
248+
249+ String encodedAasAId = VABPathTools .encodePathElement (aasAId .getId ());
250+ aasAEndpoint = VABPathTools .concatenatePaths (rootEndpoint , AASAggregatorProvider .PREFIX , encodedAasAId , "aas" );
251+ smAEndpoint = VABPathTools .concatenatePaths (aasAEndpoint , "submodels" , smAIdShort , "submodel" );
252+
253+ String encodedAasBId = VABPathTools .encodePathElement (aasBId .getId ());
254+ aasBEndpoint = VABPathTools .concatenatePaths (rootEndpoint , AASAggregatorProvider .PREFIX , encodedAasBId , "aas" );
255+ smBEndpoint = VABPathTools .concatenatePaths (aasBEndpoint , "submodels" , smBIdShort , "submodel" );
256+
257+ logger .info ("AAS URL for servlet test: " + aasEndpoint );
258+ }
259+
197260 private void checkElementCollectionFiles (Collection <ISubmodelElement > elements ) {
198261 for (ISubmodelElement element : elements ) {
199262 if (element instanceof IFile ) {
@@ -226,4 +289,23 @@ private void checkFile(String absolutePath) {
226289 private ConnectedAssetAdministrationShell getConnectedAssetAdministrationShell () throws Exception {
227290 return manager .retrieveAAS (aasId );
228291 }
292+
293+ private File getFileFromResources (String filename ) throws IOException , URISyntaxException {
294+ URL resource = getClass ().getClassLoader ().getResource (filename );
295+ if (resource == null )
296+ throw new IllegalArgumentException ("File not found!" );
297+
298+ return new File (resource .toURI ());
299+
300+ }
301+
302+ private CloseableHttpResponse uploadDummyFileToSubmodelElement (String endpoint , File file , ContentType contentType ) throws IOException {
303+ CloseableHttpClient client = HttpClients .createDefault ();
304+
305+ HttpEntity fileEntity = MultipartEntityBuilder .create ().addBinaryBody ("file" , file , contentType , file .getName ()).build ();
306+ HttpPost postRequest = new HttpPost (endpoint );
307+ postRequest .setEntity (fileEntity );
308+
309+ return client .execute (postRequest );
310+ }
229311}
0 commit comments