3 * ============LICENSE_START====================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
23 package org.onap.aaf.cadi.lur.test;
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;
31 import java.security.Principal;
32 import java.util.ArrayList;
33 import java.util.List;
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;
46 public class JU_EpiLur {
48 private ArrayList<Permission> perms;
49 private CredValStub lurMock3;
51 @Mock private Lur lurMock1;
52 @Mock private CachingLur<?> lurMock2;
53 @Mock private Principal princMock;
54 @Mock private Permission permMock;
58 MockitoAnnotations.initMocks(this);
60 perms = new ArrayList<>();
63 lurMock3 = new CredValStub();
67 public void test() throws CadiException {
71 } catch (CadiException e) {
72 assertThat(e.getMessage(), is("Need at least one Lur implementation in constructor"));
74 lur = new EpiLur(lurMock1, lurMock2, lurMock3);
75 assertThat(lur.fish(null, null), is(false));
77 assertThat(lur.fish(princMock, permMock), is(false));
79 when(lurMock2.handlesExclusively(permMock)).thenReturn(true);
80 assertThat(lur.fish(princMock, permMock), is(false));
82 when(lurMock2.fish(princMock, permMock)).thenReturn(true);
83 assertThat(lur.fish(princMock, permMock), is(true));
85 lur.fishAll(princMock, perms);
87 assertThat(lur.handlesExclusively(permMock), is(false));
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()));
95 assertThat(lur.handles(princMock), is(false));
96 when(lurMock2.handles(princMock)).thenReturn(true);
97 assertThat(lur.handles(princMock), is(true));
101 lur.clear(princMock, null);
103 assertThat(lur.createPerm("perm"), is(not(nullValue())));
105 lur.getUserPassImpl();
106 assertThat(lur.getUserPassImpl(), is((CredVal)lurMock3));
111 lur = new EpiLur(lurMock1, lurMock2);
112 assertThat(lur.getUserPassImpl(), is(nullValue()));
114 assertThat(lur.subLur(Lur.class), is(nullValue()));
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) { }