43fd2903c62b71467704e8953c085f1529fce1f4
[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 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22 package org.onap.aai;
23
24 import org.apache.commons.io.IOUtils;
25
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.util.HashMap;
29 import java.util.Map;
30 import java.util.regex.Matcher;
31 import java.util.regex.Pattern;
32
33 import static org.junit.Assert.assertNotNull;
34
35 public class PayloadUtil {
36
37     private static final Map<String, String> cache = new HashMap<>();
38     private static final Pattern TEMPLATE_PATTERN = Pattern.compile("\\$\\{[^}]+\\}");
39
40     public static String getExpectedPayload(String fileName) throws IOException {
41
42         InputStream inputStream = 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 = PayloadUtil.class.getClassLoader().getResourceAsStream("payloads/resource/" + fileName);
56
57         String message = String.format("Unable to find the %s in src/test/resources", fileName);
58         assertNotNull(message, inputStream);
59
60         String resource = IOUtils.toString(inputStream);
61
62         inputStream.close();
63         return resource;
64     }
65
66     public static String getTemplatePayload(String fileName, Map<String, String> templateValueMap) throws Exception {
67
68         InputStream inputStream = PayloadUtil.class.getClassLoader().getResourceAsStream("payloads/templates/" + fileName);
69
70         String message = String.format("Unable to find the %s in src/test/resources", fileName);
71         assertNotNull(message, inputStream);
72
73         String resource;
74
75         if(cache.containsKey(fileName)){
76             resource = cache.get(fileName);
77         } else {
78             resource = IOUtils.toString(inputStream);
79             cache.put(fileName, resource);
80         }
81
82         Matcher matcher = TEMPLATE_PATTERN.matcher(resource);
83
84         String resourceWithTemplateValues = resource;
85
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));
92             } else {
93                 throw new RuntimeException("Unable to find the key value pair in map for the template processing for key " + key);
94             }
95         }
96
97         inputStream.close();
98         return resourceWithTemplateValues;
99     }
100 }