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=========================================================
23 import static org.junit.Assert.assertNotNull;
25 import java.io.IOException;
26 import java.io.InputStream;
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);
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);
67 public static String getTemplatePayload(String fileName, Map<String, String> templateValueMap) throws Exception {
69 InputStream inputStream =
70 PayloadUtil.class.getClassLoader().getResourceAsStream("payloads/templates/" + fileName);
72 String message = String.format("Unable to find the %s in src/test/resources", fileName);
73 assertNotNull(message, inputStream);
77 if (cache.containsKey(fileName)) {
78 resource = cache.get(fileName);
80 resource = IOUtils.toString(inputStream);
81 cache.put(fileName, resource);
84 Matcher matcher = TEMPLATE_PATTERN.matcher(resource);
86 String resourceWithTemplateValues = resource;
88 while (matcher.find()) {
89 int start = matcher.start() + 2;
90 int end = matcher.end() - 1;
91 String key = resource.substring(start, end);
92 if (templateValueMap.containsKey(key)) {
93 resourceWithTemplateValues =
94 resourceWithTemplateValues.replaceAll("\\$\\{" + key + "\\}", templateValueMap.get(key));
96 throw new RuntimeException(
97 "Unable to find the key value pair in map for the template processing for key " + key);
102 return resourceWithTemplateValues;