2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.groupbasedpolicy.resolver;
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;
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;
86 import com.google.common.collect.ImmutableList;
87 import com.google.common.collect.ImmutableSet;
88 import com.google.common.collect.Table;
90 import static org.junit.Assert.*;
92 public class PolicyResolverTest {
93 Quality q1 = new QualityBuilder()
94 .setName(new QualityName("q1"))
96 Quality q2 = new QualityBuilder()
97 .setName(new QualityName("q2"))
99 Quality q3 = new QualityBuilder()
100 .setName(new QualityName("q3"))
103 Requirement r1 = new RequirementBuilder()
104 .setName(new RequirementName("r1"))
106 Requirement r3 = new RequirementBuilder()
107 .setName(new RequirementName("r3"))
110 Capability cap1 = new CapabilityBuilder()
111 .setName(new CapabilityName("cap1"))
113 Capability cap3 = new CapabilityBuilder()
114 .setName(new CapabilityName("cap3"))
117 Condition cond1 = new ConditionBuilder()
118 .setName(new ConditionName("cond1"))
120 Condition cond2 = new ConditionBuilder()
121 .setName(new ConditionName("cond2"))
123 Condition cond3 = new ConditionBuilder()
124 .setName(new ConditionName("cond3"))
127 Target t1 = new TargetBuilder()
128 .setName(new TargetName("t1"))
129 .setQuality(ImmutableList.of(q1,q2))
131 Target t2 = new TargetBuilder()
132 .setName(new TargetName("t1"))
133 .setQuality(ImmutableList.of(q3))
135 Target t0 = new TargetBuilder()
136 .setName(new TargetName("t1"))
139 Rule rule1 = new RuleBuilder()
140 .setName(new RuleName("r1"))
141 .setOrder(Integer.valueOf(5))
143 Rule rule2 = new RuleBuilder()
144 .setName(new RuleName("r2"))
146 Rule rule3 = new RuleBuilder()
147 .setName(new RuleName("r3"))
149 Subject s1 = new SubjectBuilder()
150 .setName(new SubjectName("s1"))
151 .setRule(ImmutableList.of(rule1))
153 Subject s2 = new SubjectBuilder()
154 .setName(new SubjectName("s1"))
155 .setRule(ImmutableList.of(rule2))
156 .setOrder(Integer.valueOf(3))
158 Subject s3 = new SubjectBuilder()
159 .setName(new SubjectName("3"))
160 .setRule(ImmutableList.of(rule3))
161 .setOrder(Integer.valueOf(3))
164 RequirementMatcher rm1 = new RequirementMatcherBuilder()
165 .setName(new RequirementMatcherName("rm1"))
166 .setMatcherRequirement(ImmutableList.of(new MatcherRequirementBuilder(r1).build()))
168 CapabilityMatcher capm1 = new CapabilityMatcherBuilder()
169 .setName(new CapabilityMatcherName("cap1"))
170 .setMatcherCapability(ImmutableList.of(new MatcherCapabilityBuilder(cap1).build()))
172 ConditionMatcher condm1 = new ConditionMatcherBuilder()
173 .setName(new ConditionMatcherName("condm1"))
174 .setCondition(ImmutableList.of(cond1))
175 .setMatchType(MatchType.All)
177 ConditionMatcher condm2 = new ConditionMatcherBuilder()
178 .setName(new ConditionMatcherName("condm2"))
179 .setCondition(ImmutableList.of(cond1, cond2))
180 .setMatchType(MatchType.Any)
182 ConditionMatcher condm3 = new ConditionMatcherBuilder()
183 .setName(new ConditionMatcherName("condm3"))
184 .setCondition(ImmutableList.of(cond3))
185 .setMatchType(MatchType.None)
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))
194 .setProviderMatchers(new ProviderMatchersBuilder()
195 .setCapabilityMatcher(ImmutableList.of(capm1))
197 .setSubjectRefs(ImmutableList.of(s1.getName()))
199 Clause clause3 = new ClauseBuilder()
200 .setName(new ClauseName("clause3"))
201 .setSubjectRefs(ImmutableList.of(s3.getName(), s2.getName()))
203 Clause clause0 = new ClauseBuilder()
204 .setName(new ClauseName("clause0"))
206 Clause clause00 = new ClauseBuilder()
207 .setName(new ClauseName("clause00"))
208 .setConsumerMatchers(new ConsumerMatchersBuilder().build())
209 .setProviderMatchers(new ProviderMatchersBuilder().build())
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))
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))
224 Contract contract0 = new ContractBuilder()
225 .setId(new ContractId("ce467a3c-2c7b-4e9e-a575-7da1fbdf1833"))
227 Contract contract00 = new ContractBuilder()
228 .setId(new ContractId("79de88e8-b37f-4764-a1a3-7f3b37b15433"))
229 .setTarget(ImmutableList.of(t0))
232 ConsumerNamedSelector cns1 = new ConsumerNamedSelectorBuilder()
233 .setName(new SelectorName("cns1"))
234 .setContract(ImmutableList.of(contract1.getId()))
235 .setRequirement(ImmutableList.of(r1, r3))
237 ConsumerNamedSelector cns2 = new ConsumerNamedSelectorBuilder()
238 .setName(new SelectorName("cns2"))
239 .setContract(ImmutableList.of(contract2.getId()))
240 .setRequirement(ImmutableList.of(r1, r3))
242 ProviderNamedSelector pns1 = new ProviderNamedSelectorBuilder()
243 .setName(new SelectorName("pns1"))
244 .setContract(ImmutableList.of(contract1.getId(), contract2.getId()))
245 .setCapability(ImmutableList.of(cap1, cap3))
248 QualityMatcher qm1 = new QualityMatcherBuilder()
249 .setName(new QualityMatcherName("qm1"))
250 .setMatcherQuality(ImmutableList.of(new MatcherQualityBuilder(q1).build()))
252 QualityMatcher qm3 = new QualityMatcherBuilder()
253 .setName(new QualityMatcherName("qm3"))
254 .setMatcherQuality(ImmutableList.of(new MatcherQualityBuilder(q3).build()))
256 ConsumerTargetSelector cts1 = new ConsumerTargetSelectorBuilder()
257 .setName(new SelectorName("cts1"))
258 .setQualityMatcher(ImmutableList.of(qm1))
260 ProviderTargetSelector pts1 = new ProviderTargetSelectorBuilder()
261 .setName(new SelectorName("pts1"))
262 .setQualityMatcher(ImmutableList.of(qm3))
265 EndpointGroup eg1 = new EndpointGroupBuilder()
266 .setId(new EndpointGroupId("12802e21-8602-40ec-91d3-a75a296881ab"))
267 .setConsumerNamedSelector(ImmutableList.of(cns1))
269 EndpointGroup eg2 = new EndpointGroupBuilder()
270 .setId(new EndpointGroupId("66bb92ff-6e4c-41f1-8c7d-baa322016ab5"))
271 .setProviderNamedSelector(ImmutableList.of(pns1))
273 EndpointGroup eg3 = new EndpointGroupBuilder()
274 .setId(new EndpointGroupId("0ed93cb5-28ee-46bd-a5a1-41d6aa88dae5"))
275 .setConsumerNamedSelector(ImmutableList.of(cns1, cns2))
277 EndpointGroup eg4 = new EndpointGroupBuilder()
278 .setId(new EndpointGroupId("51eaf011-94a9-4cb1-b12d-149b77c5c016"))
279 .setConsumerTargetSelector(ImmutableList.of(cts1))
281 EndpointGroup eg5 = new EndpointGroupBuilder()
282 .setId(new EndpointGroupId("92344738-ba37-4d69-b9e5-904eebdad585"))
283 .setProviderTargetSelector(ImmutableList.of(pts1))
285 EndpointGroup eg0 = new EndpointGroupBuilder()
286 .setId(new EndpointGroupId("64e03313-d6d8-43cb-ae4d-5a9b0a410c91"))
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))
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))
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))
304 Tenant tenant0 = new TenantBuilder().build();
305 Tenant tenant00 = new TenantBuilder()
306 .setContract(ImmutableList.of(contract0, contract00))
307 .setEndpointGroup(ImmutableList.of(eg0))
310 PolicyResolver resolver;
313 public void setup() throws Exception {
314 resolver = new PolicyResolver(null, null);
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)));
324 assertEquals(contrids.size(), matches.size());
325 for (ContractMatch m : matches) {
327 new ContractMatchKey(m.contractTenant.getId(),
329 assertTrue(v.contains(k));
334 public void testContractSelection() throws Exception {
336 TenantContext tc = new TenantContext(null);
337 Collection<TenantContext> tCol = Collections.singleton(tc);
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()),
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()),
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()),
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()),
377 tc.tenant.set(new IndexedTenant(tenant0));
378 contractMatches = resolver.selectContracts(tCol);
379 assertEquals(0, contractMatches.size());
381 tc.tenant.set(new IndexedTenant(tenant00));
382 contractMatches = resolver.selectContracts(tCol);
383 assertEquals(0, contractMatches.size());
387 public void testSubjectSelection() throws Exception {
389 new ConditionSet(ImmutableSet.of(cond1.getName()),
390 ImmutableSet.of(cond3.getName()),
391 ImmutableSet.of(ImmutableSet.of(cond1.getName(),
393 TenantContext tc = new TenantContext(null);
394 Collection<TenantContext> tCol = Collections.singleton(tc);
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());
415 tc.tenant.set(new IndexedTenant(tenant2));
416 contractMatches = resolver.selectContracts(tCol);
417 egConditions = new HashMap<>();
418 policy = resolver.selectSubjects(contractMatches, egConditions);
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());
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());
433 rules = p.ruleMap.get(ConditionSet.EMPTY, ConditionSet.EMPTY);
434 assertNotNull(rules);
435 assertEquals(1, rules.size());
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());
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());
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());
455 tc.tenant.set(new IndexedTenant(tenant3));
456 contractMatches = resolver.selectContracts(tCol);
457 egConditions = new HashMap<>();
458 policy = resolver.selectSubjects(contractMatches, egConditions);
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());
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());
474 private static class ContractMatchKey {
477 public ContractMatchKey(TenantId tenant, ContractId contract) {
479 this.tenant = tenant;
480 this.contract = contract;
483 public int hashCode() {
484 final int prime = 31;
486 result = prime * result +
487 ((contract == null) ? 0 : contract.hashCode());
488 result = prime * result +
489 ((tenant == null) ? 0 : tenant.hashCode());
493 public boolean equals(Object obj) {
498 if (getClass() != obj.getClass())
500 ContractMatchKey other = (ContractMatchKey) obj;
501 if (contract == null) {
502 if (other.contract != null)
504 } else if (!contract.equals(other.contract))
506 if (tenant == null) {
507 if (other.tenant != null)
509 } else if (!tenant.equals(other.tenant))
514 public String toString() {
515 return "ContractMatchKey [tenant=" + tenant + ", contract=" +