Commit includes ControlLoopPolicy API and bugfixes
[policy/engine.git] / ECOMP-PDP / src / test / java / org / openecomp / policy / pdp / test / conformance / ResponseMatchResult.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ECOMP-PDP
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.conformance;
22
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.Iterator;
26 import java.util.List;
27
28 import com.att.research.xacml.api.Response;
29 import com.att.research.xacml.api.Result;
30
31 /**
32  * ResponseMatchResult provides information about how a {@link com.att.research.xacml.api.Response} object matches
33  * another <code>Response</code> object.
34  * 
35  * @version $Revision: 1.1 $
36  */
37 public class ResponseMatchResult {
38         private List<ResultMatchResult> resultMatchResults      = new ArrayList<>();
39         
40         private boolean bAssociatedAdviceMatches                        = true;
41         private boolean bAttributesMatch                                        = true;
42         private boolean bDecisionsMatch                                         = true;
43         private boolean bStatusCodesMatch                                       = true;
44         private boolean bObligationsMatch                                       = true;
45         private boolean bPolicyIdentifiersMatch                         = true;
46         private boolean bPolicySetIdentifiersMatch                      = true;
47         private boolean bNumResultsMatch                                        = true;
48         private boolean bUnknownFunction;
49         
50         protected void addResultMatchResult(ResultMatchResult resultMatchResult) {
51                 this.resultMatchResults.add(resultMatchResult);
52                 this.bAssociatedAdviceMatches   = resultMatchResult.associatedAdviceMatches() && this.bAssociatedAdviceMatches;
53                 this.bAttributesMatch                   = resultMatchResult.attributesMatch() && this.bAttributesMatch;
54                 this.bDecisionsMatch                    = resultMatchResult.decisionsMatch() && this.bDecisionsMatch;
55                 this.bStatusCodesMatch                  = resultMatchResult.statusCodesMatch() && this.bStatusCodesMatch;
56                 this.bObligationsMatch                  = resultMatchResult.obligationsMatch() && this.bObligationsMatch;
57                 this.bPolicyIdentifiersMatch    = resultMatchResult.policyIdentifiersMatch() && this.bPolicyIdentifiersMatch;
58                 this.bPolicySetIdentifiersMatch = resultMatchResult.policySetIdentifiersMatch() && this.bPolicySetIdentifiersMatch;
59                 this.bUnknownFunction                   = resultMatchResult.unknownFunction() || this.bUnknownFunction;
60         }
61         
62         protected void setNumResultsMatch(boolean b) {
63                 this.bNumResultsMatch   = b;
64         }
65         
66         public ResponseMatchResult() {
67         }
68         
69         public static ResponseMatchResult newInstance(Response response1, Response response2) {
70                 ResponseMatchResult responseMatchResult = new ResponseMatchResult();
71
72                 Collection<Result> listResultsResponse1 = response1.getResults();
73                 Collection<Result> listResultsResponse2 = response2.getResults();
74                 if (listResultsResponse1.size() == 1 && listResultsResponse2.size() == 1) {
75                         /*
76                          * Just add a single ResultMatchResult comparing the results in the two responses
77                          */
78                         responseMatchResult.addResultMatchResult(ResultMatchResult.newInstance(listResultsResponse1.iterator().next(), listResultsResponse2.iterator().next()));
79                 } else {
80                         /*
81                          * Iterate over all of the results in the two responses and match them
82                          */
83                         Iterator<Result> iterResponse1Results   = listResultsResponse1.iterator();
84                         Iterator<Result> iterResponse2Results   = listResultsResponse2.iterator();
85                         while ((iterResponse1Results != null && iterResponse1Results.hasNext()) || (iterResponse2Results != null && iterResponse2Results.hasNext())) {
86                                 Result result1  = (iterResponse1Results != null && iterResponse1Results.hasNext() ? iterResponse1Results.next() : null);
87                                 Result result2  = (iterResponse2Results != null && iterResponse2Results.hasNext() ? iterResponse2Results.next() : null);
88                                 if ((result1 == null || result2 == null) && responseMatchResult.numResultsMatch()) {
89                                         responseMatchResult.setNumResultsMatch(false);
90                                 }
91                                 responseMatchResult.addResultMatchResult(ResultMatchResult.newInstance(result1, result2));
92                         }
93                 }
94                 return responseMatchResult;
95         }
96
97         public Iterator<ResultMatchResult> getResultMatchResults() {
98                 return this.resultMatchResults.iterator();
99         }
100         
101         public boolean numResultsMatch() {
102                 return this.bNumResultsMatch;
103         }
104         
105         public boolean associatedAdviceMatches() {
106                 return this.bAssociatedAdviceMatches;
107         }
108         
109         public boolean attributesMatch() {
110                 return this.bAttributesMatch;
111         }
112         
113         public boolean decisionsMatch() {
114                 return this.bDecisionsMatch;
115         }
116         
117         public boolean obligationsMatch() {
118                 return this.bObligationsMatch;
119         }
120         
121         public boolean policyIdentifiersMatch() {
122                 return this.bPolicyIdentifiersMatch;
123         }
124         
125         public boolean policySetIdentifiersMatch() {
126                 return this.bPolicySetIdentifiersMatch;
127         }
128         
129         public boolean statusCodesMatch() {
130                 return this.bStatusCodesMatch;
131         }
132         
133         public boolean unknownFunction() {
134                 return this.bUnknownFunction;
135         }
136
137 }