3
3
import dev .openfeature .contrib .providers .flagd .resolver .process .model .FlagParser ;
4
4
5
5
import java .io .IOException ;
6
+ import java .net .URISyntaxException ;
6
7
import java .net .URL ;
7
8
import java .nio .file .Files ;
9
+ import java .nio .file .Path ;
8
10
import java .nio .file .Paths ;
11
+ import java .util .Objects ;
9
12
10
13
public class TestUtils {
11
14
public static final String VALID_SIMPLE = "flagConfigurations/valid-simple.json" ;
@@ -15,21 +18,23 @@ public class TestUtils {
15
18
public static final String INVALID_CFG = "flagConfigurations/invalid-configuration.json" ;
16
19
public static final String UPDATABLE_FILE = "flagConfigurations/updatableFlags.json" ;
17
20
18
-
19
21
public static String getFlagsFromResource (final String file ) throws IOException {
20
- final URL url = FlagParser .class .getClassLoader ().getResource (file );
21
- if (url == null ) {
22
- throw new IllegalStateException (String .format ("Resource %s not found" , file ));
23
- } else {
24
- return new String (Files .readAllBytes (Paths .get (url .getPath ())));
25
- }
22
+ final Path resourcePath = getResourcePathInternal (file );
23
+ return new String (Files .readAllBytes (resourcePath ));
26
24
}
27
25
28
26
public static String getResourcePath (final String relativePath ) {
29
- final URL url = FlagParser .class .getClassLoader ().getResource (relativePath );
30
- if (url == null ) {
31
- throw new IllegalStateException (String .format ("Resource %s not found" , relativePath ));
27
+ return getResourcePathInternal (relativePath ).toString ();
28
+ }
29
+
30
+ private static Path getResourcePathInternal (String file ) {
31
+ try {
32
+ URL url = Objects .requireNonNull (FlagParser .class .getClassLoader ().getResource (file ));
33
+ return Paths .get (url .toURI ());
34
+ } catch (NullPointerException e ) {
35
+ throw new IllegalStateException (String .format ("Resource %s not found" , file ), e );
36
+ } catch (URISyntaxException e ) {
37
+ throw new IllegalStateException ("Invalid resource path" , e );
32
38
}
33
- return url .getPath ();
34
39
}
35
40
}
0 commit comments