Bug 4724 added containers to tenant
[groupbasedpolicy.git] / groupbasedpolicy / src / test / java / org / opendaylight / groupbasedpolicy / util / PolicyResolutionTest.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.groupbasedpolicy.util;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14
15 import java.util.HashMap;
16 import java.util.HashSet;
17 import java.util.List;
18 import java.util.Map;
19 import java.util.Set;
20
21 import org.junit.Test;
22 import org.opendaylight.groupbasedpolicy.dto.ConditionSet;
23 import org.opendaylight.groupbasedpolicy.dto.EgKey;
24 import org.opendaylight.groupbasedpolicy.dto.EndpointConstraint;
25 import org.opendaylight.groupbasedpolicy.dto.IndexedTenant;
26 import org.opendaylight.groupbasedpolicy.dto.Policy;
27 import org.opendaylight.groupbasedpolicy.dto.RuleGroup;
28 import org.opendaylight.groupbasedpolicy.util.ContractResolverUtils.ContractMatch;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.CapabilityMatcherName;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.CapabilityName;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ClauseName;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ConditionMatcherName;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ConditionName;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ContractId;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.EndpointGroupId;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.QualityMatcherName;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.QualityName;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.RequirementMatcherName;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.RequirementName;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.RuleName;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.SelectorName;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.SubjectName;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TargetName;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.Matcher.MatchType;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.capabilities.Capability;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.capabilities.CapabilityBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.condition.matchers.ConditionMatcher;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.condition.matchers.ConditionMatcherBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.conditions.Condition;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.conditions.ConditionBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.qualities.Quality;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.qualities.QualityBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.requirements.Requirement;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.requirements.RequirementBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.target.selector.QualityMatcher;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.target.selector.QualityMatcherBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.target.selector.quality.matcher.MatcherQualityBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.Tenant;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.TenantBuilder;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.PolicyBuilder;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.Contract;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.ContractBuilder;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.EndpointGroup;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.EndpointGroupBuilder;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.Clause;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.ClauseBuilder;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.Subject;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.SubjectBuilder;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.Target;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.TargetBuilder;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.clause.ConsumerMatchersBuilder;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.clause.ProviderMatchersBuilder;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.clause.consumer.matchers.group.identification.constraints.GroupRequirementConstraintCaseBuilder;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.clause.consumer.matchers.group.identification.constraints.group.requirement.constraint._case.RequirementMatcher;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.clause.consumer.matchers.group.identification.constraints.group.requirement.constraint._case.RequirementMatcherBuilder;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.clause.consumer.matchers.group.identification.constraints.group.requirement.constraint._case.requirement.matcher.MatcherRequirementBuilder;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.clause.provider.matchers.group.identification.constraints.GroupCapabilityConstraintCaseBuilder;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.clause.provider.matchers.group.identification.constraints.group.capability.constraint._case.CapabilityMatcher;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.clause.provider.matchers.group.identification.constraints.group.capability.constraint._case.CapabilityMatcherBuilder;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.clause.provider.matchers.group.identification.constraints.group.capability.constraint._case.capability.matcher.MatcherCapabilityBuilder;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.subject.Rule;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.subject.RuleBuilder;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.endpoint.group.ConsumerNamedSelector;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.endpoint.group.ConsumerNamedSelectorBuilder;
86 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.endpoint.group.ConsumerTargetSelector;
87 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.endpoint.group.ConsumerTargetSelectorBuilder;
88 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.endpoint.group.ProviderNamedSelector;
89 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.endpoint.group.ProviderNamedSelectorBuilder;
90 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.endpoint.group.ProviderTargetSelector;
91 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.endpoint.group.ProviderTargetSelectorBuilder;
92
93 import com.google.common.collect.ImmutableList;
94 import com.google.common.collect.ImmutableSet;
95 import com.google.common.collect.Table;
96
97 public class PolicyResolutionTest {
98
99     Quality q1 = new QualityBuilder().setName(new QualityName("q1")).build();
100     Quality q2 = new QualityBuilder().setName(new QualityName("q2")).build();
101     Quality q3 = new QualityBuilder().setName(new QualityName("q3")).build();
102
103     Requirement r1 = new RequirementBuilder().setName(new RequirementName("r1")).build();
104     Requirement r3 = new RequirementBuilder().setName(new RequirementName("r3")).build();
105
106     Capability cap1 = new CapabilityBuilder().setName(new CapabilityName("cap1")).build();
107     Capability cap3 = new CapabilityBuilder().setName(new CapabilityName("cap3")).build();
108
109     Condition cond1 = new ConditionBuilder().setName(new ConditionName("cond1")).build();
110     Condition cond2 = new ConditionBuilder().setName(new ConditionName("cond2")).build();
111     Condition cond3 = new ConditionBuilder().setName(new ConditionName("cond3")).build();
112
113     Target t1 = new TargetBuilder().setName(new TargetName("t1")).setQuality(ImmutableList.of(q1, q2)).build();
114     Target t2 = new TargetBuilder().setName(new TargetName("t1")).setQuality(ImmutableList.of(q3)).build();
115     Target t0 = new TargetBuilder().setName(new TargetName("t1")).build();
116
117     Rule rule1 = new RuleBuilder().setName(new RuleName("r1")).setOrder(Integer.valueOf(5)).build();
118     Rule rule2 = new RuleBuilder().setName(new RuleName("r2")).build();
119     Rule rule3 = new RuleBuilder().setName(new RuleName("r3")).build();
120     Subject s1 = new SubjectBuilder().setName(new SubjectName("s1")).setRule(ImmutableList.of(rule1)).build();
121     Subject s2 = new SubjectBuilder().setName(new SubjectName("s1"))
122         .setRule(ImmutableList.of(rule2))
123         .setOrder(Integer.valueOf(3))
124         .build();
125     Subject s3 = new SubjectBuilder().setName(new SubjectName("s3"))
126         .setRule(ImmutableList.of(rule3))
127         .setOrder(Integer.valueOf(3))
128         .build();
129
130     RequirementMatcher rm1 = new RequirementMatcherBuilder().setName(new RequirementMatcherName("rm1"))
131         .setMatcherRequirement(ImmutableList.of(new MatcherRequirementBuilder(r1).build()))
132         .build();
133     CapabilityMatcher capm1 = new CapabilityMatcherBuilder().setName(new CapabilityMatcherName("cap1"))
134         .setMatcherCapability(ImmutableList.of(new MatcherCapabilityBuilder(cap1).build()))
135         .build();
136     ConditionMatcher condm1 = new ConditionMatcherBuilder().setName(new ConditionMatcherName("condm1"))
137         .setCondition(ImmutableList.of(cond1))
138         .setMatchType(MatchType.All)
139         .build();
140     ConditionMatcher condm2 = new ConditionMatcherBuilder().setName(new ConditionMatcherName("condm2"))
141         .setCondition(ImmutableList.of(cond1, cond2))
142         .setMatchType(MatchType.Any)
143         .build();
144     ConditionMatcher condm3 = new ConditionMatcherBuilder().setName(new ConditionMatcherName("condm3"))
145         .setCondition(ImmutableList.of(cond3))
146         .setMatchType(MatchType.None)
147         .build();
148
149     Clause clause1 = new ClauseBuilder().setName(new ClauseName("clause1"))
150         .setConsumerMatchers(
151                 new ConsumerMatchersBuilder().setGroupIdentificationConstraints(
152                         new GroupRequirementConstraintCaseBuilder().setRequirementMatcher(ImmutableList.of(rm1))
153                             .build())
154                     .setConditionMatcher(ImmutableList.of(condm1, condm2, condm3))
155                     .build())
156         .setProviderMatchers(
157                 new ProviderMatchersBuilder().setGroupIdentificationConstraints(
158                         new GroupCapabilityConstraintCaseBuilder().setCapabilityMatcher(ImmutableList.of(capm1))
159                             .build()).build())
160         .setSubjectRefs(ImmutableList.of(s1.getName()))
161         .build();
162     Clause clause3 = new ClauseBuilder().setName(new ClauseName("clause3"))
163         .setSubjectRefs(ImmutableList.of(s3.getName(), s2.getName()))
164         .build();
165     Clause clause0 = new ClauseBuilder().setName(new ClauseName("clause0")).build();
166     Clause clause00 = new ClauseBuilder().setName(new ClauseName("clause00"))
167         .setConsumerMatchers(new ConsumerMatchersBuilder().build())
168         .setProviderMatchers(new ProviderMatchersBuilder().build())
169         .build();
170
171     Contract contract1 = new ContractBuilder().setId(new ContractId("c9eea992-ba51-4e11-b797-986853832ad9"))
172         .setTarget(ImmutableList.of(t1))
173         .setClause(ImmutableList.of(clause1, clause0, clause00))
174         .setSubject(ImmutableList.of(s1))
175         .build();
176     Contract contract2 = new ContractBuilder().setId(new ContractId("3a3b67ff-1795-4dc0-a7b2-2c3453872e4e"))
177         .setTarget(ImmutableList.of(t1, t2))
178         .setClause(ImmutableList.of(clause3))
179         .setSubject(ImmutableList.of(s2))
180         .build();
181     Contract contract0 = new ContractBuilder().setId(new ContractId("ce467a3c-2c7b-4e9e-a575-7da1fbdf1833")).build();
182     Contract contract00 = new ContractBuilder().setId(new ContractId("79de88e8-b37f-4764-a1a3-7f3b37b15433"))
183         .setTarget(ImmutableList.of(t0))
184         .build();
185
186     ConsumerNamedSelector cns1 = new ConsumerNamedSelectorBuilder().setName(new SelectorName("cns1"))
187         .setContract(ImmutableList.of(contract1.getId()))
188         .setRequirement(ImmutableList.of(r1, r3))
189         .build();
190     ConsumerNamedSelector cns2 = new ConsumerNamedSelectorBuilder().setName(new SelectorName("cns2"))
191         .setContract(ImmutableList.of(contract2.getId()))
192         .setRequirement(ImmutableList.of(r1, r3))
193         .build();
194     ProviderNamedSelector pns1 = new ProviderNamedSelectorBuilder().setName(new SelectorName("pns1"))
195         .setContract(ImmutableList.of(contract1.getId(), contract2.getId()))
196         .setCapability(ImmutableList.of(cap1, cap3))
197         .build();
198
199     QualityMatcher qm1 = new QualityMatcherBuilder().setName(new QualityMatcherName("qm1"))
200         .setMatcherQuality(ImmutableList.of(new MatcherQualityBuilder(q1).build()))
201         .build();
202     QualityMatcher qm3 = new QualityMatcherBuilder().setName(new QualityMatcherName("qm3"))
203         .setMatcherQuality(ImmutableList.of(new MatcherQualityBuilder(q3).build()))
204         .build();
205     ConsumerTargetSelector cts1 = new ConsumerTargetSelectorBuilder().setName(new SelectorName("cts1"))
206         .setQualityMatcher(ImmutableList.of(qm1))
207         .build();
208     ProviderTargetSelector pts1 = new ProviderTargetSelectorBuilder().setName(new SelectorName("pts1"))
209         .setQualityMatcher(ImmutableList.of(qm3))
210         .build();
211
212     EndpointGroup eg1 = new EndpointGroupBuilder().setId(new EndpointGroupId("12802e21-8602-40ec-91d3-a75a296881ab"))
213         .setConsumerNamedSelector(ImmutableList.of(cns1))
214         .build();
215     EndpointGroup eg2 = new EndpointGroupBuilder().setId(new EndpointGroupId("66bb92ff-6e4c-41f1-8c7d-baa322016ab5"))
216         .setProviderNamedSelector(ImmutableList.of(pns1))
217         .build();
218     EndpointGroup eg3 = new EndpointGroupBuilder().setId(new EndpointGroupId("0ed93cb5-28ee-46bd-a5a1-41d6aa88dae5"))
219         .setConsumerNamedSelector(ImmutableList.of(cns1, cns2))
220         .build();
221     EndpointGroup eg4 = new EndpointGroupBuilder().setId(new EndpointGroupId("51eaf011-94a9-4cb1-b12d-149b77c5c016"))
222         .setConsumerTargetSelector(ImmutableList.of(cts1))
223         .build();
224     EndpointGroup eg5 = new EndpointGroupBuilder().setId(new EndpointGroupId("92344738-ba37-4d69-b9e5-904eebdad585"))
225         .setProviderTargetSelector(ImmutableList.of(pts1))
226         .build();
227     EndpointGroup eg0 = new EndpointGroupBuilder().setId(new EndpointGroupId("64e03313-d6d8-43cb-ae4d-5a9b0a410c91"))
228         .build();
229
230     Tenant tenant1 = new TenantBuilder().setId(new TenantId("144b9aec-ef06-44f1-a50c-2fe5be456feb"))
231         .setPolicy(new PolicyBuilder().setContract(ImmutableList.of(contract1, contract2))
232             .setEndpointGroup(ImmutableList.of(eg1, eg2))
233             .build())
234         .build();
235     Tenant tenant2 = new TenantBuilder().setId(new TenantId("138a2bc3-d3cb-4588-ad7a-63c9f19ce3e5"))
236         .setPolicy(new PolicyBuilder().setContract(ImmutableList.of(contract1, contract2))
237             .setEndpointGroup(ImmutableList.of(eg1, eg2, eg3))
238             .build())
239         .build();
240     Tenant tenant3 = new TenantBuilder().setId(new TenantId("d1feede4-c31f-4232-ace2-93fcd065af1d"))
241         .setPolicy(new PolicyBuilder().setContract(ImmutableList.of(contract1, contract2))
242             .setEndpointGroup(ImmutableList.of(eg4, eg5))
243             .build())
244         .build();
245     Tenant tenant0 = new TenantBuilder().build();
246     Tenant tenant00 = new TenantBuilder().setPolicy(new PolicyBuilder()
247         .setContract(ImmutableList.of(contract0, contract00)).setEndpointGroup(ImmutableList.of(eg0)).build()).build();
248
249     public void verifyMatches(List<ContractId> contrids, List<TenantId> contrtids, List<ContractMatch> matches) {
250         HashSet<ContractMatchKey> v = new HashSet<>();
251         for (int i = 0; i < contrids.size(); i++) {
252             v.add(new ContractMatchKey(contrtids.get(i), contrids.get(i)));
253         }
254         assertEquals(contrids.size(), matches.size());
255         for (ContractMatch m : matches) {
256             ContractMatchKey k = new ContractMatchKey(m.contractTenant.getId(), m.contract.getId());
257             assertTrue(v.contains(k));
258         }
259     }
260
261     @Test
262     public void testContractSelection() throws Exception {
263         // named selectors
264         IndexedTenant indexedTenant = new IndexedTenant(tenant1);
265         Table<EgKey, EgKey, List<ContractMatch>> contractMatches = ContractResolverUtils.selectContracts(ImmutableSet.of(indexedTenant));
266         assertEquals(1, contractMatches.size());
267         List<ContractMatch> matches = contractMatches.get(new EgKey(tenant1.getId(), eg1.getId()),
268                 new EgKey(tenant1.getId(), eg2.getId()));
269         verifyMatches(ImmutableList.of(contract1.getId()), ImmutableList.of(tenant1.getId()), matches);
270
271         indexedTenant = new IndexedTenant(tenant2);
272         contractMatches = ContractResolverUtils.selectContracts(ImmutableSet.of(indexedTenant));
273         assertEquals(2, contractMatches.size());
274         matches = contractMatches.get(new EgKey(tenant2.getId(), eg1.getId()), new EgKey(tenant2.getId(), eg2.getId()));
275         verifyMatches(ImmutableList.of(contract1.getId()), ImmutableList.of(tenant2.getId()), matches);
276
277         matches = contractMatches.get(new EgKey(tenant2.getId(), eg3.getId()), new EgKey(tenant2.getId(), eg2.getId()));
278         verifyMatches(ImmutableList.of(contract2.getId(), contract1.getId()),
279                 ImmutableList.of(tenant2.getId(), tenant2.getId()), matches);
280
281         // target selectors
282         indexedTenant = new IndexedTenant(tenant3);
283         contractMatches = ContractResolverUtils.selectContracts(ImmutableSet.of(indexedTenant));
284         assertEquals(1, contractMatches.size());
285         matches = contractMatches.get(new EgKey(tenant3.getId(), eg4.getId()), new EgKey(tenant3.getId(), eg5.getId()));
286         verifyMatches(ImmutableList.of(contract2.getId()), ImmutableList.of(tenant3.getId()), matches);
287
288         // empty matches
289         indexedTenant = new IndexedTenant(tenant0);
290         contractMatches = ContractResolverUtils.selectContracts(ImmutableSet.of(indexedTenant));
291         assertEquals(0, contractMatches.size());
292
293         indexedTenant = new IndexedTenant(tenant00);
294         contractMatches = ContractResolverUtils.selectContracts(ImmutableSet.of(indexedTenant));
295         assertEquals(0, contractMatches.size());
296     }
297
298     @Test
299     public void testSubjectSelection() throws Exception {
300         ConditionSet cs = new ConditionSet(ImmutableSet.of(cond1.getName()), ImmutableSet.of(cond3.getName()),
301                 ImmutableSet.of(ImmutableSet.of(cond1.getName(), cond2.getName())));
302         EndpointConstraint ec1 = new EndpointConstraint(cs, null);
303         EndpointConstraint ec2 = new EndpointConstraint(ConditionSet.EMPTY, null);
304
305         IndexedTenant indexedTenant = new IndexedTenant(tenant1);
306         Table<EgKey, EgKey, List<ContractMatch>> contractMatches = ContractResolverUtils.selectContracts(ImmutableSet.of(indexedTenant));
307         Map<EgKey, Set<ConditionSet>> egConditions = new HashMap<>();
308         Table<EgKey, EgKey, Policy> policy = SubjectResolverUtils.selectSubjects(contractMatches, egConditions);
309         assertEquals(1, policy.size());
310         Policy p = policy.get(new EgKey(tenant1.getId(), eg1.getId()), new EgKey(tenant1.getId(), eg2.getId()));
311         List<RuleGroup> rules = p.getRuleMap().get(ec1, ec2);
312         assertNotNull(rules);
313         assertEquals(1, rules.size());
314         RuleGroup rg = rules.get(0);
315         assertEquals(tenant1.getId(), rg.getContractTenant().getId());
316         assertEquals(contract1.getId(), rg.getRelatedContract().getId());
317         assertEquals(s1.getName(), rg.getRelatedSubject());
318         assertEquals(1, rg.getRules().size());
319         assertEquals(rule1.getName(), rg.getRules().get(0).getName());
320
321         indexedTenant = new IndexedTenant(tenant2);
322         contractMatches = ContractResolverUtils.selectContracts(ImmutableSet.of(indexedTenant));
323         egConditions = new HashMap<>();
324         policy = SubjectResolverUtils.selectSubjects(contractMatches, egConditions);
325
326         assertEquals(2, policy.size());
327         p = policy.get(new EgKey(tenant2.getId(), eg3.getId()), new EgKey(tenant2.getId(), eg2.getId()));
328         rules = p.getRuleMap().get(ec1, ec2);
329         assertNotNull(rules);
330         assertEquals(1, rules.size());
331         rg = rules.get(0);
332         assertEquals(tenant2.getId(), rg.getContractTenant().getId());
333         assertEquals(contract1.getId(), rg.getRelatedContract().getId());
334         assertEquals(s1.getName(), rg.getRelatedSubject());
335         assertEquals(1, rg.getRules().size());
336         assertEquals(rule1.getName(), rg.getRules().get(0).getName());
337
338         rules = p.getRuleMap().get(ec2, ec2);
339         assertNotNull(rules);
340         assertEquals(1, rules.size());
341         rg = rules.get(0);
342         assertEquals(tenant2.getId(), rg.getContractTenant().getId());
343         assertEquals(contract2.getId(), rg.getRelatedContract().getId());
344         assertEquals(s2.getName(), rg.getRelatedSubject());
345         assertEquals(1, rg.getRules().size());
346         assertEquals(rule2.getName(), rg.getRules().get(0).getName());
347
348         p = policy.get(new EgKey(tenant2.getId(), eg1.getId()), new EgKey(tenant2.getId(), eg2.getId()));
349         rules = p.getRuleMap().get(ec1, ec2);
350         assertNotNull(rules);
351         assertEquals(1, rules.size());
352         rg = rules.get(0);
353         assertEquals(tenant2.getId(), rg.getContractTenant().getId());
354         assertEquals(contract1.getId(), rg.getRelatedContract().getId());
355         assertEquals(s1.getName(), rg.getRelatedSubject());
356         assertEquals(1, rg.getRules().size());
357         assertEquals(rule1.getName(), rg.getRules().get(0).getName());
358
359         indexedTenant = new IndexedTenant(tenant3);
360         contractMatches = ContractResolverUtils.selectContracts(ImmutableSet.of(indexedTenant));
361         egConditions = new HashMap<>();
362         policy = SubjectResolverUtils.selectSubjects(contractMatches, egConditions);
363
364         assertEquals(1, policy.size());
365         p = policy.get(new EgKey(tenant3.getId(), eg4.getId()), new EgKey(tenant3.getId(), eg5.getId()));
366         rules = p.getRuleMap().get(ec2, ec2);
367         assertNotNull(rules);
368         assertEquals(1, rules.size());
369         rg = rules.get(0);
370         assertEquals(tenant3.getId(), rg.getContractTenant().getId());
371         assertEquals(contract2.getId(), rg.getRelatedContract().getId());
372         assertEquals(s2.getName(), rg.getRelatedSubject());
373         assertEquals(1, rg.getRules().size());
374         assertEquals(rule2.getName(), rg.getRules().get(0).getName());
375     }
376
377     private static class ContractMatchKey {
378
379         TenantId tenant;
380         ContractId contract;
381
382         public ContractMatchKey(TenantId tenant, ContractId contract) {
383             super();
384             this.tenant = tenant;
385             this.contract = contract;
386         }
387
388         @Override
389         public int hashCode() {
390             final int prime = 31;
391             int result = 1;
392             result = prime * result + ((contract == null) ? 0 : contract.hashCode());
393             result = prime * result + ((tenant == null) ? 0 : tenant.hashCode());
394             return result;
395         }
396
397         @Override
398         public boolean equals(Object obj) {
399             if (this == obj)
400                 return true;
401             if (obj == null)
402                 return false;
403             if (getClass() != obj.getClass())
404                 return false;
405             ContractMatchKey other = (ContractMatchKey) obj;
406             if (contract == null) {
407                 if (other.contract != null)
408                     return false;
409             } else if (!contract.equals(other.contract))
410                 return false;
411             if (tenant == null) {
412                 if (other.tenant != null)
413                     return false;
414             } else if (!tenant.equals(other.tenant))
415                 return false;
416             return true;
417         }
418
419         @Override
420         public String toString() {
421             return "ContractMatchKey [tenant=" + tenant + ", contract=" + contract + "]";
422         }
423     }
424
425 }