From c197f1673748c12202c9ef85ab344e6d2dca951f Mon Sep 17 00:00:00 2001 From: Yauhenikapl Date: Tue, 10 Sep 2024 19:22:00 +0300 Subject: [PATCH] Fix tests --- .../aas/AasToAspectModelGenerator.java | 24 +++++++++++++------ 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/core/esmf-aspect-model-aas-generator/src/main/java/org/eclipse/esmf/aspectmodel/aas/AasToAspectModelGenerator.java b/core/esmf-aspect-model-aas-generator/src/main/java/org/eclipse/esmf/aspectmodel/aas/AasToAspectModelGenerator.java index 1a76acb7..c688f90e 100644 --- a/core/esmf-aspect-model-aas-generator/src/main/java/org/eclipse/esmf/aspectmodel/aas/AasToAspectModelGenerator.java +++ b/core/esmf-aspect-model-aas-generator/src/main/java/org/eclipse/esmf/aspectmodel/aas/AasToAspectModelGenerator.java @@ -19,6 +19,7 @@ import java.net.URI; import java.util.ArrayDeque; import java.util.Arrays; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Locale; @@ -180,20 +181,29 @@ private Collector, ArrayDeque> reverseOrder() { } private String iriToReversedHostNameNotation( final IRI iri ) { - final URI uri = URI.create( iri.toString().contains( "://" ) ? iri.toString() : "https://" + iri ); + URI uri; + try { + uri = URI.create( iri.toString().contains( "://" ) ? iri.toString() : "https://" + iri ); + } catch ( IllegalArgumentException e ) { + throw new IllegalArgumentException( "Incorrect IRI: " + iri, e ); + } - final String[] hostParts = uri.getHost().split( "\\." ); - final String[] pathParts = uri.getPath().split( "/" ); + if ( uri.getHost() == null ) { + throw new IllegalArgumentException( "URI doesn't contain host: " + uri ); + } - final String reversedHost = String.join( ".", Arrays.stream( hostParts ) - .collect( reverseOrder() ) ); + final String[] hostParts = uri.getHost().split( "\\." ); + final List hostPartsList = Arrays.asList( hostParts ); + Collections.reverse( hostPartsList ); + final String reversedHost = String.join( ".", hostPartsList ); + final String[] pathParts = uri.getPath().split( "/" ); final String path = Arrays.stream( pathParts ) .filter( StringUtils::isNotBlank ) - .limit( pathParts.length - 2 ) + .limit( Math.max( 0, pathParts.length - 2 ) ) .collect( Collectors.joining( "." ) ); - return reversedHost + "." + path; + return reversedHost + ( path.isEmpty() ? "" : "." + path ); } private Optional iri( final String lexicalRepresentation ) {