2 * Copyright (c) 2016 Red Hat, Inc. 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.aaa.idpmapping;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14 import static org.mockito.Matchers.any;
15 import static org.mockito.Mockito.times;
16 import static org.mockito.Mockito.verify;
17 import static org.mockito.Mockito.when;
19 import java.util.ArrayList;
20 import java.util.HashMap;
21 import java.util.List;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.mockito.Mock;
27 import org.mockito.Mockito;
28 import org.powermock.api.mockito.PowerMockito;
29 import org.powermock.api.support.membermodification.MemberMatcher;
30 import org.powermock.api.support.membermodification.MemberModifier;
31 import org.powermock.core.classloader.annotations.PrepareForTest;
32 import org.powermock.modules.junit4.PowerMockRunner;
33 import org.powermock.reflect.Whitebox;
35 @PrepareForTest(RuleProcessor.class)
36 @RunWith(PowerMockRunner.class)
37 public class RuleProcessorTest {
40 private RuleProcessor ruleProcess;
44 ruleProcess = PowerMockito.mock(RuleProcessor.class, Mockito.CALLS_REAL_METHODS);
48 public void testJoin() {
49 List<Object> list = new ArrayList<Object>();
53 assertEquals("str1/str2/str3", RuleProcessor.join(list, "/"));
57 public void testSubstituteVariables() {
58 Map<String, Object> namespace = new HashMap<String, Object>() {
60 put("foo1", new HashMap<String, String>() {
67 String str = "foo1[0]";
68 String subVariable = ruleProcess.substituteVariables(str, namespace);
69 assertNotNull(subVariable);
70 assertEquals(subVariable, str);
74 public void testGetMapping() {
75 Map<String, Object> namespace = new HashMap<String, Object>() {
77 put("foo1", new HashMap<String, String>() {
84 final Map<String, Object> item = new HashMap<String, Object>() {
89 Map<String, Object> rules = new HashMap<String, Object>() {
92 put("mapping_name", "mapping");
95 Map<String, Object> mapping = ruleProcess.getMapping(namespace, rules);
96 assertNotNull(mapping);
97 assertTrue(mapping.containsKey("str"));
98 assertEquals("val", mapping.get("str"));
102 public void testProcess() throws Exception {
103 String json = " {\"rules\":[" + "{\"Name\":\"user\", \"Id\":1},"
104 + "{\"Name\":\"Admin\", \"Id\":2}]} ";
105 Map<String, Object> mapping = new HashMap<String, Object>() {
107 put("Name", "Admin");
110 List<Map<String, Object>> internalRules = new ArrayList<Map<String, Object>>();
111 Map<String, Object> internalRule = new HashMap<String, Object>() {
113 put("Name", "Admin");
114 put("statement_blocks", "user");
117 internalRules.add(internalRule);
118 MemberModifier.field(RuleProcessor.class, "rules").set(ruleProcess, internalRules);
119 PowerMockito.suppress(MemberMatcher.method(RuleProcessor.class, "processRule", Map.class,
121 PowerMockito.when(ruleProcess, "processRule", any(Map.class), any(Map.class)).thenReturn(
122 ProcessResult.RULE_SUCCESS);
123 PowerMockito.suppress(MemberMatcher.method(RuleProcessor.class, "getMapping", Map.class,
125 when(ruleProcess.getMapping(any(Map.class), any(Map.class))).thenReturn(mapping);
126 Whitebox.invokeMethod(ruleProcess, "process", json);
127 verify(ruleProcess, times(3)).getMapping(any(Map.class), any(Map.class));