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