2 * ============LICENSE_START====================================================
4 * ===========================================================================
5 * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6 * ===========================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END====================================================
22 package org.onap.aaf.cadi.oauth.test;
24 import static org.mockito.Mockito.when;
25 import static org.hamcrest.CoreMatchers.is;
26 import static org.junit.Assert.assertThat;
28 import java.security.Principal;
29 import java.util.ArrayList;
30 import java.util.List;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.mockito.Mock;
35 import org.mockito.MockitoAnnotations;
36 import org.onap.aaf.cadi.Permission;
37 import org.onap.aaf.cadi.aaf.AAFPermission;
38 import org.onap.aaf.cadi.oauth.OAuth2Lur;
39 import org.onap.aaf.cadi.oauth.OAuth2Principal;
40 import org.onap.aaf.cadi.oauth.TokenMgr;
41 import org.onap.aaf.cadi.oauth.TokenPerm;
42 import org.onap.aaf.cadi.principal.BearerPrincipal;
44 public class JU_OAuth2Lur {
46 private List<AAFPermission> aafPerms;
47 private List<Permission> perms;
49 @Mock private TokenMgr tmMock;
50 @Mock private AAFPermission pondMock;
51 @Mock private Principal princMock;
52 @Mock private OAuth2Principal oauthPrincMock;
53 @Mock private BearerPrincipal bearPrincMock;
54 @Mock private TokenPerm tpMock;
58 MockitoAnnotations.initMocks(this);
63 OAuth2Lur lur = new OAuth2Lur(tmMock);
64 lur.createPerm("testPerm");
65 lur.createPerm("testPerm1|testPerm2|testPerm3");
67 assertThat(lur.fish(princMock, pondMock), is(false));
68 assertThat(lur.fish(oauthPrincMock, pondMock), is(false));
70 when(oauthPrincMock.tokenPerm()).thenReturn(tpMock);
71 assertThat(lur.fish(oauthPrincMock, pondMock), is(false));
73 aafPerms = new ArrayList<>();
74 aafPerms.add(pondMock);
75 aafPerms.add(pondMock);
76 when(tpMock.perms()).thenReturn(aafPerms);
77 when(pondMock.match(pondMock)).thenReturn(false).thenReturn(true);
78 assertThat(lur.fish(oauthPrincMock, pondMock), is(true));
80 perms = new ArrayList<>();
83 lur.fishAll(oauthPrincMock, perms);
85 when(oauthPrincMock.tokenPerm()).thenReturn(null);
86 lur.fishAll(oauthPrincMock, perms);
88 assertThat(lur.handlesExclusively(pondMock), is(false));
90 assertThat(lur.handles(null), is(false));
91 assertThat(lur.handles(princMock), is(false));
92 assertThat(lur.handles(bearPrincMock), is(false));
93 when(bearPrincMock.getBearer()).thenReturn("not null :)");
94 assertThat(lur.handles(bearPrincMock), is(true));
97 lur.clear(null, null);