Initial OpenECOMP policy/engine commit
[policy/engine.git] / ECOMP-TEST / src / test / java / org / openecomp / policy / pdp / test / std / dom / DOMResponseConformanceTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ECOMP-TEST
4  * ================================================================================
5  * Copyright (C) 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
21 package org.openecomp.policy.pdp.test.std.dom;
22
23 import static org.junit.Assert.fail;
24
25 import java.io.BufferedReader;
26 import java.io.File;
27 import java.io.FileReader;
28 import java.util.ArrayList;
29 import java.util.List;
30
31 import org.junit.Test;
32
33 import com.att.research.xacml.api.Response;
34 import com.att.research.xacml.std.dom.DOMResponse;
35
36 /**
37  * Tests for handling the XML version of the XACML Response object.
38  * 
39  * TO RUN - use jUnit
40  * In Eclipse select this file or the enclosing directory, right-click and select Run As/JUnit Test
41  * 
42  * Normally the Response is generated by the PDP and returned through the RESTful interface as JSON.
43  * Testing of the XML interface is minimal and not complete.
44  * 
45  * 
46  * 
47  *
48  */
49 public class DOMResponseConformanceTest {
50         
51         // where to find the conformance test XML files
52         private final String CONFORMANCE_DIRECTORY_PATH = "testsets/conformance/xacml3.0-ct-v.0.4";
53         
54         // The request object output from each test conversion from JSON string
55         Response response;
56
57         
58         
59         // Load the Conformance test responses into Response objects, generate the output XML for that Response and compare with the original files.
60         @Test
61         public void testDOMResponse() {
62                 List<File> filesInDirectory = null;
63                 
64                 File conformanceDirectory = null;
65                 
66                 File currentFile = null;
67                 
68                 try {
69                         conformanceDirectory = new File(CONFORMANCE_DIRECTORY_PATH);
70                         filesInDirectory = getRequestsInDirectory(conformanceDirectory);
71                 } catch (Exception e) {
72                         fail("Unable to set up Conformance tests for dir '" + conformanceDirectory.getAbsolutePath()+"' e="+ e);
73                 }
74                 
75                 // run through each XML file
76                 //      - load the file from XML into an internal Response object
77                 //      - generate the XML representation from that Response object
78                 //      - reload the file into a String
79                 //      - compare the 2 XML strings
80                 Response xmlResponse = null;
81                 try {
82                         for (File f : filesInDirectory) {
83                                 currentFile = f;
84
85 //// This is a simple way to select just one file for debugging - comment out when not being used
86 //if ( ! f.getName().equals("IID302Response.xml")) {   continue;  }
87
88 // during debugging it is helpful to know what file it is starting to work on
89 //                              System.out.println("starting file="+currentFile.getName());
90                                 
91                                 
92                                 BufferedReader br = new BufferedReader(new FileReader(f));
93                                 StringBuffer sb = new StringBuffer();
94                                 String line;
95                                 while ((line = br.readLine()) != null) {
96                                         sb.append(line + "\n");
97                                 }
98                                 br.close();
99                                 
100                                 String xmlFromFile = sb.toString();
101                                 
102                                 try {
103                                         // load XML into a Response object
104                                         xmlResponse = DOMResponse.load(xmlFromFile);
105                                 } catch (Exception e) {
106                                         // if XML does not load, just note it and continue with next file
107                                         System.out.println("XML file did not load: '" + f.getName() + "  e=" + e);
108                                         continue;
109                                 }
110 //System.out.println(xmlFromFile);                              
111                                 
112                                 // create String version from the Response object
113                                 String xmlResponseString = DOMResponse.toString(xmlResponse, false);
114                                 
115                                 // Comparing the string directly to the String from the file is difficult.
116                                 // We can minimize the problems with newlines and whitespace, but we have other issues with how various object values are represented.
117                                 // For instance, and input double of "23.50" is output as "23.5" which is the same value but not identical strings.
118                                 // Therefore we take the XML output and use it to create a new Response object, then compare the two objects.
119
120 //System.out.println(xmlResponseString);                        
121                                 Response reGeneratedResponse = DOMResponse.load(xmlResponseString);
122                                 
123                                 if ( ! xmlResponse.equals(reGeneratedResponse)) {
124                                         String normalizedFromFile = xmlFromFile.replaceAll("\\r|\\n", "");
125                                         normalizedFromFile = normalizedFromFile.replaceAll("\\s+", " ");
126                                         normalizedFromFile = normalizedFromFile.replaceAll(">\\s*<", "><");
127                                         System.out.println("File="+normalizedFromFile);
128                                         System.out.println("Gend="+ xmlResponseString);
129                                         
130                                         System.out.println(DOMResponse.toString(xmlResponse, true));
131                                 
132                                         fail("Output string did not re-generate eqivilent object.");
133                                 }
134
135 //                              // Normally whitespace is significant in XML.
136 //                              // However in this case we are generating an XML string for output and comparing it to a hand-made file.
137 //                              // The file may contain extra newlines or fewer spaces then our prettyPrinted output version.
138 //                              // Therefore we do the comparison on the un-prettyPrinted generated string.
139 //                              // To do this we have to remove the extra whitespace from the version read from the file.
140 //                              String normalizedFromFile = xmlFromFile.replaceAll("\\r|\\n", "");
141 //                              normalizedFromFile = normalizedFromFile.replaceAll("\\s+", " ");
142 //                              normalizedFromFile = normalizedFromFile.replaceAll(">\\s*<", "><");
143 //                      
144 //                              if ( ! xmlResponseString.equals(normalizedFromFile)) {
145 //                                      System.out.println("file="+normalizedFromFile+"\ngend="+xmlResponseString);
146 //                                      fail("file not same as generated string: " + f.getName()+ "\nFile="+xmlFromFile + "\nString="+xmlResponseString);
147 //                              }
148
149
150                         }                       
151
152                 } catch (Exception e) {
153                         fail ("Failed test with '" + currentFile.getName() + "', e=" + e);
154                 }
155
156                 
157         }
158         
159         
160         
161         //
162         // HELPER to get list of all Request files in the given directory
163         //
164         
165         private List<File> getRequestsInDirectory(File directory) {
166                 List<File> fileList = new ArrayList<File>();
167                 
168                 File[] fileArray = directory.listFiles();
169                 for (File f : fileArray) {
170                         if (f.isDirectory()) {
171                                 List<File> subDirList = getRequestsInDirectory(f);
172                                 fileList.addAll(subDirList);
173                         }
174                         if (f.getName().endsWith("Response.xml")) {
175                                 fileList.add(f);
176                         }
177                 }
178                 return fileList;
179                 
180         }
181         
182         
183 }
184
185
186 /*
187 Place to edit long strings output during tests
188
189
190
191
192
193
194
195
196 */