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.nio.charset.Charset;
28 import java.util.HashMap;
30 import java.util.regex.Matcher;
31 import java.util.regex.Pattern;
33 import org.apache.commons.io.IOUtils;
35 public class PayloadUtil {
37 private static final Map<String, String> cache = new HashMap<>();
38 private static final Pattern TEMPLATE_PATTERN = Pattern.compile("\\$\\{[^}]+\\}");
40 public static String getExpectedPayload(String fileName) throws IOException {
42 InputStream inputStream =
43 PayloadUtil.class.getClassLoader().getResourceAsStream("payloads/expected/" + fileName);
45 String message = String.format("Unable to find the %s in src/test/resources", fileName);
46 assertNotNull(message, inputStream);
48 String resource = IOUtils.toString(inputStream, Charset.defaultCharset());
54 public static String getResourcePayload(String fileName) throws IOException {
56 InputStream inputStream =
57 PayloadUtil.class.getClassLoader().getResourceAsStream("payloads/resource/" + fileName);
59 String message = String.format("Unable to find the %s in src/test/resources", fileName);
60 assertNotNull(message, inputStream);
62 String resource = IOUtils.toString(inputStream, Charset.defaultCharset());
68 public static String getTemplatePayload(String fileName, Map<String, String> templateValueMap) throws Exception {
70 InputStream inputStream =
71 PayloadUtil.class.getClassLoader().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 =
95 resourceWithTemplateValues.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 " + key);
103 return resourceWithTemplateValues;