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.pdp.test.conformance;
24 import java.io.FileInputStream;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.util.ArrayList;
28 import java.util.Iterator;
29 import java.util.List;
30 import java.util.Properties;
32 import com.att.research.xacml.util.StringUtils;
33 import com.att.research.xacml.util.XACMLProperties;
34 import com.att.research.xacmlatt.pdp.std.StdPolicyFinderFactory;
37 * ConformanceRepository represents one or more policies for a single policy test, which will include one or more root policies, and
38 * zero or more referenced policies.
42 public class ConformanceRepository {
43 private List<File> rootPolicies = new ArrayList<File>();
44 private List<File> referencedPolicies = new ArrayList<File>();
46 private void setXACMLProperty(String propertyName, List<File> listFiles) {
47 Iterator<File> iterFiles = listFiles.iterator();
48 StringBuilder stringBuilderIdList = new StringBuilder();
49 while (iterFiles.hasNext()) {
50 File file = iterFiles.next();
51 if (stringBuilderIdList.length() > 0) {
52 stringBuilderIdList.append(',');
54 stringBuilderIdList.append(file.getName());
56 XACMLProperties.setProperty(file.getName() + StdPolicyFinderFactory.PROP_FILE, file.getAbsolutePath());
58 XACMLProperties.setProperty(propertyName, stringBuilderIdList.toString());
61 public ConformanceRepository() {
64 public void setXACMLProperties() {
65 if (this.rootPolicies.size() > 0) {
66 this.setXACMLProperty(XACMLProperties.PROP_ROOTPOLICIES, this.rootPolicies);
68 if (this.referencedPolicies.size() > 0) {
69 this.setXACMLProperty(XACMLProperties.PROP_REFERENCEDPOLICIES, this.referencedPolicies);
73 private void loadProperty(File fileDir, Properties properties, String propertyName, List<File> listFiles) {
74 String fileNameList = properties.getProperty(propertyName);
75 if (fileNameList != null) {
76 String[] fileNameArray = fileNameList.split("[,]",0);
77 if (fileNameArray != null && fileNameArray.length > 0) {
78 for (String fileName : fileNameArray) {
79 File file = new File(fileDir, fileName);
80 if (file.exists() && file.canRead()) {
88 public void load(File fileRepository) throws IOException {
89 Properties propertiesRepository = new Properties();
90 try (InputStream is = new FileInputStream(fileRepository)) {
91 propertiesRepository.load(is);
93 this.loadProperty(fileRepository.getParentFile(), propertiesRepository, XACMLProperties.PROP_ROOTPOLICIES, this.rootPolicies);
94 this.loadProperty(fileRepository.getParentFile(), propertiesRepository, XACMLProperties.PROP_REFERENCEDPOLICIES, this.referencedPolicies);
97 public void addRootPolicy(File filePolicy) {
98 this.rootPolicies.add(filePolicy);
101 public boolean hasRootPolicy() {
102 return (this.rootPolicies.size() > 0);
106 public String toString() {
107 StringBuilder stringBuilder = new StringBuilder("{");
108 boolean needComma = false;
110 if (this.rootPolicies != null && this.rootPolicies.size() > 0) {
111 stringBuilder.append("rootPolicies=");
112 stringBuilder.append(StringUtils.toString(this.rootPolicies.iterator()));
115 if (this.referencedPolicies != null && this.referencedPolicies.size() > 0) {
117 stringBuilder.append(',');
119 stringBuilder.append("referencedPolicies=");
120 stringBuilder.append(StringUtils.toString(this.referencedPolicies.iterator()));
123 stringBuilder.append('}');
124 return stringBuilder.toString();