Refine Agent to create CADI Configs
[aaf/authz.git] / auth / auth-locate / src / test / java / org / onap / aaf / auth / locate / validation / JU_LocateValidatorTest.java
1 /**\r
2  * ============LICENSE_START====================================================\r
3  * org.onap.aaf\r
4  * ===========================================================================\r
5  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.\r
6  * ===========================================================================\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  * \r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * \r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  * ============LICENSE_END====================================================\r
19  *\r
20  */\r
21 package org.onap.aaf.auth.locate.validation;\r
22 \r
23 import static org.junit.Assert.assertEquals;\r
24 import static org.mockito.Mockito.when;\r
25 \r
26 import java.util.ArrayList;\r
27 import java.util.List;\r
28 \r
29 import org.junit.Before;\r
30 import org.junit.Test;\r
31 import org.mockito.Answers;\r
32 import org.mockito.Mock;\r
33 import org.mockito.MockitoAnnotations;\r
34 \r
35 import locate.v1_0.Endpoint;\r
36 import locate.v1_0.Endpoints;\r
37 import locate.v1_0.MgmtEndpoint;\r
38 import locate.v1_0.MgmtEndpoint.SpecialPorts;\r
39 import locate.v1_0.MgmtEndpoints;\r
40 \r
41 public class JU_LocateValidatorTest {\r
42 \r
43     @Mock\r
44     private Endpoint endpoint;\r
45 \r
46     @Mock(answer = Answers.RETURNS_DEEP_STUBS)\r
47     private Endpoints endpoints;\r
48     @Mock(answer = Answers.RETURNS_DEEP_STUBS)\r
49     private MgmtEndpoints me;\r
50     @Mock(answer = Answers.RETURNS_DEEP_STUBS)\r
51     private MgmtEndpoint mgmtEndpoint;\r
52     @Mock(answer = Answers.RETURNS_DEEP_STUBS)\r
53     private SpecialPorts specialPort;\r
54 \r
55     @Before\r
56     public void setup() {\r
57         MockitoAnnotations.initMocks(this);\r
58     }\r
59 \r
60     @Test\r
61     public void testNullEndPoint() {\r
62         LocateValidator validator = new LocateValidator();\r
63 \r
64         validator.endpoint(null);\r
65         assertEquals("Endpoint Data is null.\n", validator.errs());\r
66     }\r
67 \r
68     @Test\r
69     public void testEndPoint() {\r
70         LocateValidator validator = new LocateValidator();\r
71 \r
72         when(endpoint.getName()).thenReturn("Endpoint1");\r
73         when(endpoint.getHostname()).thenReturn("HOST1");\r
74         when(endpoint.getPort()).thenReturn(9090);\r
75         when(endpoint.getProtocol()).thenReturn("HTTP");\r
76 \r
77         validator.endpoint(endpoint);\r
78 \r
79         assertEquals("Endpoint Name must prefixed by Namespace\n", validator.errs());\r
80     }\r
81 \r
82     @Test\r
83     public void testSubProtoCol() {\r
84         LocateValidator validator = new LocateValidator();\r
85 \r
86         List<String> subProtocol = new ArrayList<String>();\r
87         subProtocol.add(null);\r
88 \r
89         when(endpoint.getName()).thenReturn("EndPoint.Endpoint1");\r
90         when(endpoint.getHostname()).thenReturn("HOST1");\r
91         when(endpoint.getPort()).thenReturn(9090);\r
92         when(endpoint.getProtocol()).thenReturn("HTTP");\r
93         when(endpoint.getSubprotocol()).thenReturn(subProtocol);\r
94 \r
95         validator.endpoint(endpoint);\r
96 \r
97         assertEquals("Endpoint Subprotocol is null.\n", validator.errs());\r
98     }\r
99 \r
100     @Test\r
101     public void testNullEndpoints() {\r
102         LocateValidator validator = new LocateValidator();\r
103 \r
104         validator.endpoints(null, false);\r
105         validator.mgmt_endpoint_key(null);\r
106         validator.mgmt_endpoints(null, false);\r
107         assertEquals("Endpoints Data is null.\n" + "MgmtEndpoints Data is null.\n" + "MgmtEndpoints Data is null.\n",\r
108                 validator.errs());\r
109     }\r
110 \r
111     @Test\r
112     public void testEndpointsWithListContaingNull() {\r
113         LocateValidator validator = new LocateValidator();\r
114         when(endpoints.getEndpoint().size()).thenReturn(0);\r
115         when(me.getMgmtEndpoint().size()).thenReturn(0);\r
116 \r
117         validator.endpoints(endpoints, true);\r
118         validator.mgmt_endpoints(me, false);\r
119         assertEquals("Endpoints contains no endpoints\n" + "MgmtEndpoints contains no data\n", validator.errs());\r
120     }\r
121 \r
122     @Test\r
123     public void testEndpointsWithSpecialPortsNull() {\r
124         LocateValidator validator = new LocateValidator();\r
125 \r
126         when(endpoint.getName()).thenReturn("EndPoint.Endpoint1");\r
127         when(endpoint.getHostname()).thenReturn("HOST1");\r
128         when(endpoint.getPort()).thenReturn(9090);\r
129         when(endpoint.getProtocol()).thenReturn("HTTP");\r
130         List<String> subprotocol = new ArrayList<String>();\r
131         when(endpoint.getSubprotocol()).thenReturn(subprotocol);\r
132 \r
133         List<Endpoint> endpointList = new ArrayList<Endpoint>();\r
134         endpointList.add(endpoint);\r
135 \r
136         when(mgmtEndpoint.getName()).thenReturn("EndPoint.Endpoint1");\r
137         when(mgmtEndpoint.getHostname()).thenReturn("HOST1");\r
138         when(mgmtEndpoint.getPort()).thenReturn(9090);\r
139         when(mgmtEndpoint.getProtocol()).thenReturn("HTTP");\r
140         List<SpecialPorts> specialPorts = new ArrayList<SpecialPorts>();\r
141         specialPorts.add(null);\r
142         when(mgmtEndpoint.getSpecialPorts()).thenReturn(specialPorts);\r
143         List<MgmtEndpoint> mgmtEndpoints = new ArrayList<MgmtEndpoint>();\r
144         mgmtEndpoints.add(mgmtEndpoint);\r
145 \r
146         when(endpoints.getEndpoint()).thenReturn(endpointList);\r
147         when(me.getMgmtEndpoint()).thenReturn(mgmtEndpoints);\r
148 \r
149         validator.endpoints(endpoints, false);\r
150         validator.mgmt_endpoints(me, true);\r
151         assertEquals("Special Ports is null.\n", validator.errs());\r
152     }\r
153 \r
154     @Test\r
155     public void testEndpointsWithSpecialPorts() {\r
156         LocateValidator validator = new LocateValidator();\r
157 \r
158         when(mgmtEndpoint.getName()).thenReturn("EndPoint.Endpoint1");\r
159         when(mgmtEndpoint.getHostname()).thenReturn("HOST1");\r
160         when(mgmtEndpoint.getPort()).thenReturn(9090);\r
161         when(mgmtEndpoint.getProtocol()).thenReturn("HTTP");\r
162 \r
163         List<SpecialPorts> specialPorts = new ArrayList<SpecialPorts>();\r
164         specialPorts.add(specialPort);\r
165 \r
166         when(specialPort.getName()).thenReturn("Port1");\r
167         when(specialPort.getProtocol()).thenReturn("HTTP");\r
168         when(specialPort.getPort()).thenReturn(9090);\r
169 \r
170         List<String> versions = new ArrayList<String>();\r
171         versions.add("1");\r
172 \r
173         when(specialPort.getProtocolVersions()).thenReturn(versions);\r
174 \r
175         when(mgmtEndpoint.getSpecialPorts()).thenReturn(specialPorts);\r
176         List<MgmtEndpoint> mgmtEndpoints = new ArrayList<MgmtEndpoint>();\r
177         mgmtEndpoints.add(mgmtEndpoint);\r
178 \r
179         when(me.getMgmtEndpoint()).thenReturn(mgmtEndpoints);\r
180 \r
181         validator.endpoints(endpoints, false);\r
182         validator.mgmt_endpoints(me, true);\r
183         validator.mgmt_endpoint_key(me);\r
184         assertEquals(false, validator.err());\r
185 \r
186     }\r
187 }\r