853c4ae3735cd3abc4536f6e68e1d4a89abb3a4b
[aaf/authz.git] / cadi / aaf / src / test / java / org / onap / aaf / cadi / oauth / test / JU_OAuth2Lur.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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====================================================
19  *
20  */
21
22 package org.onap.aaf.cadi.oauth.test;
23
24 import static org.mockito.Mockito.when;
25 import static org.hamcrest.CoreMatchers.is;
26 import static org.junit.Assert.assertThat;
27
28 import java.security.Principal;
29 import java.util.ArrayList;
30 import java.util.List;
31
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;
43
44 public class JU_OAuth2Lur {
45         
46         private List<AAFPermission> aafPerms;
47         private List<Permission> perms;
48         
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;
55         
56         @Before
57         public void setup() {
58                 MockitoAnnotations.initMocks(this);
59         }
60
61         @Test
62         public void test() {
63                 OAuth2Lur lur = new OAuth2Lur(tmMock);
64                 lur.createPerm("testPerm");
65                 lur.createPerm("testPerm1|testPerm2|testPerm3");
66
67                 assertThat(lur.fish(princMock, pondMock), is(false));
68                 assertThat(lur.fish(oauthPrincMock, pondMock), is(false));
69                 
70                 when(oauthPrincMock.tokenPerm()).thenReturn(tpMock);
71                 assertThat(lur.fish(oauthPrincMock, pondMock), is(false));
72                 
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));
79
80                 perms = new ArrayList<>();
81                 perms.add(pondMock);
82                 perms.add(pondMock);
83                 lur.fishAll(oauthPrincMock, perms);
84
85                 when(oauthPrincMock.tokenPerm()).thenReturn(null);
86                 lur.fishAll(oauthPrincMock, perms);
87                 
88                 assertThat(lur.handlesExclusively(pondMock), is(false));
89                 
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));
95
96                 lur.destroy();
97                 lur.clear(null, null);
98         }
99
100 }