2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 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=========================================================
20 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
24 import org.apache.commons.io.IOUtils;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.util.HashMap;
30 import java.util.regex.Matcher;
31 import java.util.regex.Pattern;
33 import static org.junit.Assert.assertNotNull;
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 = 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 = PayloadUtil.class.getClassLoader().getResourceAsStream("payloads/resource/" + fileName);
57 String message = String.format("Unable to find the %s in src/test/resources", fileName);
58 assertNotNull(message, inputStream);
60 String resource = IOUtils.toString(inputStream);
66 public static String getTemplatePayload(String fileName, Map<String, String> templateValueMap) throws Exception {
68 InputStream inputStream = PayloadUtil.class.getClassLoader().getResourceAsStream("payloads/templates/" + fileName);
70 String message = String.format("Unable to find the %s in src/test/resources", fileName);
71 assertNotNull(message, inputStream);
75 if(cache.containsKey(fileName)){
76 resource = cache.get(fileName);
78 resource = IOUtils.toString(inputStream);
79 cache.put(fileName, resource);
82 Matcher matcher = TEMPLATE_PATTERN.matcher(resource);
84 String resourceWithTemplateValues = resource;
86 while(matcher.find()){
87 int start = matcher.start() + 2;
88 int end = matcher.end() - 1;
89 String key = resource.substring(start, end);
90 if(templateValueMap.containsKey(key)){
91 resourceWithTemplateValues = resourceWithTemplateValues.replaceAll("\\$\\{" + key +"\\}", templateValueMap.get(key));
93 throw new RuntimeException("Unable to find the key value pair in map for the template processing for key " + key);
98 return resourceWithTemplateValues;