|
| 1 | +/** |
| 2 | + * Copyright 2017 IBM Corp. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
| 5 | + * the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
| 10 | + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 11 | + * specific language governing permissions and limitations under the License. |
| 12 | + */ |
| 13 | +package com.ibm.cloud.sdk.core.test.util; |
| 14 | + |
| 15 | +import com.ibm.cloud.sdk.core.util.GsonSingleton; |
| 16 | +import org.junit.Ignore; |
| 17 | + |
| 18 | +import java.io.BufferedReader; |
| 19 | +import java.io.FileInputStream; |
| 20 | +import java.io.FileNotFoundException; |
| 21 | +import java.io.IOException; |
| 22 | +import java.io.InputStream; |
| 23 | +import java.io.InputStreamReader; |
| 24 | + |
| 25 | +/** |
| 26 | + * The Class TestUtils. |
| 27 | + */ |
| 28 | +@Ignore |
| 29 | +public final class TestUtils { |
| 30 | + /** The Constant DELETE. */ |
| 31 | + protected static final String DELETE = "DELETE"; |
| 32 | + |
| 33 | + /** The Constant GET. */ |
| 34 | + protected static final String GET = "GET"; |
| 35 | + |
| 36 | + /** The Constant POST. */ |
| 37 | + protected static final String POST = "POST"; |
| 38 | + |
| 39 | + /** The Constant PUT. */ |
| 40 | + protected static final String PUT = "PUT"; |
| 41 | + |
| 42 | + /** |
| 43 | + * Private constructor. |
| 44 | + */ |
| 45 | + private TestUtils() { } |
| 46 | + |
| 47 | + /** |
| 48 | + * Gets the string from input stream. |
| 49 | + * |
| 50 | + * @param is the input stream |
| 51 | + * @return the string from input stream |
| 52 | + */ |
| 53 | + static String getStringFromInputStream(InputStream is) { |
| 54 | + BufferedReader br = null; |
| 55 | + final StringBuilder sb = new StringBuilder(); |
| 56 | + |
| 57 | + String line; |
| 58 | + try { |
| 59 | + |
| 60 | + br = new BufferedReader(new InputStreamReader(is)); |
| 61 | + while ((line = br.readLine()) != null) { |
| 62 | + sb.append(line); |
| 63 | + } |
| 64 | + |
| 65 | + } catch (final IOException e) { |
| 66 | + e.printStackTrace(); |
| 67 | + } finally { |
| 68 | + if (br != null) { |
| 69 | + try { |
| 70 | + br.close(); |
| 71 | + } catch (final IOException e) { |
| 72 | + e.printStackTrace(); |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + return sb.toString(); |
| 78 | + |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Loads fixture. |
| 83 | + * |
| 84 | + * @param <T> the return type |
| 85 | + * @param filename the file name |
| 86 | + * @param returnType the return type |
| 87 | + * @return the t |
| 88 | + * @throws FileNotFoundException the file not found exception |
| 89 | + */ |
| 90 | + public static <T> T loadFixture(String filename, Class<T> returnType) throws FileNotFoundException { |
| 91 | + String jsonString = getStringFromInputStream(new FileInputStream(filename)); |
| 92 | + return GsonSingleton.getGsonWithoutPrettyPrinting().fromJson(jsonString, returnType); |
| 93 | + } |
| 94 | +} |
0 commit comments