2 * ============LICENSE_START=======================================================
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
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=========================================================
21 package org.openecomp.policy.xacml.test;
23 import static org.junit.Assert.fail;
25 import java.io.BufferedReader;
27 import java.io.FileReader;
28 import java.util.ArrayList;
29 import java.util.List;
31 import org.junit.Test;
33 import com.att.research.xacml.api.Response;
34 import com.att.research.xacml.std.dom.DOMResponse;
37 * Tests for handling the XML version of the XACML Response object.
40 * In Eclipse select this file or the enclosing directory, right-click and select Run As/JUnit Test
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.
49 public class DOMResponseConformanceTest {
51 // where to find the conformance test XML files
52 private final String CONFORMANCE_DIRECTORY_PATH = "testsets/conformance/xacml3.0-ct-v.0.4";
54 // The request object output from each test conversion from JSON string
59 // Load the Conformance test responses into Response objects, generate the output XML for that Response and compare with the original files.
61 public void testDOMResponse() {
62 List<File> filesInDirectory = null;
64 File conformanceDirectory = null;
66 File currentFile = null;
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);
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;
82 for (File f : filesInDirectory) {
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; }
88 // during debugging it is helpful to know what file it is starting to work on
89 // System.out.println("starting file="+currentFile.getName());
92 BufferedReader br = new BufferedReader(new FileReader(f));
93 StringBuffer sb = new StringBuffer();
95 while ((line = br.readLine()) != null) {
96 sb.append(line + "\n");
100 String xmlFromFile = sb.toString();
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);
110 //System.out.println(xmlFromFile);
112 // create String version from the Response object
113 String xmlResponseString = DOMResponse.toString(xmlResponse, false);
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.
120 //System.out.println(xmlResponseString);
121 Response reGeneratedResponse = DOMResponse.load(xmlResponseString);
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);
130 System.out.println(DOMResponse.toString(xmlResponse, true));
132 fail("Output string did not re-generate eqivilent object.");
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*<", "><");
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);
152 } catch (Exception e) {
153 fail ("Failed test with '" + currentFile.getName() + "', e=" + e);
162 // HELPER to get list of all Request files in the given directory
165 private List<File> getRequestsInDirectory(File directory) {
166 List<File> fileList = new ArrayList<File>();
168 File[] fileArray = directory.listFiles();
169 for (File f : fileArray) {
170 if (f.isDirectory()) {
171 List<File> subDirList = getRequestsInDirectory(f);
172 fileList.addAll(subDirList);
174 if (f.getName().endsWith("Response.xml")) {
187 Place to edit long strings output during tests