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