Separating renderers into features.
[groupbasedpolicy.git] / renderers / ofoverlay / 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 java.util.Collection;
12 import java.util.Collections;
13 import java.util.HashMap;
14 import java.util.HashSet;
15 import java.util.List;
16 import java.util.Map;
17 import java.util.Set;
18
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.opendaylight.groupbasedpolicy.resolver.PolicyResolver;
22 import org.opendaylight.groupbasedpolicy.resolver.ConditionSet;
23 import org.opendaylight.groupbasedpolicy.resolver.PolicyResolver.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.RequirementMatcher;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.contract.clause.consumer.matchers.RequirementMatcherBuilder;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.contract.clause.consumer.matchers.requirement.matcher.MatcherRequirementBuilder;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.contract.clause.provider.matchers.CapabilityMatcher;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.contract.clause.provider.matchers.CapabilityMatcherBuilder;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.contract.clause.provider.matchers.capability.matcher.MatcherCapabilityBuilder;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.contract.subject.Rule;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.contract.subject.RuleBuilder;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.endpoint.group.ConsumerNamedSelector;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.endpoint.group.ConsumerNamedSelectorBuilder;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.endpoint.group.ConsumerTargetSelector;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.endpoint.group.ConsumerTargetSelectorBuilder;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.endpoint.group.ProviderNamedSelector;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.endpoint.group.ProviderNamedSelectorBuilder;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.endpoint.group.ProviderTargetSelector;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.endpoint.group.ProviderTargetSelectorBuilder;
85
86 import com.google.common.collect.ImmutableList;
87 import com.google.common.collect.ImmutableSet;
88 import com.google.common.collect.Table;
89
90 import static org.junit.Assert.*;
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             .setRequirementMatcher(ImmutableList.of(rm1))
192             .setConditionMatcher(ImmutableList.of(condm1, condm2, condm3))
193             .build())
194         .setProviderMatchers(new ProviderMatchersBuilder()
195             .setCapabilityMatcher(ImmutableList.of(capm1))
196             .build())
197         .setSubjectRefs(ImmutableList.of(s1.getName()))
198         .build();
199     Clause clause3 = new ClauseBuilder()
200         .setName(new ClauseName("clause3"))
201         .setSubjectRefs(ImmutableList.of(s3.getName(), s2.getName()))
202         .build();
203     Clause clause0 = new ClauseBuilder()
204         .setName(new ClauseName("clause0"))
205         .build();
206     Clause clause00 = new ClauseBuilder()
207         .setName(new ClauseName("clause00"))
208         .setConsumerMatchers(new ConsumerMatchersBuilder().build())
209         .setProviderMatchers(new ProviderMatchersBuilder().build())
210         .build();
211     
212     Contract contract1 = new ContractBuilder()
213         .setId(new ContractId("c9eea992-ba51-4e11-b797-986853832ad9"))
214         .setTarget(ImmutableList.of(t1))
215         .setClause(ImmutableList.of(clause1, clause0, clause00))
216         .setSubject(ImmutableList.of(s1))
217         .build();
218     Contract contract2 = new ContractBuilder()
219         .setId(new ContractId("3a3b67ff-1795-4dc0-a7b2-2c3453872e4e"))
220         .setTarget(ImmutableList.of(t1, t2))
221         .setClause(ImmutableList.of(clause3))
222         .setSubject(ImmutableList.of(s2))
223         .build();
224     Contract contract0 = new ContractBuilder()
225         .setId(new ContractId("ce467a3c-2c7b-4e9e-a575-7da1fbdf1833"))
226         .build();
227     Contract contract00 = new ContractBuilder()
228         .setId(new ContractId("79de88e8-b37f-4764-a1a3-7f3b37b15433"))
229         .setTarget(ImmutableList.of(t0))
230         .build();
231     
232     ConsumerNamedSelector cns1 = new ConsumerNamedSelectorBuilder()
233         .setName(new SelectorName("cns1"))
234         .setContract(ImmutableList.of(contract1.getId()))
235         .setRequirement(ImmutableList.of(r1, r3))
236         .build();
237     ConsumerNamedSelector cns2 = new ConsumerNamedSelectorBuilder()
238         .setName(new SelectorName("cns2"))
239         .setContract(ImmutableList.of(contract2.getId()))
240         .setRequirement(ImmutableList.of(r1, r3))
241         .build();
242     ProviderNamedSelector pns1 = new ProviderNamedSelectorBuilder()
243         .setName(new SelectorName("pns1"))
244         .setContract(ImmutableList.of(contract1.getId(), contract2.getId()))
245         .setCapability(ImmutableList.of(cap1, cap3))
246         .build();
247     
248     QualityMatcher qm1 = new QualityMatcherBuilder()
249         .setName(new QualityMatcherName("qm1"))
250         .setMatcherQuality(ImmutableList.of(new MatcherQualityBuilder(q1).build()))
251         .build();
252     QualityMatcher qm3 = new QualityMatcherBuilder()
253          .setName(new QualityMatcherName("qm3"))
254          .setMatcherQuality(ImmutableList.of(new MatcherQualityBuilder(q3).build()))
255          .build();
256     ConsumerTargetSelector cts1 = new ConsumerTargetSelectorBuilder()
257         .setName(new SelectorName("cts1"))
258         .setQualityMatcher(ImmutableList.of(qm1))
259         .build();
260     ProviderTargetSelector pts1 = new ProviderTargetSelectorBuilder()
261         .setName(new SelectorName("pts1"))
262         .setQualityMatcher(ImmutableList.of(qm3))
263         .build();
264     
265     EndpointGroup eg1 = new EndpointGroupBuilder()
266         .setId(new EndpointGroupId("12802e21-8602-40ec-91d3-a75a296881ab"))
267         .setConsumerNamedSelector(ImmutableList.of(cns1))
268         .build();
269     EndpointGroup eg2 = new EndpointGroupBuilder()
270         .setId(new EndpointGroupId("66bb92ff-6e4c-41f1-8c7d-baa322016ab5"))
271         .setProviderNamedSelector(ImmutableList.of(pns1))
272         .build();
273     EndpointGroup eg3 = new EndpointGroupBuilder()
274         .setId(new EndpointGroupId("0ed93cb5-28ee-46bd-a5a1-41d6aa88dae5"))
275         .setConsumerNamedSelector(ImmutableList.of(cns1, cns2))
276         .build();
277     EndpointGroup eg4 = new EndpointGroupBuilder()
278         .setId(new EndpointGroupId("51eaf011-94a9-4cb1-b12d-149b77c5c016"))
279         .setConsumerTargetSelector(ImmutableList.of(cts1))
280         .build();
281     EndpointGroup eg5 = new EndpointGroupBuilder()
282         .setId(new EndpointGroupId("92344738-ba37-4d69-b9e5-904eebdad585"))
283         .setProviderTargetSelector(ImmutableList.of(pts1))
284         .build();
285     EndpointGroup eg0 = new EndpointGroupBuilder()
286         .setId(new EndpointGroupId("64e03313-d6d8-43cb-ae4d-5a9b0a410c91"))
287         .build();
288
289     Tenant tenant1 = new TenantBuilder()
290         .setId(new TenantId("144b9aec-ef06-44f1-a50c-2fe5be456feb"))
291         .setContract(ImmutableList.of(contract1, contract2))
292         .setEndpointGroup(ImmutableList.of(eg1, eg2))
293         .build();
294     Tenant tenant2 = new TenantBuilder()
295         .setId(new TenantId("138a2bc3-d3cb-4588-ad7a-63c9f19ce3e5"))
296         .setContract(ImmutableList.of(contract1, contract2))
297         .setEndpointGroup(ImmutableList.of(eg1, eg2, eg3))
298         .build();
299     Tenant tenant3 = new TenantBuilder()
300         .setId(new TenantId("d1feede4-c31f-4232-ace2-93fcd065af1d"))
301         .setContract(ImmutableList.of(contract1, contract2))
302         .setEndpointGroup(ImmutableList.of(eg4, eg5))
303         .build();
304     Tenant tenant0 = new TenantBuilder().build();
305     Tenant tenant00 = new TenantBuilder()
306         .setContract(ImmutableList.of(contract0, contract00))
307         .setEndpointGroup(ImmutableList.of(eg0))
308         .build();
309
310     PolicyResolver resolver;
311     
312     @Before
313     public void setup() throws Exception {
314         resolver = new PolicyResolver(null, null);
315     }
316     
317     public void verifyMatches(List<ContractId> contrids,
318                               List<TenantId> contrtids,
319                               List<ContractMatch> matches) {
320         HashSet<ContractMatchKey> v = new HashSet<>();
321         for (int i = 0; i < contrids.size(); i++) {
322             v.add(new ContractMatchKey(contrtids.get(i), contrids.get(i)));
323         }
324         assertEquals(contrids.size(), matches.size());
325         for (ContractMatch m : matches) {
326             ContractMatchKey k = 
327                     new ContractMatchKey(m.contractTenant.getId(), 
328                                          m.contract.getId());
329             assertTrue(v.contains(k));
330         }
331     }
332     
333     @Test
334     public void testContractSelection() throws Exception {
335         // named selectors
336         TenantContext tc = new TenantContext(null);
337         Collection<TenantContext> tCol = Collections.singleton(tc);
338         
339         tc.tenant.set(new IndexedTenant(tenant1));
340         Table<EgKey, EgKey, List<ContractMatch>> contractMatches =
341                 resolver.selectContracts(tCol);
342         assertEquals(1, contractMatches.size());
343         List<ContractMatch> matches = 
344                 contractMatches.get(new EgKey(tenant1.getId(), eg1.getId()),
345                                     new EgKey(tenant1.getId(), eg2.getId()));
346         verifyMatches(ImmutableList.of(contract1.getId()),
347                       ImmutableList.of(tenant1.getId()),
348                       matches);
349
350         
351         tc.tenant.set(new IndexedTenant(tenant2));
352         contractMatches = resolver.selectContracts(tCol);
353         assertEquals(2, contractMatches.size());
354         matches = contractMatches.get(new EgKey(tenant2.getId(), eg1.getId()),
355                                       new EgKey(tenant2.getId(), eg2.getId()));
356         verifyMatches(ImmutableList.of(contract1.getId()),
357                       ImmutableList.of(tenant2.getId()),
358                       matches);
359         
360         matches = contractMatches.get(new EgKey(tenant2.getId(), eg3.getId()),
361                                       new EgKey(tenant2.getId(), eg2.getId()));
362         verifyMatches(ImmutableList.of(contract2.getId(), contract1.getId()),
363                       ImmutableList.of(tenant2.getId(), tenant2.getId()),
364                       matches);
365         
366         // target selectors
367         tc.tenant.set(new IndexedTenant(tenant3));
368         contractMatches = resolver.selectContracts(tCol);
369         assertEquals(1, contractMatches.size());
370         matches = contractMatches.get(new EgKey(tenant3.getId(), eg4.getId()),
371                                       new EgKey(tenant3.getId(), eg5.getId()));
372         verifyMatches(ImmutableList.of(contract2.getId()),
373                       ImmutableList.of(tenant3.getId()),
374                       matches);
375         
376         // empty matches
377         tc.tenant.set(new IndexedTenant(tenant0));
378         contractMatches = resolver.selectContracts(tCol);
379         assertEquals(0, contractMatches.size());
380
381         tc.tenant.set(new IndexedTenant(tenant00));
382         contractMatches = resolver.selectContracts(tCol);
383         assertEquals(0, contractMatches.size());
384     }
385
386     @Test
387     public void testSubjectSelection() throws Exception {
388         ConditionSet cs = 
389                 new ConditionSet(ImmutableSet.of(cond1.getName()), 
390                                  ImmutableSet.of(cond3.getName()),
391                                  ImmutableSet.of(ImmutableSet.of(cond1.getName(), 
392                                                                  cond2.getName())));
393         TenantContext tc = new TenantContext(null);
394         Collection<TenantContext> tCol = Collections.singleton(tc);
395         
396         tc.tenant.set(new IndexedTenant(tenant1));
397         Table<EgKey, EgKey, List<ContractMatch>> contractMatches =
398                 resolver.selectContracts(tCol);
399         Map<EgKey, Set<ConditionSet>> egConditions = new HashMap<>();
400         Table<EgKey, EgKey, Policy> policy = 
401                 resolver.selectSubjects(contractMatches, egConditions);
402         assertEquals(1, policy.size());
403         Policy p = policy.get(new EgKey(tenant1.getId(), eg2.getId()),
404                               new EgKey(tenant1.getId(), eg1.getId()));
405         List<RuleGroup> rules = p.ruleMap.get(ConditionSet.EMPTY, cs);
406         assertNotNull(rules);
407         assertEquals(1, rules.size());
408         RuleGroup rg = rules.get(0);
409         assertEquals(tenant1.getId(), rg.contractTenant.getId());
410         assertEquals(contract1.getId(), rg.relatedContract.getId());
411         assertEquals(s1.getName(), rg.relatedSubject);
412         assertEquals(1, rg.rules.size());
413         assertEquals(rule1.getName(), rg.rules.get(0).getName());
414
415         tc.tenant.set(new IndexedTenant(tenant2));
416         contractMatches = resolver.selectContracts(tCol);
417         egConditions = new HashMap<>();
418         policy = resolver.selectSubjects(contractMatches, egConditions);
419
420         assertEquals(2, policy.size());
421         p = policy.get(new EgKey(tenant2.getId(), eg2.getId()),
422                        new EgKey(tenant2.getId(), eg3.getId()));
423         rules = p.ruleMap.get(ConditionSet.EMPTY, cs);
424         assertNotNull(rules);
425         assertEquals(1, rules.size());
426         rg = rules.get(0);
427         assertEquals(tenant2.getId(), rg.contractTenant.getId());
428         assertEquals(contract1.getId(), rg.relatedContract.getId());
429         assertEquals(s1.getName(), rg.relatedSubject);
430         assertEquals(1, rg.rules.size());
431         assertEquals(rule1.getName(), rg.rules.get(0).getName());
432
433         rules = p.ruleMap.get(ConditionSet.EMPTY, ConditionSet.EMPTY);
434         assertNotNull(rules);
435         assertEquals(1, rules.size());
436         rg = rules.get(0);
437         assertEquals(tenant2.getId(), rg.contractTenant.getId());
438         assertEquals(contract2.getId(), rg.relatedContract.getId());
439         assertEquals(s2.getName(), rg.relatedSubject);
440         assertEquals(1, rg.rules.size());
441         assertEquals(rule2.getName(), rg.rules.get(0).getName());
442         
443         p = policy.get(new EgKey(tenant2.getId(), eg2.getId()),
444                        new EgKey(tenant2.getId(), eg1.getId()));
445         rules = p.ruleMap.get(ConditionSet.EMPTY, cs);
446         assertNotNull(rules);
447         assertEquals(1, rules.size());
448         rg = rules.get(0);
449         assertEquals(tenant2.getId(), rg.contractTenant.getId());
450         assertEquals(contract1.getId(), rg.relatedContract.getId());
451         assertEquals(s1.getName(), rg.relatedSubject);
452         assertEquals(1, rg.rules.size());
453         assertEquals(rule1.getName(), rg.rules.get(0).getName());
454
455         tc.tenant.set(new IndexedTenant(tenant3));
456         contractMatches = resolver.selectContracts(tCol);
457         egConditions = new HashMap<>();
458         policy = resolver.selectSubjects(contractMatches, egConditions);
459
460         assertEquals(1, policy.size());
461         p = policy.get(new EgKey(tenant3.getId(), eg5.getId()),
462                        new EgKey(tenant3.getId(), eg4.getId()));
463         rules = p.ruleMap.get(ConditionSet.EMPTY, ConditionSet.EMPTY);
464         assertNotNull(rules);
465         assertEquals(1, rules.size());
466         rg = rules.get(0);
467         assertEquals(tenant3.getId(), rg.contractTenant.getId());
468         assertEquals(contract2.getId(), rg.relatedContract.getId());
469         assertEquals(s2.getName(), rg.relatedSubject);
470         assertEquals(1, rg.rules.size());
471         assertEquals(rule2.getName(), rg.rules.get(0).getName());
472     }
473
474     private static class ContractMatchKey {
475         TenantId tenant;
476         ContractId contract;
477         public ContractMatchKey(TenantId tenant, ContractId contract) {
478             super();
479             this.tenant = tenant;
480             this.contract = contract;
481         }
482         @Override
483         public int hashCode() {
484             final int prime = 31;
485             int result = 1;
486             result = prime * result +
487                      ((contract == null) ? 0 : contract.hashCode());
488             result = prime * result +
489                      ((tenant == null) ? 0 : tenant.hashCode());
490             return result;
491         }
492         @Override
493         public boolean equals(Object obj) {
494             if (this == obj)
495                 return true;
496             if (obj == null)
497                 return false;
498             if (getClass() != obj.getClass())
499                 return false;
500             ContractMatchKey other = (ContractMatchKey) obj;
501             if (contract == null) {
502                 if (other.contract != null)
503                     return false;
504             } else if (!contract.equals(other.contract))
505                 return false;
506             if (tenant == null) {
507                 if (other.tenant != null)
508                     return false;
509             } else if (!tenant.equals(other.tenant))
510                 return false;
511             return true;
512         }
513         @Override
514         public String toString() {
515             return "ContractMatchKey [tenant=" + tenant + ", contract=" +
516                    contract + "]";
517         }
518     }
519
520 }