2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.sdnc.northbound;
24 import org.junit.After;
25 import org.junit.Assert;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.mockito.Mockito;
29 import org.mockito.internal.util.reflection.Whitebox;
30 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
31 import org.onap.ccsdk.sli.core.sli.provider.SvcLogicService;
32 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.preload.data.PreloadDataBuilder;
33 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.service.data.ServiceDataBuilder;
34 import org.slf4j.Logger;
36 import java.util.Properties;
38 import static org.mockito.Matchers.any;
39 import static org.mockito.Mockito.mock;
40 import static org.mockito.Mockito.spy;
41 import static org.mockito.Mockito.times;
43 public class GenericResourceApiSvcLogicServiceClientTest {
44 private static final String MODE = "mode";
45 private static final String MODULE = "module";
46 private static final String RPC = "rpc";
47 private static final String VERSION = "version";
49 private Logger mockLog = mock(Logger.class);
50 private SvcLogicService mockSvcLogic = mock(SvcLogicService.class);
52 private GenericResourceApiSvcLogicServiceClient svcClient;
55 public void setUp() throws Exception {
56 svcClient = spy(new GenericResourceApiSvcLogicServiceClient(mockSvcLogic));
58 Whitebox.setInternalState(svcClient, "LOG", mockLog);
59 Whitebox.setInternalState(svcClient, "svcLogic", mockSvcLogic);
63 public void tearDown() throws Exception {
67 public void testConstructorWithoutSvcLogicBundle() throws Exception {
68 GenericResourceApiSvcLogicServiceClient client = new GenericResourceApiSvcLogicServiceClient(mockSvcLogic);
69 Assert.assertEquals("Should have set mockSvcLogic",
70 mockSvcLogic, Whitebox.getInternalState(client, "svcLogic"));
73 @Test (expected = SvcLogicException.class)
74 public void testHasGraphWithException() throws Exception {
75 Mockito.doThrow(new SvcLogicException()).when(mockSvcLogic).hasGraph(MODULE, RPC, VERSION, MODE);
76 svcClient.hasGraph(MODULE, RPC, VERSION, MODE);
80 public void testHasGraph() throws Exception {
81 Mockito.doReturn(true).when(mockSvcLogic).hasGraph(MODULE, RPC, VERSION, MODE);
82 Assert.assertTrue("Should return true", svcClient.hasGraph(MODULE, RPC, VERSION, MODE));
85 @Test (expected = SvcLogicException.class)
86 public void testExecuteWithServiceDataBuilderWithException() throws Exception {
87 ServiceDataBuilder mockBuilder = mock(ServiceDataBuilder.class);
88 Mockito.doThrow(new SvcLogicException()).when(mockSvcLogic).execute(
89 any(String.class), any(String.class), any(String.class), any(String.class), any(Properties.class));
91 svcClient.execute(MODULE, RPC, VERSION, MODE, mockBuilder);
96 public void testExecuteWithServiceDataBuilder() throws Exception {
97 ServiceDataBuilder mockBuilder = mock(ServiceDataBuilder.class);
98 svcClient.execute(MODULE, RPC, VERSION, MODE, mockBuilder);
99 Mockito.verify(svcClient, times(1)).execute(
100 any(String.class), any(String.class), any(String.class), any(String.class),
101 any(ServiceDataBuilder.class), any(Properties.class));
104 @Test (expected = SvcLogicException.class)
105 public void testExecuteWithPreloadDataBuilderWithException() throws Exception {
106 PreloadDataBuilder mockBuilder = mock(PreloadDataBuilder.class);
107 Mockito.doThrow(new SvcLogicException()).when(mockSvcLogic).execute(
108 any(String.class), any(String.class), any(String.class), any(String.class), any(Properties.class));
110 svcClient.execute(MODULE, RPC, VERSION, MODE, mockBuilder);
115 public void testExecuteWithPreloadDataBuilder () throws Exception {
116 PreloadDataBuilder mockBuilder = mock(PreloadDataBuilder.class);
117 svcClient.execute(MODULE, RPC, VERSION, MODE, mockBuilder);
118 Mockito.verify(svcClient, times(1)).execute(
119 any(String.class), any(String.class), any(String.class), any(String.class),
120 any(PreloadDataBuilder .class), any(Properties.class));
123 @Test (expected = SvcLogicException.class)
124 public void testParamExecuteWithServiceDataBuilderWithException() throws Exception {
125 ServiceDataBuilder mockBuilder = mock(ServiceDataBuilder.class);
126 Mockito.doThrow(new SvcLogicException()).when(mockSvcLogic).execute(
127 any(String.class), any(String.class), any(String.class), any(String.class), any(Properties.class));
129 svcClient.execute(MODULE, RPC, VERSION, MODE, mockBuilder, new Properties());
133 public void testParamExecuteWithServiceDataBuilderWithExecutorReturnNull() throws Exception {
134 ServiceDataBuilder mockBuilder = mock(ServiceDataBuilder.class);
135 Properties properties = svcClient.execute(MODULE, RPC, VERSION, MODE, mockBuilder, new Properties());
136 Assert.assertTrue("Should return null", properties == null);
137 Mockito.verify(mockSvcLogic, times(1)).execute(
138 any(String.class), any(String.class), any(String.class), any(String.class), any(Properties.class));
142 public void testParamExecuteWithServiceDataBuilderWithExecutorReturnFailure() throws Exception {
143 ServiceDataBuilder mockBuilder = mock(ServiceDataBuilder.class);
144 Properties resultProps = new Properties();
145 resultProps.setProperty(GenericResourceApiSvcLogicServiceClient.SVC_LOGIC_STATUS_KEY,
146 GenericResourceApiSvcLogicServiceClient.FAILURE_RESULT);
147 Mockito.doReturn(resultProps).when(mockSvcLogic).execute(
148 any(String.class), any(String.class), any(String.class), any(String.class), any(Properties.class));
149 Properties properties = svcClient.execute(MODULE, RPC, VERSION, MODE, mockBuilder, new Properties());
150 Assert.assertEquals("Should return resultProps", resultProps, properties);
154 public void testParamExecuteWithServiceDataBuilder() throws Exception {
155 Mockito.doReturn(true).when(mockLog).isDebugEnabled();
156 ServiceDataBuilder mockBuilder = mock(ServiceDataBuilder.class);
157 Properties resultProps = new Properties();
158 resultProps.setProperty("my", "testing");
159 Mockito.doReturn(resultProps).when(mockSvcLogic).execute(
160 any(String.class), any(String.class), any(String.class), any(String.class), any(Properties.class));
161 Properties properties = svcClient.execute(MODULE, RPC, VERSION, MODE, mockBuilder, new Properties());
162 Assert.assertEquals("Should still return resultProps", resultProps, properties);
165 @Test (expected = SvcLogicException.class)
166 public void testParamExecuteWithPreloadDataBuilderWithException() throws Exception {
167 PreloadDataBuilder mockBuilder = mock(PreloadDataBuilder.class);
168 Mockito.doThrow(new SvcLogicException()).when(mockSvcLogic).execute(
169 any(String.class), any(String.class), any(String.class), any(String.class), any(Properties.class));
171 svcClient.execute(MODULE, RPC, VERSION, MODE, mockBuilder, new Properties());
175 public void testParamExecuteWithPreloadDataBuilderWithExecutorReturnNull() throws Exception {
176 PreloadDataBuilder mockBuilder = mock(PreloadDataBuilder.class);
177 Properties properties = svcClient.execute(MODULE, RPC, VERSION, MODE, mockBuilder, new Properties());
178 Assert.assertTrue("Should return null", properties == null);
179 Mockito.verify(mockSvcLogic, times(1)).execute(
180 any(String.class), any(String.class), any(String.class), any(String.class), any(Properties.class));
184 public void testParamExecuteWithPreloadDataBuilderWithExecutorReturnFailure() throws Exception {
185 PreloadDataBuilder mockBuilder = mock(PreloadDataBuilder.class);
186 Properties resultProps = new Properties();
187 resultProps.setProperty(GenericResourceApiSvcLogicServiceClient.SVC_LOGIC_STATUS_KEY,
188 GenericResourceApiSvcLogicServiceClient.FAILURE_RESULT);
189 Mockito.doReturn(resultProps).when(mockSvcLogic).execute(
190 any(String.class), any(String.class), any(String.class), any(String.class), any(Properties.class));
191 Properties properties = svcClient.execute(MODULE, RPC, VERSION, MODE, mockBuilder, new Properties());
192 Assert.assertEquals("Should return resultProps", resultProps, properties);
196 public void testParamExecuteWithPreloadDataBuilder() throws Exception {
197 Mockito.doReturn(true).when(mockLog).isDebugEnabled();
198 PreloadDataBuilder mockBuilder = mock(PreloadDataBuilder.class);
199 Properties resultProps = new Properties();
200 resultProps.setProperty("my", "testing");
201 Mockito.doReturn(resultProps).when(mockSvcLogic).execute(
202 any(String.class), any(String.class), any(String.class), any(String.class), any(Properties.class));
203 Properties properties = svcClient.execute(MODULE, RPC, VERSION, MODE, mockBuilder, new Properties());
204 Assert.assertEquals("Should still return resultProps", resultProps, properties);