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.*;
41 public class GenericResourceApiSvcLogicServiceClientTest {
42 private static final String MODE = "mode";
43 private static final String MODULE = "module";
44 private static final String RPC = "rpc";
45 private static final String VERSION = "version";
47 private Logger mockLog = mock(Logger.class);
48 private SvcLogicService mockSvcLogic = mock(SvcLogicService.class);
50 private GenericResourceApiSvcLogicServiceClient svcClient;
53 public void setUp() throws Exception {
54 svcClient = spy(new GenericResourceApiSvcLogicServiceClient(mockSvcLogic));
56 Whitebox.setInternalState(svcClient, "LOG", mockLog);
57 Whitebox.setInternalState(svcClient, "svcLogic", mockSvcLogic);
61 public void tearDown() throws Exception {
65 public void testConstructorWithoutSvcLogicBundle() throws Exception {
66 GenericResourceApiSvcLogicServiceClient client = new GenericResourceApiSvcLogicServiceClient(mockSvcLogic);
67 Assert.assertEquals("Should have set mockSvcLogic",
68 mockSvcLogic, Whitebox.getInternalState(client, "svcLogic"));
71 @Test (expected = SvcLogicException.class)
72 public void testHasGraphWithException() throws Exception {
73 Mockito.doThrow(new SvcLogicException()).when(mockSvcLogic).hasGraph(MODULE, RPC, VERSION, MODE);
74 svcClient.hasGraph(MODULE, RPC, VERSION, MODE);
78 public void testHasGraph() throws Exception {
79 Mockito.doReturn(true).when(mockSvcLogic).hasGraph(MODULE, RPC, VERSION, MODE);
80 Assert.assertTrue("Should return true", svcClient.hasGraph(MODULE, RPC, VERSION, MODE));
83 @Test (expected = SvcLogicException.class)
84 public void testExecuteWithServiceDataBuilderWithException() throws Exception {
85 ServiceDataBuilder mockBuilder = mock(ServiceDataBuilder.class);
86 Mockito.doThrow(new SvcLogicException()).when(mockSvcLogic).execute(
87 any(String.class), any(String.class), any(String.class), any(String.class), any(Properties.class));
89 svcClient.execute(MODULE, RPC, VERSION, MODE, mockBuilder);
94 public void testExecuteWithServiceDataBuilder() throws Exception {
95 ServiceDataBuilder mockBuilder = mock(ServiceDataBuilder.class);
96 svcClient.execute(MODULE, RPC, VERSION, MODE, mockBuilder);
97 Mockito.verify(svcClient, times(1)).execute(
98 any(String.class), any(String.class), any(String.class), any(String.class),
99 any(ServiceDataBuilder.class), any(Properties.class));
102 @Test (expected = SvcLogicException.class)
103 public void testExecuteWithPreloadDataBuilderWithException() throws Exception {
104 PreloadDataBuilder mockBuilder = mock(PreloadDataBuilder.class);
105 Mockito.doThrow(new SvcLogicException()).when(mockSvcLogic).execute(
106 any(String.class), any(String.class), any(String.class), any(String.class), any(Properties.class));
108 svcClient.execute(MODULE, RPC, VERSION, MODE, mockBuilder);
113 public void testExecuteWithPreloadDataBuilder () throws Exception {
114 PreloadDataBuilder mockBuilder = mock(PreloadDataBuilder.class);
115 svcClient.execute(MODULE, RPC, VERSION, MODE, mockBuilder);
116 Mockito.verify(svcClient, times(1)).execute(
117 any(String.class), any(String.class), any(String.class), any(String.class),
118 any(PreloadDataBuilder .class), any(Properties.class));
121 @Test (expected = SvcLogicException.class)
122 public void testParamExecuteWithServiceDataBuilderWithException() throws Exception {
123 ServiceDataBuilder mockBuilder = mock(ServiceDataBuilder.class);
124 Mockito.doThrow(new SvcLogicException()).when(mockSvcLogic).execute(
125 any(String.class), any(String.class), any(String.class), any(String.class), any(Properties.class));
127 svcClient.execute(MODULE, RPC, VERSION, MODE, mockBuilder, new Properties());
131 public void testParamExecuteWithServiceDataBuilderWithExecutorReturnNull() throws Exception {
132 ServiceDataBuilder mockBuilder = mock(ServiceDataBuilder.class);
133 Properties properties = svcClient.execute(MODULE, RPC, VERSION, MODE, mockBuilder, new Properties());
134 Assert.assertTrue("Should return null", properties == null);
135 Mockito.verify(mockSvcLogic, times(1)).execute(
136 any(String.class), any(String.class), any(String.class), any(String.class), any(Properties.class));
140 public void testParamExecuteWithServiceDataBuilderWithExecutorReturnFailure() throws Exception {
141 ServiceDataBuilder mockBuilder = mock(ServiceDataBuilder.class);
142 Properties resultProps = new Properties();
143 resultProps.setProperty(GenericResourceApiSvcLogicServiceClient.SVC_LOGIC_STATUS_KEY,
144 GenericResourceApiSvcLogicServiceClient.FAILURE_RESULT);
145 Mockito.doReturn(resultProps).when(mockSvcLogic).execute(
146 any(String.class), any(String.class), any(String.class), any(String.class), any(Properties.class));
147 Properties properties = svcClient.execute(MODULE, RPC, VERSION, MODE, mockBuilder, new Properties());
148 Assert.assertEquals("Should return resultProps", resultProps, properties);
152 public void testParamExecuteWithServiceDataBuilder() throws Exception {
153 Mockito.doReturn(true).when(mockLog).isDebugEnabled();
154 ServiceDataBuilder mockBuilder = mock(ServiceDataBuilder.class);
155 Properties resultProps = new Properties();
156 resultProps.setProperty("my", "testing");
157 Mockito.doReturn(resultProps).when(mockSvcLogic).execute(
158 any(String.class), any(String.class), any(String.class), any(String.class), any(Properties.class));
159 Properties properties = svcClient.execute(MODULE, RPC, VERSION, MODE, mockBuilder, new Properties());
160 Assert.assertEquals("Should still return resultProps", resultProps, properties);
163 @Test (expected = SvcLogicException.class)
164 public void testParamExecuteWithPreloadDataBuilderWithException() throws Exception {
165 PreloadDataBuilder mockBuilder = mock(PreloadDataBuilder.class);
166 Mockito.doThrow(new SvcLogicException()).when(mockSvcLogic).execute(
167 any(String.class), any(String.class), any(String.class), any(String.class), any(Properties.class));
169 svcClient.execute(MODULE, RPC, VERSION, MODE, mockBuilder, new Properties());
173 public void testParamExecuteWithPreloadDataBuilderWithExecutorReturnNull() throws Exception {
174 PreloadDataBuilder mockBuilder = mock(PreloadDataBuilder.class);
175 Properties properties = svcClient.execute(MODULE, RPC, VERSION, MODE, mockBuilder, new Properties());
176 Assert.assertTrue("Should return null", properties == null);
177 Mockito.verify(mockSvcLogic, times(1)).execute(
178 any(String.class), any(String.class), any(String.class), any(String.class), any(Properties.class));
182 public void testParamExecuteWithPreloadDataBuilderWithExecutorReturnFailure() throws Exception {
183 PreloadDataBuilder mockBuilder = mock(PreloadDataBuilder.class);
184 Properties resultProps = new Properties();
185 resultProps.setProperty(GenericResourceApiSvcLogicServiceClient.SVC_LOGIC_STATUS_KEY,
186 GenericResourceApiSvcLogicServiceClient.FAILURE_RESULT);
187 Mockito.doReturn(resultProps).when(mockSvcLogic).execute(
188 any(String.class), any(String.class), any(String.class), any(String.class), any(Properties.class));
189 Properties properties = svcClient.execute(MODULE, RPC, VERSION, MODE, mockBuilder, new Properties());
190 Assert.assertEquals("Should return resultProps", resultProps, properties);
194 public void testParamExecuteWithPreloadDataBuilder() throws Exception {
195 Mockito.doReturn(true).when(mockLog).isDebugEnabled();
196 PreloadDataBuilder mockBuilder = mock(PreloadDataBuilder.class);
197 Properties resultProps = new Properties();
198 resultProps.setProperty("my", "testing");
199 Mockito.doReturn(resultProps).when(mockSvcLogic).execute(
200 any(String.class), any(String.class), any(String.class), any(String.class), any(Properties.class));
201 Properties properties = svcClient.execute(MODULE, RPC, VERSION, MODE, mockBuilder, new Properties());
202 Assert.assertEquals("Should still return resultProps", resultProps, properties);