cf8505878be3046e43630e223184986257db421e
[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
49 @RunWith(MockitoJUnitRunner.class) 
50 public class JU_DirectAAFLocator {
51
52         @Mock
53         LocateDAO ldao;
54         
55         @Mock
56         AuthzEnv env;
57         
58         @Mock
59         AuthzTrans trans;
60         
61         @Mock
62         Access access;
63         
64         @Before
65         public void setUp() throws Exception {
66                 initMocks(this);
67         }
68         
69         @Test
70         public void testConstructorExcpetion() {
71
72                 PropAccess access = Mockito.mock(PropAccess.class);
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                 try {
77                         DirectAAFLocator aafLocatorObj=new DirectAAFLocator(env, ldao,"test","test");
78                 } catch (LocatorException e) {
79 //                      System.out.println(e.getMessage());
80                         assertEquals("Invalid Version String: test", e.getMessage());
81                 }
82         }
83         
84         @Test
85         public void testConstructorUriExcpetion() {
86
87                 PropAccess access = Mockito.mock(PropAccess.class);
88                 Mockito.doReturn(access).when(env).access();
89                 Mockito.doReturn("20").when(access).getProperty(Config.CADI_LATITUDE,null);
90                 Mockito.doReturn("20").when(access).getProperty(Config.CADI_LONGITUDE,null);
91                 try {
92                         DirectAAFLocator aafLocatorObj=new DirectAAFLocator(env, ldao," test","3.2");
93                 } catch (LocatorException e) {
94 //                      System.out.println(e.getMessage());
95                         assertTrue(e.getMessage().contains("Illegal character in path at index"));
96                 }
97         }
98         @Test
99         public void testRefresh() {
100                 
101                 DirectAAFLocator aafLocatorObj=null;
102                 PropAccess access = Mockito.mock(PropAccess.class);
103                 Mockito.doReturn(access).when(env).access();
104                 Mockito.doReturn(trans).when(env).newTransNoAvg();
105                 Mockito.doReturn("20").when(access).getProperty(Config.CADI_LATITUDE,null);
106                 Mockito.doReturn("20").when(access).getProperty(Config.CADI_LONGITUDE,null);
107                 try {
108                         aafLocatorObj = new DirectAAFLocator(env, ldao,"test","30.20.30.30");
109                 } catch (LocatorException e) {
110                         // TODO Auto-generated catch block
111                         e.printStackTrace();
112                 }
113                 Result<List<Data>> retVal1 = new Result<List<Data>>(null,0,"",new String[0]);
114                 
115                 Data data= new Data();
116                 data.major=30;
117                 data.minor=30;
118                 data.patch=30;
119                 data.pkg=30;
120                 retVal1.value = new ArrayList<Data>();
121                 retVal1.value.add(data);
122                 
123                 Mockito.doReturn(retVal1).when(ldao).readByName(trans,"test");
124                 boolean retVal = aafLocatorObj.refresh();
125 //              System.out.println(retVal);
126                 assertTrue(retVal);
127         }       
128         
129         @Test
130         public void testRefreshNOK() {
131                 
132                 DirectAAFLocator aafLocatorObj=null;
133                 PropAccess access = Mockito.mock(PropAccess.class);
134                 Mockito.doReturn(access).when(env).access();
135                 Mockito.doReturn(trans).when(env).newTransNoAvg();
136                 Mockito.doReturn("20").when(access).getProperty(Config.CADI_LATITUDE,null);
137                 Mockito.doReturn("20").when(access).getProperty(Config.CADI_LONGITUDE,null);
138                 try {
139                         aafLocatorObj = new DirectAAFLocator(env, ldao,"test","30.20.30.30");
140                 } catch (LocatorException e) {
141                         // TODO Auto-generated catch block
142                         e.printStackTrace();
143                 }
144                 Result<List<Data>> retVal1 = new Result<List<Data>>(null,1,"",new String[0]);
145                 
146                 Mockito.doReturn(retVal1).when(ldao).readByName(trans,"test");
147                 boolean retVal = aafLocatorObj.refresh();
148 //              System.out.println(retVal);
149                 assertFalse(retVal);
150         }       
151         
152 }