Update the license for 2017-2018 license
[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 package org.onap.aai;
21
22 import org.apache.commons.io.IOUtils;
23
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.util.HashMap;
27 import java.util.Map;
28 import java.util.regex.Matcher;
29 import java.util.regex.Pattern;
30
31 import static org.junit.Assert.assertNotNull;
32
33 public class PayloadUtil {
34
35     private static final Map<String, String> cache = new HashMap<>();
36     private static final Pattern TEMPLATE_PATTERN = Pattern.compile("\\$\\{[^}]+\\}");
37
38     public static String getExpectedPayload(String fileName) throws IOException {
39
40         InputStream inputStream = PayloadUtil.class.getClassLoader().getResourceAsStream("payloads/expected/" + fileName);
41
42         String message = String.format("Unable to find the %s in src/test/resources", fileName);
43         assertNotNull(message, inputStream);
44
45         String resource = IOUtils.toString(inputStream);
46
47         inputStream.close();
48         return resource;
49     }
50
51     public static String getResourcePayload(String fileName) throws IOException {
52
53         InputStream inputStream = PayloadUtil.class.getClassLoader().getResourceAsStream("payloads/resource/" + fileName);
54
55         String message = String.format("Unable to find the %s in src/test/resources", fileName);
56         assertNotNull(message, inputStream);
57
58         String resource = IOUtils.toString(inputStream);
59
60         inputStream.close();
61         return resource;
62     }
63
64     public static String getTemplatePayload(String fileName, Map<String, String> templateValueMap) throws Exception {
65
66         InputStream inputStream = PayloadUtil.class.getClassLoader().getResourceAsStream("payloads/templates/" + fileName);
67
68         String message = String.format("Unable to find the %s in src/test/resources", fileName);
69         assertNotNull(message, inputStream);
70
71         String resource;
72
73         if(cache.containsKey(fileName)){
74             resource = cache.get(fileName);
75         } else {
76             resource = IOUtils.toString(inputStream);
77             cache.put(fileName, resource);
78         }
79
80         Matcher matcher = TEMPLATE_PATTERN.matcher(resource);
81
82         String resourceWithTemplateValues = resource;
83
84         while(matcher.find()){
85             int start = matcher.start() + 2;
86             int end = matcher.end() - 1;
87             String key = resource.substring(start, end);
88             if(templateValueMap.containsKey(key)){
89                 resourceWithTemplateValues = resourceWithTemplateValues.replaceAll("\\$\\{" + key +"\\}", templateValueMap.get(key));
90             } else {
91                 throw new RuntimeException("Unable to find the key value pair in map for the template processing for key " + key);
92             }
93         }
94
95         inputStream.close();
96         return resourceWithTemplateValues;
97     }
98 }