Sonar fix: Perm.java
[aaf/authz.git] / auth / auth-cass / src / test / java / org / onap / aaf / auth / direct / test / JU_DirectAAFLocator.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.direct.test;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertTrue;
26 import static org.mockito.MockitoAnnotations.initMocks;
27
28 import java.util.ArrayList;
29 import java.util.List;
30
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.mockito.Mock;
35 import org.mockito.Mockito;
36 import org.mockito.runners.MockitoJUnitRunner;
37 import org.onap.aaf.auth.dao.cass.LocateDAO;
38 import org.onap.aaf.auth.dao.cass.LocateDAO.Data;
39 import org.onap.aaf.auth.direct.DirectAAFLocator;
40 import org.onap.aaf.auth.env.AuthzEnv;
41 import org.onap.aaf.auth.env.AuthzTrans;
42 import org.onap.aaf.auth.layer.Result;
43 import org.onap.aaf.cadi.Access;
44 import org.onap.aaf.cadi.LocatorException;
45 import org.onap.aaf.cadi.PropAccess;
46 import org.onap.aaf.cadi.config.Config;
47
48 import junit.framework.Assert;
49
50
51 @RunWith(MockitoJUnitRunner.class) 
52 public class JU_DirectAAFLocator {
53
54         @Mock
55         LocateDAO ldao;
56         
57         @Mock
58         AuthzEnv env;
59         
60         @Mock
61         AuthzTrans trans;
62         
63         @Mock
64         PropAccess access;
65         
66         @Before
67         public void setUp() throws Exception {
68                 initMocks(this);
69         }
70         
71         @Test
72         public void testConstructorVersionExcpetion() {
73                 Mockito.doReturn(access).when(env).access();
74                 Mockito.doReturn("20").when(access).getProperty(Config.CADI_LATITUDE,null);
75                 Mockito.doReturn("20").when(access).getProperty(Config.CADI_LONGITUDE,null);
76                 Mockito.doReturn("20").when(access).getProperty(Config.AAF_LOCATOR_CONTAINER,"");
77                 try {
78                         DirectAAFLocator aafLocatorObj=new DirectAAFLocator(env, ldao,"test","test");
79                 } catch (LocatorException e) {
80 //                      System.out.println(e.getMessage());
81                         assertEquals("Invalid Version String: test", e.getMessage());
82                 }
83                 try {
84                         DirectAAFLocator aafLocatorObj=new DirectAAFLocator(env, ldao,"test","20.test");
85                 } catch (LocatorException e) {
86 //                      System.out.println(e.getMessage());
87                         assertEquals("Invalid Version String: 20.test", e.getMessage());
88                 }try {
89                         DirectAAFLocator aafLocatorObj=new DirectAAFLocator(env, ldao,"test","20.20.test");
90                 } catch (LocatorException e) {
91 //                      System.out.println(e.getMessage());
92                         assertEquals("Invalid Version String: 20.20.test", e.getMessage());
93                 }try {
94                         DirectAAFLocator aafLocatorObj=new DirectAAFLocator(env, ldao,"test","20.20.20.test");
95                 } catch (LocatorException e) {
96 //                      System.out.println(e.getMessage());
97                         assertEquals("Invalid Version String: 20.20.20.test", e.getMessage());
98                 }
99         }
100         
101         @Test
102         public void testConstructorExcpetion() {
103                 Mockito.doReturn(access).when(env).access();
104                 Mockito.doReturn("20").when(access).getProperty(Config.CADI_LATITUDE,null);
105                 Mockito.doReturn("20").when(access).getProperty(Config.CADI_LONGITUDE,null);
106                 Mockito.doReturn("20").when(access).getProperty(Config.AAF_LOCATOR_CONTAINER,"");
107                 Mockito.doReturn("20").when(access).getProperty(Config.AAF_LOCATOR_APP_NS,"AAF_NS");
108                 try {
109                         DirectAAFLocator aafLocatorObj=new DirectAAFLocator(env, ldao,"test",null);
110                 } catch (LocatorException e) {
111 //                      System.out.println(e.getMessage());
112                 }
113         }
114         
115         // NOTE: These mocks to not well represent the DirectAAFLocator Class.
116         @Test
117         public void testConstructorUriExcpetion() {
118                 Mockito.doReturn(access).when(env).access();
119                 Mockito.doReturn("20").when(access).getProperty(Config.CADI_LATITUDE,null);
120                 Mockito.doReturn("20").when(access).getProperty(Config.CADI_LONGITUDE,null);
121                 Mockito.doReturn("20").when(access).getProperty(Config.AAF_LOCATOR_CONTAINER,"");
122                 Mockito.doReturn("20 30").when(access).getProperty(Config.AAF_URL,null);
123                 try {
124                         DirectAAFLocator aafLocatorObj=new DirectAAFLocator(env, ldao,"test","192.0.0.1");
125                 } catch (LocatorException e) {
126 //                      System.out.println(e.getMessage());
127                         assertTrue(e.getMessage().contains("Illegal character in path at index 2:"));
128                 }
129         }
130
131
132         @Test
133         public void testRefresh() {
134                 
135                 DirectAAFLocator aafLocatorObj=null;
136                 PropAccess access = Mockito.mock(PropAccess.class);
137                 Mockito.doReturn(access).when(env).access();
138                 Mockito.doReturn(trans).when(env).newTransNoAvg();
139                 Mockito.doReturn("20").when(access).getProperty(Config.CADI_LATITUDE,null);
140                 Mockito.doReturn("20").when(access).getProperty(Config.CADI_LONGITUDE,null);
141                 Mockito.doReturn("20").when(access).getProperty(Config.AAF_LOCATOR_CONTAINER,"");
142                 Mockito.doReturn("http://aafurl.com").when(access).getProperty(Config.AAF_URL,null);
143                 try {
144                         aafLocatorObj = new DirectAAFLocator(env, ldao,"test","30.20.30.30");
145                 } catch (LocatorException e) {
146                         // TODO Auto-generated catch block
147                         e.printStackTrace();
148                 }
149                 Result<List<Data>> retVal1 = new Result<List<Data>>(null,0,"",new Object[0]);
150                 
151                 Data data= new Data();
152                 data.major=30;
153                 data.minor=30;
154                 data.patch=30;
155                 data.pkg=30;
156                 retVal1.value = new ArrayList<Data>();
157                 retVal1.value.add(data);
158                 
159                 Mockito.doReturn(retVal1).when(ldao).readByName(trans,"test");
160                 boolean retVal = aafLocatorObj.refresh();
161 //              System.out.println(retVal);
162                 assertTrue(retVal);
163         }       
164         
165         @Test
166         public void testRefreshNOK() {
167                 
168                 DirectAAFLocator aafLocatorObj=null;
169                 PropAccess access = Mockito.mock(PropAccess.class);
170                 Mockito.doReturn(access).when(env).access();
171                 Mockito.doReturn(trans).when(env).newTransNoAvg();
172                 Mockito.doReturn("20").when(access).getProperty(Config.CADI_LATITUDE,null);
173                 Mockito.doReturn("20").when(access).getProperty(Config.CADI_LONGITUDE,null);
174                 Mockito.doReturn("20").when(access).getProperty(Config.AAF_LOCATOR_CONTAINER,"");
175                 Mockito.doReturn("http://aafurl.com").when(access).getProperty(Config.AAF_URL,null);
176                 try {
177                         aafLocatorObj = new DirectAAFLocator(env, ldao,"test","30.20.30.30");
178                 } catch (LocatorException e) {
179                         // TODO Auto-generated catch block
180                         e.printStackTrace();
181                 }
182                 Result<List<Data>> retVal1 = new Result<List<Data>>(null,1,"",new String[0]);
183                 
184                 Mockito.doReturn(retVal1).when(ldao).readByName(trans,"test");
185                 boolean retVal = aafLocatorObj.refresh();
186 //              System.out.println(retVal);
187                 assertFalse(retVal);
188         }       
189         
190 }