2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 import static org.junit.Assert.assertNotNull;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.nio.charset.Charset;
27 import java.util.HashMap;
29 import java.util.regex.Matcher;
30 import java.util.regex.Pattern;
32 import org.apache.commons.io.IOUtils;
34 public class PayloadUtil {
36 private static final Map<String, String> cache = new HashMap<>();
37 private static final Pattern TEMPLATE_PATTERN = Pattern.compile("\\$\\{[^}]+\\}");
39 public static String getExpectedPayload(String fileName) throws IOException {
41 InputStream inputStream =
42 PayloadUtil.class.getClassLoader().getResourceAsStream("payloads/expected/" + fileName);
44 String message = String.format("Unable to find the %s in src/test/resources", fileName);
45 assertNotNull(message, inputStream);
47 String resource = IOUtils.toString(inputStream, Charset.defaultCharset());
53 public static String getResourcePayload(String fileName) throws IOException {
55 InputStream inputStream =
56 PayloadUtil.class.getClassLoader().getResourceAsStream("payloads/resource/" + fileName);
58 String message = String.format("Unable to find the %s in src/test/resources", fileName);
59 assertNotNull(message, inputStream);
61 String resource = IOUtils.toString(inputStream, Charset.defaultCharset());
67 public static String getTemplatePayload(String fileName, Map<String, String> templateValueMap)
70 InputStream inputStream = PayloadUtil.class.getClassLoader()
71 .getResourceAsStream("payloads/templates/" + fileName);
73 String message = String.format("Unable to find the %s in src/test/resources", fileName);
74 assertNotNull(message, inputStream);
78 if (cache.containsKey(fileName)) {
79 resource = cache.get(fileName);
81 resource = IOUtils.toString(inputStream, Charset.defaultCharset());
82 cache.put(fileName, resource);
85 Matcher matcher = TEMPLATE_PATTERN.matcher(resource);
87 String resourceWithTemplateValues = resource;
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));
97 throw new RuntimeException(
98 "Unable to find the key value pair in map for the template processing for key "
104 return resourceWithTemplateValues;
107 public static String getNamedQueryPayload(String fileName) throws IOException {
109 InputStream inputStream = PayloadUtil.class.getClassLoader()
110 .getResourceAsStream("payloads/named-queries/" + fileName);
112 String message = String
113 .format("Unable to find the %s in src/test/resources/payloads/named-queries", fileName);
114 assertNotNull(message, inputStream);
116 String resource = IOUtils.toString(inputStream, Charset.defaultCharset());