Update Janusgraph to 0.4.0 in aai-common
[aai/aai-common.git] / aai-core / 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
21 package org.onap.aai;
22
23 import static org.junit.Assert.assertNotNull;
24
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.nio.charset.Charset;
28 import java.util.HashMap;
29 import java.util.Map;
30 import java.util.regex.Matcher;
31 import java.util.regex.Pattern;
32
33 import org.apache.commons.io.IOUtils;
34
35 public class PayloadUtil {
36
37     private static final Map<String, String> cache = new HashMap<>();
38     private static final Pattern TEMPLATE_PATTERN = Pattern.compile("\\$\\{[^}]+\\}");
39
40     public static String getExpectedPayload(String fileName) throws IOException {
41
42         InputStream inputStream =
43                 PayloadUtil.class.getClassLoader().getResourceAsStream("payloads/expected/" + fileName);
44
45         String message = String.format("Unable to find the %s in src/test/resources", fileName);
46         assertNotNull(message, inputStream);
47
48         String resource = IOUtils.toString(inputStream, Charset.defaultCharset());
49
50         inputStream.close();
51         return resource;
52     }
53
54     public static String getResourcePayload(String fileName) throws IOException {
55
56         InputStream inputStream =
57                 PayloadUtil.class.getClassLoader().getResourceAsStream("payloads/resource/" + fileName);
58
59         String message = String.format("Unable to find the %s in src/test/resources", fileName);
60         assertNotNull(message, inputStream);
61
62         String resource = IOUtils.toString(inputStream, Charset.defaultCharset());
63
64         inputStream.close();
65         return resource;
66     }
67
68     public static String getTemplatePayload(String fileName, Map<String, String> templateValueMap) throws Exception {
69
70         InputStream inputStream =
71                 PayloadUtil.class.getClassLoader().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 =
95                         resourceWithTemplateValues.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 " + key);
99             }
100         }
101
102         inputStream.close();
103         return resourceWithTemplateValues;
104     }
105 }