Upgrade to ODL Aluminum
[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.onap.ccsdk.sli.core.sli.SvcLogicException;
30 import org.onap.ccsdk.sli.core.sli.provider.SvcLogicService;
31 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.preload.data.PreloadDataBuilder;
32 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.service.data.ServiceDataBuilder;
33 import org.slf4j.Logger;
34
35 import java.util.Properties;
36
37 import static org.mockito.Matchers.any;
38 import static org.mockito.Mockito.mock;
39 import static org.mockito.Mockito.spy;
40 import static org.mockito.Mockito.times;
41
42 public class GenericResourceApiSvcLogicServiceClientTest {
43     private static final String MODE = "mode";
44     private static final String MODULE = "module";
45     private static final String RPC = "rpc";
46     private static final String VERSION = "version";
47
48     private Logger mockLog = mock(Logger.class);
49     private SvcLogicService mockSvcLogic = mock(SvcLogicService.class);
50
51     private GenericResourceApiSvcLogicServiceClient svcClient;
52
53     @Before
54     public void setUp() throws Exception {
55         svcClient = spy(new GenericResourceApiSvcLogicServiceClient(mockSvcLogic));
56         svcClient.LOG = mockLog;
57         svcClient.svcLogic = mockSvcLogic;
58     }
59
60     @After
61     public void tearDown() throws Exception {
62     }
63
64     @Test
65     public void testConstructorWithoutSvcLogicBundle() throws Exception {
66         GenericResourceApiSvcLogicServiceClient client = new GenericResourceApiSvcLogicServiceClient(mockSvcLogic);
67         Assert.assertEquals("Should have set mockSvcLogic",
68                 mockSvcLogic, client.svcLogic);
69     }
70
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);
75     }
76
77     @Test
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));
81     }
82
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));
88
89         svcClient.execute(MODULE, RPC, VERSION, MODE, mockBuilder);
90     }
91
92
93     @Test
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));
100     }
101
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));
107
108         svcClient.execute(MODULE, RPC, VERSION, MODE, mockBuilder);
109     }
110
111
112     @Test
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));
119     }
120
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));
126
127         svcClient.execute(MODULE, RPC, VERSION, MODE, mockBuilder, new Properties());
128     }
129
130     @Test
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));
137     }
138
139     @Test
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);
149     }
150
151     @Test
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);
161     }
162
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));
168
169         svcClient.execute(MODULE, RPC, VERSION, MODE, mockBuilder, new Properties());
170     }
171
172     @Test
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));
179     }
180
181     @Test
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);
191     }
192
193     @Test
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);
203     }
204
205 }