6517b68d3dda2315631641795361d4f64583bdc7
[ccsdk/dashboard.git] /
1 /*******************************************************************************
2  * =============LICENSE_START=========================================================
3  *
4  * =================================================================================
5  * Copyright (c) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  * http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  *
20  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  *******************************************************************************/
22
23 package org.onap.ccsdk.dashboard.controller;
24
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertTrue;
27 import static org.mockito.Mockito.doNothing;
28 import static org.mockito.Mockito.when;
29
30 import org.junit.After;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.mockito.InjectMocks;
34 import org.mockito.Matchers;
35 import org.mockito.Mock;
36 import org.mockito.MockitoAnnotations;
37 import org.onap.ccsdk.dashboard.core.MockitoTestSuite;
38 import org.onap.ccsdk.dashboard.exceptions.BadRequestException;
39 import org.onap.ccsdk.dashboard.exceptions.DeploymentNotFoundException;
40 import org.onap.ccsdk.dashboard.exceptions.DownstreamException;
41 import org.onap.ccsdk.dashboard.exceptions.ServerErrorException;
42 import org.onap.ccsdk.dashboard.exceptions.ServiceAlreadyExistsException;
43 import org.onap.ccsdk.dashboard.model.deploymenthandler.DeploymentRequest;
44 import org.onap.ccsdk.dashboard.model.deploymenthandler.DeploymentRequestObject;
45 import org.onap.ccsdk.dashboard.model.deploymenthandler.DeploymentResponse;
46 import org.onap.ccsdk.dashboard.model.deploymenthandler.DeploymentResponseLinks;
47 import org.onap.ccsdk.dashboard.rest.DeploymentHandlerClient;
48
49 import com.fasterxml.jackson.core.JsonProcessingException;
50 import com.fasterxml.jackson.databind.ObjectMapper;
51 import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
52
53 public class DeploymentHandlerControllerTest extends MockitoTestSuite {
54
55     @Mock
56     DeploymentHandlerClient restClient;
57
58     @InjectMocks
59     DeploymentHandlerController subject = new DeploymentHandlerController();
60
61     protected final ObjectMapper objectMapper = new ObjectMapper();
62
63     BadRequestException badReqError;
64     ServiceAlreadyExistsException srvcExistError;
65     ServerErrorException serverError;
66     DownstreamException downStrmError;
67     JsonProcessingException jsonError;
68     DeploymentNotFoundException notFoundError;
69
70     @Before
71     public void setUp() throws Exception {
72         MockitoAnnotations.initMocks(this);
73         objectMapper.registerModule(new Jdk8Module());
74         mockedRequest = getMockedRequest();
75         mockedResponse = getMockedResponse();
76         badReqError = new BadRequestException("bad request");
77         srvcExistError = new ServiceAlreadyExistsException("service already exists");
78         serverError = new ServerErrorException("Error occured in server");
79         downStrmError = new DownstreamException("error occured in downstream");
80         notFoundError = new DeploymentNotFoundException("item not found");
81     }
82
83     @After
84     public void tearDown() throws Exception {
85     }
86
87     @SuppressWarnings("unchecked")
88     @Test
89     public final void testPutDeployment_create() throws Exception {
90         DeploymentRequestObject expectReq =
91             new DeploymentRequestObject("dep1", "dep1", null, "tenant1", "create");
92
93         DeploymentResponseLinks expectLink = new DeploymentResponseLinks("self", "status");
94         DeploymentResponse expectResp = new DeploymentResponse("req1", expectLink);
95
96         when(restClient.putDeployment(Matchers.anyString(), Matchers.anyString(),
97             Matchers.<DeploymentRequest>any())).thenReturn(expectResp).thenThrow(badReqError)
98                 .thenThrow(srvcExistError).thenThrow(serverError).thenThrow(downStrmError)
99                 .thenThrow(Exception.class);
100
101         String actualResp = subject.putDeployment(mockedRequest, expectReq);
102         assertTrue(actualResp.contains("req1"));
103
104         actualResp = subject.putDeployment(mockedRequest, expectReq);
105         assertTrue(actualResp.contains("error"));
106
107         actualResp = subject.putDeployment(mockedRequest, expectReq);
108         assertTrue(actualResp.contains("error"));
109
110         actualResp = subject.putDeployment(mockedRequest, expectReq);
111         assertTrue(actualResp.contains("error"));
112
113         actualResp = subject.putDeployment(mockedRequest, expectReq);
114         assertTrue(actualResp.contains("error"));
115
116         actualResp = subject.putDeployment(mockedRequest, expectReq);
117         assertTrue(actualResp.contains("error"));
118     }
119
120     @SuppressWarnings("unchecked")
121     @Test
122     public final void testPutDeployment_update() throws Exception {
123         DeploymentRequestObject expectReq =
124             new DeploymentRequestObject("dep1", "dep1", null, "tenant1", "update");
125
126         DeploymentResponseLinks expectLink = new DeploymentResponseLinks("self", "status");
127         DeploymentResponse expectResp = new DeploymentResponse("req1", expectLink);
128
129         when(restClient.updateDeployment(Matchers.anyString(), Matchers.anyString(),
130             Matchers.<DeploymentRequest>any())).thenReturn(expectResp).thenThrow(badReqError)
131                 .thenThrow(srvcExistError).thenThrow(serverError).thenThrow(downStrmError)
132                 .thenThrow(Exception.class);
133
134         String actualResp = subject.putDeployment(mockedRequest, expectReq);
135         assertTrue(actualResp.contains("req1"));
136
137         actualResp = subject.putDeployment(mockedRequest, expectReq);
138         assertTrue(actualResp.contains("error"));
139
140         actualResp = subject.putDeployment(mockedRequest, expectReq);
141         assertTrue(actualResp.contains("error"));
142
143         actualResp = subject.putDeployment(mockedRequest, expectReq);
144         assertTrue(actualResp.contains("error"));
145
146         actualResp = subject.putDeployment(mockedRequest, expectReq);
147         assertTrue(actualResp.contains("error"));
148
149         actualResp = subject.putDeployment(mockedRequest, expectReq);
150         assertTrue(actualResp.contains("error"));
151     }
152
153     @Test
154     public final void testDeleteDeployment() throws Exception {
155
156         doNothing().doThrow(badReqError).doThrow(serverError).doThrow(downStrmError)
157             .doThrow(notFoundError).doThrow(Exception.class).when(restClient)
158             .deleteDeployment(Matchers.anyString(), Matchers.anyString());
159
160         StringBuffer expectedStrBuff = new StringBuffer();
161         expectedStrBuff.append("http://oom.s2.com");
162         when(mockedRequest.getRequestURL()).thenReturn(expectedStrBuff);
163
164         String actual = subject.deleteDeployment("dep1", mockedRequest, "tenant1", mockedResponse);
165         assertFalse(actual.contains("error"));
166
167         actual = subject.deleteDeployment("dep1", mockedRequest, "tenant1", mockedResponse);
168         assertTrue(actual.contains("error"));
169
170         actual = subject.deleteDeployment("dep1", mockedRequest, "tenant1", mockedResponse);
171         assertTrue(actual.contains("error"));
172
173         actual = subject.deleteDeployment("dep1", mockedRequest, "tenant1", mockedResponse);
174         assertTrue(actual.contains("error"));
175
176         actual = subject.deleteDeployment("dep1", mockedRequest, "tenant1", mockedResponse);
177         assertTrue(actual.contains("error"));
178
179         actual = subject.deleteDeployment("dep1", mockedRequest, "tenant1", mockedResponse);
180         assertTrue(actual.contains("error"));
181
182     }
183
184 }