JUnits for Mapper_2_0
[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 \r
22 package org.onap.aaf.auth.locate.validation;\r
23 \r
24 import static org.junit.Assert.assertEquals;\r
25 import static org.mockito.Mockito.when;\r
26 \r
27 import java.util.ArrayList;\r
28 import java.util.List;\r
29 \r
30 import org.junit.Before;\r
31 import org.junit.Test;\r
32 import org.mockito.Answers;\r
33 import org.mockito.Mock;\r
34 import org.mockito.MockitoAnnotations;\r
35 \r
36 import locate.v1_0.Endpoint;\r
37 import locate.v1_0.Endpoints;\r
38 import locate.v1_0.MgmtEndpoint;\r
39 import locate.v1_0.MgmtEndpoint.SpecialPorts;\r
40 import locate.v1_0.MgmtEndpoints;\r
41 \r
42 public class JU_LocateValidatorTest {\r
43 \r
44     @Mock\r
45     private Endpoint endpoint;\r
46 \r
47     @Mock(answer = Answers.RETURNS_DEEP_STUBS)\r
48     private Endpoints endpoints;\r
49     @Mock(answer = Answers.RETURNS_DEEP_STUBS)\r
50     private MgmtEndpoints me;\r
51     @Mock(answer = Answers.RETURNS_DEEP_STUBS)\r
52     private MgmtEndpoint mgmtEndpoint;\r
53     @Mock(answer = Answers.RETURNS_DEEP_STUBS)\r
54     private SpecialPorts specialPort;\r
55 \r
56     @Before\r
57     public void setup() {\r
58         MockitoAnnotations.initMocks(this);\r
59     }\r
60 \r
61     @Test\r
62     public void testNullEndPoint() {\r
63         LocateValidator validator = new LocateValidator();\r
64 \r
65         validator.endpoint(null);\r
66         assertEquals("Endpoint Data is null.\n", validator.errs());\r
67     }\r
68 \r
69     @Test\r
70     public void testEndPoint() {\r
71         LocateValidator validator = new LocateValidator();\r
72 \r
73         when(endpoint.getName()).thenReturn("Endpoint1");\r
74         when(endpoint.getHostname()).thenReturn("HOST1");\r
75         when(endpoint.getPort()).thenReturn(9090);\r
76         when(endpoint.getProtocol()).thenReturn("HTTP");\r
77 \r
78         validator.endpoint(endpoint);\r
79 \r
80         assertEquals("Endpoint Name must prefixed by Namespace\n", validator.errs());\r
81     }\r
82 \r
83     @Test\r
84     public void testSubProtoCol() {\r
85         LocateValidator validator = new LocateValidator();\r
86 \r
87         List<String> subProtocol = new ArrayList<>();\r
88         subProtocol.add(null);\r
89 \r
90         when(endpoint.getName()).thenReturn("EndPoint.Endpoint1");\r
91         when(endpoint.getHostname()).thenReturn("HOST1");\r
92         when(endpoint.getPort()).thenReturn(9090);\r
93         when(endpoint.getProtocol()).thenReturn("HTTP");\r
94         when(endpoint.getSubprotocol()).thenReturn(subProtocol);\r
95 \r
96         validator.endpoint(endpoint);\r
97 \r
98         assertEquals("Endpoint Subprotocol is null.\n", validator.errs());\r
99     }\r
100 \r
101     @Test\r
102     public void testNullEndpoints() {\r
103         LocateValidator validator = new LocateValidator();\r
104 \r
105         validator.endpoints(null, false);\r
106         validator.mgmt_endpoint_key(null);\r
107         validator.mgmt_endpoints(null, false);\r
108         assertEquals("Endpoints Data is null.\n" + "MgmtEndpoints Data is null.\n" + "MgmtEndpoints Data is null.\n",\r
109                 validator.errs());\r
110     }\r
111 \r
112     @Test\r
113     public void testEndpointsWithListContaingNull() {\r
114         LocateValidator validator = new LocateValidator();\r
115         when(endpoints.getEndpoint().size()).thenReturn(0);\r
116         when(me.getMgmtEndpoint().size()).thenReturn(0);\r
117 \r
118         validator.endpoints(endpoints, true);\r
119         validator.mgmt_endpoints(me, false);\r
120         assertEquals("Endpoints contains no endpoints\n" + "MgmtEndpoints contains no data\n", validator.errs());\r
121     }\r
122 \r
123     @Test\r
124     public void testEndpointsWithSpecialPortsNull() {\r
125         LocateValidator validator = new LocateValidator();\r
126 \r
127         when(endpoint.getName()).thenReturn("EndPoint.Endpoint1");\r
128         when(endpoint.getHostname()).thenReturn("HOST1");\r
129         when(endpoint.getPort()).thenReturn(9090);\r
130         when(endpoint.getProtocol()).thenReturn("HTTP");\r
131         List<String> subprotocol = new ArrayList<>();\r
132         when(endpoint.getSubprotocol()).thenReturn(subprotocol);\r
133 \r
134         List<Endpoint> endpointList = new ArrayList<>();\r
135         endpointList.add(endpoint);\r
136 \r
137         when(mgmtEndpoint.getName()).thenReturn("EndPoint.Endpoint1");\r
138         when(mgmtEndpoint.getHostname()).thenReturn("HOST1");\r
139         when(mgmtEndpoint.getPort()).thenReturn(9090);\r
140         when(mgmtEndpoint.getProtocol()).thenReturn("HTTP");\r
141         List<SpecialPorts> specialPorts = new ArrayList<>();\r
142         specialPorts.add(null);\r
143         when(mgmtEndpoint.getSpecialPorts()).thenReturn(specialPorts);\r
144         List<MgmtEndpoint> mgmtEndpoints = new ArrayList<>();\r
145         mgmtEndpoints.add(mgmtEndpoint);\r
146 \r
147         when(endpoints.getEndpoint()).thenReturn(endpointList);\r
148         when(me.getMgmtEndpoint()).thenReturn(mgmtEndpoints);\r
149 \r
150         validator.endpoints(endpoints, false);\r
151         validator.mgmt_endpoints(me, true);\r
152         assertEquals("Special Ports is null.\n", validator.errs());\r
153     }\r
154 \r
155     @Test\r
156     public void testEndpointsWithSpecialPorts() {\r
157         LocateValidator validator = new LocateValidator();\r
158 \r
159         when(mgmtEndpoint.getName()).thenReturn("EndPoint.Endpoint1");\r
160         when(mgmtEndpoint.getHostname()).thenReturn("HOST1");\r
161         when(mgmtEndpoint.getPort()).thenReturn(9090);\r
162         when(mgmtEndpoint.getProtocol()).thenReturn("HTTP");\r
163 \r
164         List<SpecialPorts> specialPorts = new ArrayList<>();\r
165         specialPorts.add(specialPort);\r
166 \r
167         when(specialPort.getName()).thenReturn("Port1");\r
168         when(specialPort.getProtocol()).thenReturn("HTTP");\r
169         when(specialPort.getPort()).thenReturn(9090);\r
170 \r
171         List<String> versions = new ArrayList<>();\r
172         versions.add("1");\r
173 \r
174         when(specialPort.getProtocolVersions()).thenReturn(versions);\r
175 \r
176         when(mgmtEndpoint.getSpecialPorts()).thenReturn(specialPorts);\r
177         List<MgmtEndpoint> mgmtEndpoints = new ArrayList<>();\r
178         mgmtEndpoints.add(mgmtEndpoint);\r
179 \r
180         when(me.getMgmtEndpoint()).thenReturn(mgmtEndpoints);\r
181 \r
182         validator.endpoints(endpoints, false);\r
183         validator.mgmt_endpoints(me, true);\r
184         validator.mgmt_endpoint_key(me);\r
185         assertEquals(false, validator.err());\r
186 \r
187     }\r
188 }\r