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 static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
15 import java.util.HashMap;
16 import java.util.HashSet;
17 import java.util.List;
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;
88 import com.google.common.collect.ImmutableList;
89 import com.google.common.collect.ImmutableSet;
90 import com.google.common.collect.Table;
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("s3"))
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 .setGroupIdentificationConstraints(new GroupRequirementConstraintCaseBuilder()
192 .setRequirementMatcher(ImmutableList.of(rm1)).build())
193 .setConditionMatcher(ImmutableList.of(condm1, condm2, condm3))
195 .setProviderMatchers(new ProviderMatchersBuilder()
196 .setGroupIdentificationConstraints(new GroupCapabilityConstraintCaseBuilder()
197 .setCapabilityMatcher(ImmutableList.of(capm1)).build())
199 .setSubjectRefs(ImmutableList.of(s1.getName()))
201 Clause clause3 = new ClauseBuilder()
202 .setName(new ClauseName("clause3"))
203 .setSubjectRefs(ImmutableList.of(s3.getName(), s2.getName()))
205 Clause clause0 = new ClauseBuilder()
206 .setName(new ClauseName("clause0"))
208 Clause clause00 = new ClauseBuilder()
209 .setName(new ClauseName("clause00"))
210 .setConsumerMatchers(new ConsumerMatchersBuilder().build())
211 .setProviderMatchers(new ProviderMatchersBuilder().build())
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))
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))
226 Contract contract0 = new ContractBuilder()
227 .setId(new ContractId("ce467a3c-2c7b-4e9e-a575-7da1fbdf1833"))
229 Contract contract00 = new ContractBuilder()
230 .setId(new ContractId("79de88e8-b37f-4764-a1a3-7f3b37b15433"))
231 .setTarget(ImmutableList.of(t0))
234 ConsumerNamedSelector cns1 = new ConsumerNamedSelectorBuilder()
235 .setName(new SelectorName("cns1"))
236 .setContract(ImmutableList.of(contract1.getId()))
237 .setRequirement(ImmutableList.of(r1, r3))
239 ConsumerNamedSelector cns2 = new ConsumerNamedSelectorBuilder()
240 .setName(new SelectorName("cns2"))
241 .setContract(ImmutableList.of(contract2.getId()))
242 .setRequirement(ImmutableList.of(r1, r3))
244 ProviderNamedSelector pns1 = new ProviderNamedSelectorBuilder()
245 .setName(new SelectorName("pns1"))
246 .setContract(ImmutableList.of(contract1.getId(), contract2.getId()))
247 .setCapability(ImmutableList.of(cap1, cap3))
250 QualityMatcher qm1 = new QualityMatcherBuilder()
251 .setName(new QualityMatcherName("qm1"))
252 .setMatcherQuality(ImmutableList.of(new MatcherQualityBuilder(q1).build()))
254 QualityMatcher qm3 = new QualityMatcherBuilder()
255 .setName(new QualityMatcherName("qm3"))
256 .setMatcherQuality(ImmutableList.of(new MatcherQualityBuilder(q3).build()))
258 ConsumerTargetSelector cts1 = new ConsumerTargetSelectorBuilder()
259 .setName(new SelectorName("cts1"))
260 .setQualityMatcher(ImmutableList.of(qm1))
262 ProviderTargetSelector pts1 = new ProviderTargetSelectorBuilder()
263 .setName(new SelectorName("pts1"))
264 .setQualityMatcher(ImmutableList.of(qm3))
267 EndpointGroup eg1 = new EndpointGroupBuilder()
268 .setId(new EndpointGroupId("12802e21-8602-40ec-91d3-a75a296881ab"))
269 .setConsumerNamedSelector(ImmutableList.of(cns1))
271 EndpointGroup eg2 = new EndpointGroupBuilder()
272 .setId(new EndpointGroupId("66bb92ff-6e4c-41f1-8c7d-baa322016ab5"))
273 .setProviderNamedSelector(ImmutableList.of(pns1))
275 EndpointGroup eg3 = new EndpointGroupBuilder()
276 .setId(new EndpointGroupId("0ed93cb5-28ee-46bd-a5a1-41d6aa88dae5"))
277 .setConsumerNamedSelector(ImmutableList.of(cns1, cns2))
279 EndpointGroup eg4 = new EndpointGroupBuilder()
280 .setId(new EndpointGroupId("51eaf011-94a9-4cb1-b12d-149b77c5c016"))
281 .setConsumerTargetSelector(ImmutableList.of(cts1))
283 EndpointGroup eg5 = new EndpointGroupBuilder()
284 .setId(new EndpointGroupId("92344738-ba37-4d69-b9e5-904eebdad585"))
285 .setProviderTargetSelector(ImmutableList.of(pts1))
287 EndpointGroup eg0 = new EndpointGroupBuilder()
288 .setId(new EndpointGroupId("64e03313-d6d8-43cb-ae4d-5a9b0a410c91"))
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))
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))
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))
306 Tenant tenant0 = new TenantBuilder().build();
307 Tenant tenant00 = new TenantBuilder()
308 .setContract(ImmutableList.of(contract0, contract00))
309 .setEndpointGroup(ImmutableList.of(eg0))
312 PolicyResolver resolver;
315 public void setup() throws Exception {
316 resolver = new PolicyResolver(null, null);
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)));
326 assertEquals(contrids.size(), matches.size());
327 for (ContractMatch m : matches) {
329 new ContractMatchKey(m.contractTenant.getId(),
331 assertTrue(v.contains(k));
336 public void testContractSelection() throws Exception {
338 TenantContext tc = new TenantContext(null);
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()),
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()),
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()),
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()),
378 tc.tenant.set(new IndexedTenant(tenant0));
379 contractMatches = ContractResolverUtils.selectContracts(ImmutableSet.of(tc.tenant.get()));
380 assertEquals(0, contractMatches.size());
382 tc.tenant.set(new IndexedTenant(tenant00));
383 contractMatches = ContractResolverUtils.selectContracts(ImmutableSet.of(tc.tenant.get()));
384 assertEquals(0, contractMatches.size());
388 public void testSubjectSelection() throws Exception {
390 new ConditionSet(ImmutableSet.of(cond1.getName()),
391 ImmutableSet.of(cond3.getName()),
392 ImmutableSet.of(ImmutableSet.of(cond1.getName(),
394 TenantContext tc = new TenantContext(null);
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());
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);
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());
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());
432 rules = p.getRuleMap().get(ConditionSet.EMPTY, ConditionSet.EMPTY);
433 assertNotNull(rules);
434 assertEquals(1, rules.size());
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());
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());
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());
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);
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());
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());
473 private static class ContractMatchKey {
476 public ContractMatchKey(TenantId tenant, ContractId contract) {
478 this.tenant = tenant;
479 this.contract = contract;
482 public int hashCode() {
483 final int prime = 31;
485 result = prime * result +
486 ((contract == null) ? 0 : contract.hashCode());
487 result = prime * result +
488 ((tenant == null) ? 0 : tenant.hashCode());
492 public boolean equals(Object obj) {
497 if (getClass() != obj.getClass())
499 ContractMatchKey other = (ContractMatchKey) obj;
500 if (contract == null) {
501 if (other.contract != null)
503 } else if (!contract.equals(other.contract))
505 if (tenant == null) {
506 if (other.tenant != null)
508 } else if (!tenant.equals(other.tenant))
513 public String toString() {
514 return "ContractMatchKey [tenant=" + tenant + ", contract=" +