Update Janusgraph to 0.5.0 in traversal
[aai/traversal.git] / aai-traversal / src / test / java / org / onap / aai / PayloadUtil.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.aai;
21
22 import static org.junit.Assert.assertNotNull;
23
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.nio.charset.Charset;
27 import java.util.HashMap;
28 import java.util.Map;
29 import java.util.regex.Matcher;
30 import java.util.regex.Pattern;
31
32 import org.apache.commons.io.IOUtils;
33
34 public class PayloadUtil {
35
36     private static final Map<String, String> cache = new HashMap<>();
37     private static final Pattern TEMPLATE_PATTERN = Pattern.compile("\\$\\{[^}]+\\}");
38
39     public static String getExpectedPayload(String fileName) throws IOException {
40
41         InputStream inputStream =
42             PayloadUtil.class.getClassLoader().getResourceAsStream("payloads/expected/" + fileName);
43
44         String message = String.format("Unable to find the %s in src/test/resources", fileName);
45         assertNotNull(message, inputStream);
46
47         String resource = IOUtils.toString(inputStream, Charset.defaultCharset());
48
49         inputStream.close();
50         return resource;
51     }
52
53     public static String getResourcePayload(String fileName) throws IOException {
54
55         InputStream inputStream =
56             PayloadUtil.class.getClassLoader().getResourceAsStream("payloads/resource/" + fileName);
57
58         String message = String.format("Unable to find the %s in src/test/resources", fileName);
59         assertNotNull(message, inputStream);
60
61         String resource = IOUtils.toString(inputStream, Charset.defaultCharset());
62
63         inputStream.close();
64         return resource;
65     }
66
67     public static String getTemplatePayload(String fileName, Map<String, String> templateValueMap)
68         throws Exception {
69
70         InputStream inputStream = PayloadUtil.class.getClassLoader()
71             .getResourceAsStream("payloads/templates/" + fileName);
72
73         String message = String.format("Unable to find the %s in src/test/resources", fileName);
74         assertNotNull(message, inputStream);
75
76         String resource;
77
78         if (cache.containsKey(fileName)) {
79             resource = cache.get(fileName);
80         } else {
81             resource = IOUtils.toString(inputStream, Charset.defaultCharset());
82             cache.put(fileName, resource);
83         }
84
85         Matcher matcher = TEMPLATE_PATTERN.matcher(resource);
86
87         String resourceWithTemplateValues = resource;
88
89         while (matcher.find()) {
90             int start = matcher.start() + 2;
91             int end = matcher.end() - 1;
92             String key = resource.substring(start, end);
93             if (templateValueMap.containsKey(key)) {
94                 resourceWithTemplateValues = resourceWithTemplateValues
95                     .replaceAll("\\$\\{" + key + "\\}", templateValueMap.get(key));
96             } else {
97                 throw new RuntimeException(
98                     "Unable to find the key value pair in map for the template processing for key "
99                         + key);
100             }
101         }
102
103         inputStream.close();
104         return resourceWithTemplateValues;
105     }
106
107     public static String getNamedQueryPayload(String fileName) throws IOException {
108
109         InputStream inputStream = PayloadUtil.class.getClassLoader()
110             .getResourceAsStream("payloads/named-queries/" + fileName);
111
112         String message = String
113             .format("Unable to find the %s in src/test/resources/payloads/named-queries", fileName);
114         assertNotNull(message, inputStream);
115
116         String resource = IOUtils.toString(inputStream, Charset.defaultCharset());
117
118         inputStream.close();
119         return resource;
120     }
121 }