300157200dabe0444825a6a209de6dc0e64926e1
[sdnc/northbound.git] / generic-resource-api / provider / src / test / java / org / onap / sdnc / northbound / GenericResourceApiSvcLogicServiceClientTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : SDN-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                             reserved.
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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21
22 package org.onap.sdnc.northbound;
23
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;
35
36 import java.util.Properties;
37
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;
42
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";
48
49     private Logger mockLog = mock(Logger.class);
50     private SvcLogicService mockSvcLogic = mock(SvcLogicService.class);
51
52     private GenericResourceApiSvcLogicServiceClient svcClient;
53
54     @Before
55     public void setUp() throws Exception {
56         svcClient = spy(new GenericResourceApiSvcLogicServiceClient(mockSvcLogic));
57
58         Whitebox.setInternalState(svcClient, "LOG", mockLog);
59         Whitebox.setInternalState(svcClient, "svcLogic", mockSvcLogic);
60     }
61
62     @After
63     public void tearDown() throws Exception {
64     }
65
66     @Test
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"));
71     }
72
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);
77     }
78
79     @Test
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));
83     }
84
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));
90
91         svcClient.execute(MODULE, RPC, VERSION, MODE, mockBuilder);
92     }
93
94
95     @Test
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));
102     }
103
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));
109
110         svcClient.execute(MODULE, RPC, VERSION, MODE, mockBuilder);
111     }
112
113
114     @Test
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));
121     }
122
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));
128
129         svcClient.execute(MODULE, RPC, VERSION, MODE, mockBuilder, new Properties());
130     }
131
132     @Test
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));
139     }
140
141     @Test
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);
151     }
152
153     @Test
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);
163     }
164
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));
170
171         svcClient.execute(MODULE, RPC, VERSION, MODE, mockBuilder, new Properties());
172     }
173
174     @Test
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));
181     }
182
183     @Test
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);
193     }
194
195     @Test
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);
205     }
206
207 }