f6d2a593daf6252f4bc5d67d475cdba974f9df28
[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("").when(access).getProperty(Config.AAF_LOCATOR_CONTAINER,"");
107                 Mockito.doReturn("").when(access).getProperty(Config.AAF_LOCATOR_CONTAINER_NS,"");
108                 Mockito.doReturn("20").when(access).getProperty(Config.AAF_LOCATOR_APP_NS,"AAF_NS");
109                 try {
110                         DirectAAFLocator aafLocatorObj=new DirectAAFLocator(env, ldao,"test",null);
111                 } catch (LocatorException e) {
112 //                      System.out.println(e.getMessage());
113                 }
114         }
115         
116         // NOTE: These mocks to not well represent the DirectAAFLocator Class.
117         @Test
118         public void testConstructorUriExcpetion() {
119                 Mockito.doReturn(access).when(env).access();
120                 Mockito.doReturn("20").when(access).getProperty(Config.CADI_LATITUDE,null);
121                 Mockito.doReturn("20").when(access).getProperty(Config.CADI_LONGITUDE,null);
122                 Mockito.doReturn("").when(access).getProperty(Config.AAF_LOCATOR_CONTAINER,"");
123                 Mockito.doReturn("").when(access).getProperty(Config.AAF_LOCATOR_CONTAINER_NS,"");
124                 Mockito.doReturn("20 30").when(access).getProperty(Config.AAF_URL,null);
125                 try {
126                         DirectAAFLocator aafLocatorObj=new DirectAAFLocator(env, ldao,"test","192.0.0.1");
127                 } catch (LocatorException e) {
128 //                      System.out.println(e.getMessage());
129                         assertTrue(e.getMessage().contains("Illegal character in path at index 2:"));
130                 }
131         }
132
133
134         @Test
135         public void testRefresh() {
136                 
137                 DirectAAFLocator aafLocatorObj=null;
138                 PropAccess access = Mockito.mock(PropAccess.class);
139                 Mockito.doReturn(access).when(env).access();
140                 Mockito.doReturn(trans).when(env).newTransNoAvg();
141                 Mockito.doReturn("20").when(access).getProperty(Config.CADI_LATITUDE,null);
142                 Mockito.doReturn("20").when(access).getProperty(Config.CADI_LONGITUDE,null);
143                 Mockito.doReturn("").when(access).getProperty(Config.AAF_LOCATOR_CONTAINER,"");
144                 Mockito.doReturn("").when(access).getProperty(Config.AAF_LOCATOR_CONTAINER_NS,"");
145                 Mockito.doReturn("http://aafurl.com").when(access).getProperty(Config.AAF_URL,null);
146                 try {
147                         aafLocatorObj = new DirectAAFLocator(env, ldao,"test","30.20.30.30");
148                 } catch (LocatorException e) {
149                         // TODO Auto-generated catch block
150                         e.printStackTrace();
151                 }
152                 Result<List<Data>> retVal1 = new Result<List<Data>>(null,0,"",new Object[0]);
153                 
154                 Data data= new Data();
155                 data.major=30;
156                 data.minor=30;
157                 data.patch=30;
158                 data.pkg=30;
159                 retVal1.value = new ArrayList<Data>();
160                 retVal1.value.add(data);
161                 
162                 Mockito.doReturn(retVal1).when(ldao).readByName(trans,"test");
163                 boolean retVal = aafLocatorObj.refresh();
164 //              System.out.println(retVal);
165                 assertTrue(retVal);
166         }       
167         
168         @Test
169         public void testRefreshNOK() {
170                 
171                 DirectAAFLocator aafLocatorObj=null;
172                 PropAccess access = Mockito.mock(PropAccess.class);
173                 Mockito.doReturn(access).when(env).access();
174                 Mockito.doReturn(trans).when(env).newTransNoAvg();
175                 Mockito.doReturn("20").when(access).getProperty(Config.CADI_LATITUDE,null);
176                 Mockito.doReturn("20").when(access).getProperty(Config.CADI_LONGITUDE,null);
177                 Mockito.doReturn("").when(access).getProperty(Config.AAF_LOCATOR_CONTAINER,"");
178                 Mockito.doReturn("").when(access).getProperty(Config.AAF_LOCATOR_CONTAINER_NS,"");
179                 Mockito.doReturn("http://aafurl.com").when(access).getProperty(Config.AAF_URL,null);
180                 try {
181                         aafLocatorObj = new DirectAAFLocator(env, ldao,"test","30.20.30.30");
182                 } catch (LocatorException e) {
183                         // TODO Auto-generated catch block
184                         e.printStackTrace();
185                 }
186                 Result<List<Data>> retVal1 = new Result<List<Data>>(null,1,"",new String[0]);
187                 
188                 Mockito.doReturn(retVal1).when(ldao).readByName(trans,"test");
189                 boolean retVal = aafLocatorObj.refresh();
190 //              System.out.println(retVal);
191                 assertFalse(retVal);
192         }       
193         
194 }