Merge "DOC Add Architecture CADI"
[aaf/authz.git] / auth / auth-cass / src / test / java / org / onap / aaf / auth / dao / JU_CassExecutor.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 package org.onap.aaf.auth.dao.hl;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.mockito.MockitoAnnotations.initMocks;
26
27 import java.util.Properties;
28
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.mockito.Mock;
33 import org.mockito.Mockito;
34 import org.mockito.invocation.InvocationOnMock;
35 import org.mockito.runners.MockitoJUnitRunner;
36 import org.mockito.stubbing.Answer;
37 import org.onap.aaf.auth.common.Define;
38 import org.onap.aaf.auth.dao.cass.NsSplit;
39 import org.onap.aaf.auth.dao.cass.UserRoleDAO.Data;
40 import org.onap.aaf.auth.env.AuthzTransImpl;
41 import org.onap.aaf.auth.layer.Result;
42 import org.onap.aaf.cadi.Access;
43 import org.onap.aaf.cadi.CadiException;
44
45
46 @RunWith(MockitoJUnitRunner.class) 
47 public class JU_CassExecutor {
48
49         
50         
51         @Mock
52         AuthzTransImpl trans;
53         
54         @Mock
55         Question q;
56         
57         @Mock
58         Access access;
59         
60         Function f;
61         
62         @Before
63         public void setUp() throws Exception {
64                 initMocks(this);
65                 try {
66                         Mockito.doReturn("0.0").when(access).getProperty("aaf_root_ns","org.osaaf.aaf");
67                         Mockito.doReturn(new Properties()).when(access).getProperties();
68                         Define.set(access);
69                 } catch (CadiException e) {
70                         // TODO Auto-generated catch block
71                         e.printStackTrace();
72                 }
73                 f =new Function(trans, q);
74         }
75         
76         @Test
77         public void testHasPermission() {
78                 
79                 CassExecutor cassExecutorObj =new CassExecutor(trans, f);
80                 Mockito.doReturn(false).when(q).isGranted(trans, "","","","","");
81                 boolean retVal = cassExecutorObj.hasPermission("", "", "", "", "");
82 //              System.out.println(retVal);
83                 assertFalse(retVal);
84         }       
85         
86         @Test
87         public void testInRole() {
88                 
89                 CassExecutor cassExecutorObj =new CassExecutor(trans, f);
90                 Result<NsSplit> retVal1 = new Result<NsSplit>(null,1,"",new String[0]);
91                 Mockito.doReturn(retVal1).when(q).deriveNsSplit(trans, "test");
92                 
93                 boolean retVal = cassExecutorObj.inRole("test");
94 //              System.out.println(retVal);
95                 assertFalse(retVal);
96         }
97         
98         @Test
99         public void testNamespace() {
100                 f =new Function(trans, q);
101                 CassExecutor cassExecutorObj =new CassExecutor(trans, f);
102                 Result<Data> retVal1 = new Result<Data>(null,1,"",new String[0]);
103                 Mockito.doReturn(retVal1).when(q).validNSOfDomain(trans, null);
104                 
105                 String retVal="";
106                 try {
107                         retVal = cassExecutorObj.namespace();
108                 } catch (Exception e) {
109                         System.out.println(e.getMessage());
110                         assertEquals("33", e.getMessage());
111                 }
112                 System.out.println(retVal);
113 //              assertFalse(retVal);
114         }
115         
116         @Test
117         public void testId() {
118                 Mockito.doReturn("").when(trans).user();
119                 CassExecutor cassExecutorObj =new CassExecutor(trans, f);
120                 String retVal = cassExecutorObj.id();
121                 assertEquals("", retVal);
122         }
123         
124         @Test
125         public void testNamespaceSuccess() {
126                 Mockito.doAnswer(new Answer() {
127                     private int count = 0;
128
129                     public Object answer(InvocationOnMock invocation) {
130                         if (count++ == 1)
131                             return "test@test.com";
132
133                         return null;
134                     }
135                 }).when(trans).user();
136                 f =new Function(trans, q);
137                 CassExecutor cassExecutorObj =new CassExecutor(trans, f);
138                 Result<Data> retVal1 = new Result<Data>(null,0,"",new String[0]);
139                 Mockito.doReturn(retVal1).when(q).validNSOfDomain(trans, null);
140                 
141                 
142                 String retVal="";
143                 try {
144                         retVal = cassExecutorObj.namespace();
145                 } catch (Exception e) {
146                         e.printStackTrace();
147                         System.out.println(e.getMessage());
148 //                      assertNull( e.getMessage());
149                 }
150 //              System.out.println(retVal);
151 //              assertFalse(retVal);
152         }
153         
154 }