re base code
[sdc.git] / catalog-dao / src / test / java / org / openecomp / sdc / be / utils / FixtureHelpers.java
1 package org.openecomp.sdc.be.utils;
2
3 import com.google.common.io.Resources;
4
5 import java.io.IOException;
6 import java.net.URL;
7 import java.nio.charset.Charset;
8 import java.nio.charset.StandardCharsets;
9
10 /**
11  * A set of helper method for fixture files.
12  */
13 public class FixtureHelpers {
14
15     private FixtureHelpers() {
16         // singleton
17     }
18
19     /**
20      * Reads the given fixture file from the classpath (e. g. {@code src/test/resources})
21      * and returns its contents as a UTF-8 string.
22      *
23      * @param filename the filename of the fixture file
24      * @return the contents of {@code src/test/resources/{filename}}
25      * @throws IllegalArgumentException if an I/O error occurs.
26      */
27     public static String fixture(String filename) {
28         return fixture(filename, StandardCharsets.UTF_8);
29     }
30
31     /**
32      * Reads the given fixture file from the classpath (e. g. {@code src/test/resources})
33      * and returns its contents as a string.
34      *
35      * @param filename the filename of the fixture file
36      * @param charset the character set of {@code filename}
37      * @return the contents of {@code src/test/resources/{filename}}
38      * @throws IllegalArgumentException if an I/O error occurs.
39      */
40     private static String fixture(String filename, Charset charset) {
41         try {
42             URL url = Resources.getResource(filename);
43             String text = Resources.toString(url, charset);
44             return text.trim();
45         }
46         catch (IOException e) {
47             throw new IllegalArgumentException(e);
48         }
49     }
50 }