Sonar Fixes, Formatting
[aaf/authz.git] / auth / auth-cmd / src / test / java / org / onap / aaf / auth / cmd / test / perm / JU_Create.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 java.io.ByteArrayOutputStream;
29 import java.io.PrintStream;
30 import java.io.Writer;
31 import java.net.HttpURLConnection;
32 import java.net.URI;
33 import java.net.URISyntaxException;
34
35 import org.junit.Before;
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.cmd.ns.Create;
43 import org.onap.aaf.auth.cmd.ns.NS;
44 import org.onap.aaf.auth.cmd.test.HMangrStub;
45 import org.onap.aaf.auth.env.AuthzEnv;
46 import org.onap.aaf.cadi.CadiException;
47 import org.onap.aaf.cadi.Locator;
48 import org.onap.aaf.cadi.LocatorException;
49 import org.onap.aaf.cadi.PropAccess;
50 import org.onap.aaf.cadi.SecuritySetter;
51 import org.onap.aaf.cadi.client.Future;
52 import org.onap.aaf.cadi.client.Rcli;
53 import org.onap.aaf.misc.env.APIException;
54
55 @RunWith(MockitoJUnitRunner.class)
56 public class JU_Create {
57
58     @Mock private SecuritySetter<HttpURLConnection> ssMock;
59     @Mock private Locator<URI> locMock;
60     @Mock private Writer wrtMock;
61     @Mock private Rcli<HttpURLConnection> clientMock;
62     @Mock private Future<String> futureMock;
63
64     private static Create create;
65
66     private NS ns;
67     private PropAccess access;
68     private HMangrStub hman;
69     private AuthzEnv aEnv;
70     private AAFcli aafcli;
71
72     @Before
73     public void setUp () throws NoSuchFieldException, SecurityException, Exception, IllegalAccessException {
74         MockitoAnnotations.initMocks(this);
75
76         when(clientMock.create(any(), any(), any(String.class))).thenReturn(futureMock);
77         when(clientMock.delete(any(), any(), any(String.class))).thenReturn(futureMock);
78         when(clientMock.update(any(), any(), any(String.class))).thenReturn(futureMock);
79
80         hman = new HMangrStub(access, locMock, clientMock);
81         access = new PropAccess(new PrintStream(new ByteArrayOutputStream()), new String[0]);
82         aEnv = new AuthzEnv();
83         aafcli = new AAFcli(access, aEnv, wrtMock, hman, null, ssMock);
84
85         ns = new NS(aafcli);
86
87         create = new Create(ns);
88     }
89
90     @Test
91     public void testError() throws APIException, LocatorException, CadiException, URISyntaxException {
92         create._exec(0, new String[] {"grant","ungrant","setTo","grant","ungrant","setTo"});
93         create._exec(4, new String[] {"grant","ungrant","setTo","grant","ungrant","setTo"});
94     }
95
96     @Test
97     public void testSuccess1() throws APIException, LocatorException, CadiException, URISyntaxException {
98         when(futureMock.code()).thenReturn(202);
99         create._exec(0, new String[] {"grant","ungrant","setTo","grant","ungrant","setTo"});
100     }
101
102     @Test
103     public void testSuccess2() throws APIException, LocatorException, CadiException, URISyntaxException {
104         when(futureMock.get(any(Integer.class))).thenReturn(true);
105         create._exec(0, new String[] {"grant","ungrant","setTo","grant","ungrant","setTo"});
106     }
107
108     @Test
109     public void testDetailedHelp() {
110         StringBuilder sb = new StringBuilder();
111         create.detailedHelp(0, sb);
112     }
113
114 }