f7c3a0a2ca6092df279b3208ebab8fc4baa3be47
[aaf/authz.git] / cadi / core / src / test / java / org / onap / aaf / cadi / lur / test / JU_EpiLur.java
1 /**
2  *
3  * ============LICENSE_START====================================================
4  * org.onap.aaf
5  * ===========================================================================
6  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
7  * ===========================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END====================================================
20  *
21  */
22
23 package org.onap.aaf.cadi.lur.test;
24
25 import static org.hamcrest.CoreMatchers.is;
26 import static org.hamcrest.CoreMatchers.not;
27 import static org.hamcrest.CoreMatchers.nullValue;
28 import static org.junit.Assert.assertThat;
29 import static org.mockito.Mockito.when;
30
31 import java.security.Principal;
32 import java.util.ArrayList;
33 import java.util.List;
34
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.mockito.Mock;
38 import org.mockito.MockitoAnnotations;
39 import org.onap.aaf.cadi.CachingLur;
40 import org.onap.aaf.cadi.CadiException;
41 import org.onap.aaf.cadi.CredVal;
42 import org.onap.aaf.cadi.Lur;
43 import org.onap.aaf.cadi.Permission;
44 import org.onap.aaf.cadi.lur.EpiLur;
45
46 public class JU_EpiLur {
47
48         private ArrayList<Permission> perms;
49         private CredValStub lurMock3;
50
51         @Mock private Lur lurMock1;
52         @Mock private CachingLur<?> lurMock2;
53         @Mock private Principal princMock;
54         @Mock private Permission permMock;
55
56         @Before
57         public void setup() {
58                 MockitoAnnotations.initMocks(this);
59
60                 perms = new ArrayList<>();
61                 perms.add(permMock);
62
63                 lurMock3 = new CredValStub();
64         }
65
66         @Test
67         public void test() throws CadiException {
68                 EpiLur lur;
69                 try {
70                         lur = new EpiLur();
71                 } catch (CadiException e) {
72                         assertThat(e.getMessage(), is("Need at least one Lur implementation in constructor"));
73                 }
74                 lur = new EpiLur(lurMock1, lurMock2, lurMock3);
75                 assertThat(lur.fish(null,  null), is(false));
76
77                 assertThat(lur.fish(princMock, permMock), is(false));
78
79                 when(lurMock2.handlesExclusively(permMock)).thenReturn(true);
80                 assertThat(lur.fish(princMock, permMock), is(false));
81
82                 when(lurMock2.fish(princMock, permMock)).thenReturn(true);
83                 assertThat(lur.fish(princMock, permMock), is(true));
84
85                 lur.fishAll(princMock, perms);
86
87                 assertThat(lur.handlesExclusively(permMock), is(false));
88
89                 assertThat(lur.get(-1), is(nullValue()));
90                 assertThat(lur.get(0), is(lurMock1));
91                 assertThat(lur.get(1), is((Lur)lurMock2));
92                 assertThat(lur.get(2), is((Lur)lurMock3));
93                 assertThat(lur.get(3), is(nullValue()));
94
95                 assertThat(lur.handles(princMock), is(false));
96                 when(lurMock2.handles(princMock)).thenReturn(true);
97                 assertThat(lur.handles(princMock), is(true));
98
99                 lur.remove("id");
100
101                 lur.clear(princMock, null);
102
103                 assertThat(lur.createPerm("perm"), is(not(nullValue())));
104
105                 lur.getUserPassImpl();
106                 assertThat(lur.getUserPassImpl(), is((CredVal)lurMock3));
107
108                 lur.toString();
109                 lur.destroy();
110
111                 lur = new EpiLur(lurMock1, lurMock2);
112                 assertThat(lur.getUserPassImpl(), is(nullValue()));
113
114                 assertThat(lur.subLur(Lur.class), is(nullValue()));
115         }
116
117         private class CredValStub implements Lur, CredVal {
118                 @Override public boolean validate(String user, Type type, byte[] cred, Object state) { return false; }
119                 @Override public Permission createPerm(String p) { return null; }
120                 @Override public boolean fish(Principal bait, Permission pond) { return false; }
121                 @Override public void fishAll(Principal bait, List<Permission> permissions) { }
122                 @Override public void destroy() { }
123                 @Override public boolean handlesExclusively(Permission pond) { return false; }
124                 @Override public boolean handles(Principal principal) { return false; }
125                 @Override public void clear(Principal p, StringBuilder report) { }
126         }
127
128 }