AAI-1523 Batch reformat aai-core
[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.util.HashMap;
28 import java.util.Map;
29 import java.util.regex.Matcher;
30 import java.util.regex.Pattern;
31
32 import org.apache.commons.io.IOUtils;
33
34 public class PayloadUtil {
35
36     private static final Map<String, String> cache = new HashMap<>();
37     private static final Pattern TEMPLATE_PATTERN = Pattern.compile("\\$\\{[^}]+\\}");
38
39     public static String getExpectedPayload(String fileName) throws IOException {
40
41         InputStream inputStream =
42                 PayloadUtil.class.getClassLoader().getResourceAsStream("payloads/expected/" + fileName);
43
44         String message = String.format("Unable to find the %s in src/test/resources", fileName);
45         assertNotNull(message, inputStream);
46
47         String resource = IOUtils.toString(inputStream);
48
49         inputStream.close();
50         return resource;
51     }
52
53     public static String getResourcePayload(String fileName) throws IOException {
54
55         InputStream inputStream =
56                 PayloadUtil.class.getClassLoader().getResourceAsStream("payloads/resource/" + fileName);
57
58         String message = String.format("Unable to find the %s in src/test/resources", fileName);
59         assertNotNull(message, inputStream);
60
61         String resource = IOUtils.toString(inputStream);
62
63         inputStream.close();
64         return resource;
65     }
66
67     public static String getTemplatePayload(String fileName, Map<String, String> templateValueMap) throws Exception {
68
69         InputStream inputStream =
70                 PayloadUtil.class.getClassLoader().getResourceAsStream("payloads/templates/" + fileName);
71
72         String message = String.format("Unable to find the %s in src/test/resources", fileName);
73         assertNotNull(message, inputStream);
74
75         String resource;
76
77         if (cache.containsKey(fileName)) {
78             resource = cache.get(fileName);
79         } else {
80             resource = IOUtils.toString(inputStream);
81             cache.put(fileName, resource);
82         }
83
84         Matcher matcher = TEMPLATE_PATTERN.matcher(resource);
85
86         String resourceWithTemplateValues = resource;
87
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));
95             } else {
96                 throw new RuntimeException(
97                         "Unable to find the key value pair in map for the template processing for key " + key);
98             }
99         }
100
101         inputStream.close();
102         return resourceWithTemplateValues;
103     }
104 }