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