Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,23 @@
package org.eclipse.esmf.aspectmodel.resolver.fs;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Objects;
import java.util.stream.Stream;

import org.eclipse.esmf.aspectmodel.urn.AspectModelUrn;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public abstract class ModelsRoot {

private static final Logger LOG = LoggerFactory.getLogger( ModelsRoot.class );
private static final File EMPTY_FILE = new File( "" );
private final Path root;

protected ModelsRoot( final Path root ) {
Expand All @@ -43,6 +52,19 @@ public Stream<Path> paths() {
public abstract Path directoryForNamespace( final AspectModelUrn urn );

public File determineAspectModelFile( final AspectModelUrn urn ) {
return directoryForNamespace( urn ).resolve( urn.getName() + ".ttl" ).toFile();
Path path = directoryForNamespace( urn ).resolve( urn.getName() + ".ttl" );
return resolveByCanonicalPath( path );
}

private static File resolveByCanonicalPath( final Path path ) {
File file = path.toFile();
try {
if ( Objects.equals( path.normalize().toString(), file.getCanonicalPath() ) ) {
return file;
}
} catch ( IOException exception ) {
LOG.error( "Error resolving canonical path for file: {}", file.getPath(), exception );
}
return EMPTY_FILE;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package org.eclipse.esmf.aspectmodel.resolver.fs;

import static org.assertj.core.api.Assertions.assertThat;

import java.io.File;
import java.lang.reflect.Method;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.junit.jupiter.api.Test;

class ModelsRootTest {

private static final File EMPTY_FILE = new File( "" );

@Test
void resolveByCanonicalPathShouldReturnFileWhenCanonicalPathMatches() throws Exception {
Path testPath = Paths.get( "src/test/resources/resolve", "Aspect.ttl" ).toAbsolutePath();

File result = invokeResolveByCanonicalPath( testPath );

assertThat( result )
.matches( File::exists )
.isEqualTo( testPath.toFile() );
}

@Test
void resolveByCanonicalPathShouldReturnFileWhenCanonicalPathMatchesForSpecificPath() throws Exception {
Path testPath = Paths.get( "src/test/resources/resolve_empty/../resolve", "Aspect.ttl" ).toAbsolutePath();

File result = invokeResolveByCanonicalPath( testPath );

assertThat( result )
.matches( File::exists )
.isEqualTo( testPath.toFile() );
}

@Test
void resolveByCanonicalPathShouldReturnEmptyFileWhenCanonicalPathDoesNotMatch() throws Exception {
Path invalidPath = Paths.get( "src/test/resources/resolve", "aspect.ttl" ).toAbsolutePath();

File result = invokeResolveByCanonicalPath( invalidPath );

assertThat( result ).isEqualTo( EMPTY_FILE );
}

@Test
void resolveByCanonicalPathShouldReturnEmptyFileWhenIOExceptionOccurs() throws Exception {
Path pathCausingIOException = Paths.get( "pathCausingIOException" );

File result = invokeResolveByCanonicalPath( pathCausingIOException );

assertThat( result ).isEqualTo( EMPTY_FILE );
}

private static File invokeResolveByCanonicalPath( Path path ) throws Exception {
Method method = ModelsRoot.class.getDeclaredMethod( "resolveByCanonicalPath", Path.class );
method.setAccessible( true );
return (File) method.invoke( null, path );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright (c) 2021 Robert Bosch Manufacturing Solutions GmbH
#
# See the AUTHORS file(s) distributed with this work for additional
# information regarding authorship.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
#
# SPDX-License-Identifier: MPL-2.0

@prefix : <urn:samm:org.eclipse.esmf.test:1.0.0#> .
@prefix samm: <urn:samm:org.eclipse.esmf.samm:meta-model:1.0.0#> .

:TestAspect a samm:Aspect ;
samm:name "TestAspect" ;
samm:preferredName "Test Aspect"@en ;
samm:properties ( ) ;
samm:operations ( ) .