975b83d2f79cbde53a35b4eb715f0849a36156ac
[aaf/authz.git] / auth / auth-cmd / src / test / java / org / onap / aaf / auth / cmd / test / perm / JU_Grant.java
1 /*******************************************************************************
2  * ============LICENSE_START====================================================
3  * * org.onap.aaf
4  * * ===========================================================================
5  * * Copyright © 2017 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.auth.cmd.test.perm;
23
24 import static org.mockito.Matchers.any;
25 import static org.mockito.Mockito.when;
26
27 import org.junit.Before;
28
29 import java.io.ByteArrayOutputStream;
30 import java.io.PrintStream;
31 import java.io.Writer;
32 import java.net.HttpURLConnection;
33 import java.net.URI;
34 import java.net.URISyntaxException;
35
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.mockito.Mock;
39 import org.mockito.MockitoAnnotations;
40 import org.mockito.runners.MockitoJUnitRunner;
41 import org.onap.aaf.auth.cmd.AAFcli;
42 import org.onap.aaf.auth.env.AuthzEnv;
43 import org.onap.aaf.cadi.CadiException;
44 import org.onap.aaf.cadi.Locator;
45 import org.onap.aaf.cadi.LocatorException;
46 import org.onap.aaf.cadi.PropAccess;
47 import org.onap.aaf.cadi.SecuritySetter;
48 import org.onap.aaf.cadi.client.Future;
49 import org.onap.aaf.cadi.client.Rcli;
50 import org.onap.aaf.misc.env.APIException;
51
52 import org.onap.aaf.auth.cmd.perm.Grant;
53 import org.onap.aaf.auth.cmd.perm.Perm;
54 import org.onap.aaf.auth.cmd.role.Role;
55 import org.onap.aaf.auth.cmd.test.HMangrStub;
56
57 @RunWith(MockitoJUnitRunner.class)
58 public class JU_Grant {
59         
60         private static Grant grant;
61
62         @Mock private SecuritySetter<HttpURLConnection> ssMock;
63         @Mock private Locator<URI> locMock;
64         @Mock private Writer wrtMock;
65         @Mock private Rcli<HttpURLConnection> clientMock;
66         @Mock private Future<String> futureMock;
67
68         private PropAccess access;
69         private HMangrStub hman;        
70         private AuthzEnv aEnv;
71         private AAFcli aafcli;
72         
73         @Before
74         public void setUp () throws NoSuchFieldException, SecurityException, Exception, IllegalAccessException {
75                 MockitoAnnotations.initMocks(this);
76
77                 when(clientMock.create(any(), any(), any(String.class))).thenReturn(futureMock);
78                 when(clientMock.delete(any(), any(), any(String.class))).thenReturn(futureMock);
79                 when(clientMock.update(any(), any(), any(String.class))).thenReturn(futureMock);
80
81                 hman = new HMangrStub(access, locMock, clientMock);
82                 access = new PropAccess(new PrintStream(new ByteArrayOutputStream()), new String[0]);
83                 aEnv = new AuthzEnv();
84                 aafcli = new AAFcli(access, aEnv, wrtMock, hman, null, ssMock);
85
86                 Role role = new Role(aafcli);
87                 Perm perm = new Perm(role);
88
89                 grant = new Grant(perm);
90         }
91
92         @Test
93         public void testExecError() throws APIException, LocatorException, CadiException, URISyntaxException {
94                 grant._exec(0, new String[] {"grant","ungrant","setTo","grant","ungrant","setTo"});
95         }
96         
97         @Test
98         public void testExecSuccess1() throws APIException, LocatorException, CadiException, URISyntaxException {
99                 when(futureMock.code()).thenReturn(202);
100                 grant._exec(0, new String[] {"grant","ungrant","setTo","grant","ungrant","setTo"});
101                 grant._exec(1, new String[] {"grant","ungrant","setTo","grant","ungrant","setTo"});
102         }
103         
104         @Test
105         public void testExecSuccess2() throws APIException, LocatorException, CadiException, URISyntaxException {
106                 when(futureMock.get(any(Integer.class))).thenReturn(true);
107                 grant._exec(0, new String[] {"grant","ungrant","setTo","grant","ungrant","setTo"});
108         }
109         
110         @Test
111         public void testExecSetToError() throws APIException, LocatorException, CadiException, URISyntaxException {
112                 grant._exec(2, new String[] {"grant","ungrant","setTo","grant","ungrant","setTo"});
113         }
114         
115         @Test
116         public void testExecSetToSuccess1() throws APIException, LocatorException, CadiException, URISyntaxException {
117                 when(futureMock.get(any(Integer.class))).thenReturn(true);
118                 grant._exec(2, new String[] {"grant","ungrant","setTo","grant","ungrant","setTo"});
119         }
120         
121         @Test
122         public void testExecSetToSuccess2() throws APIException, LocatorException, CadiException, URISyntaxException {
123                 grant._exec(2, new String[] {"grant","ungrant","setTo","grant","ungrant","setTo","another"});
124         }
125         
126         @Test
127         public void testDetailedHelp() {
128                 StringBuilder sb = new StringBuilder();
129                 grant.detailedHelp(0, sb);
130         }
131 }