1 /*******************************************************************************
2 * =============LICENSE_START=========================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21 *******************************************************************************/
23 package org.onap.ccsdk.dashboard.controller;
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;
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;
49 import com.fasterxml.jackson.core.JsonProcessingException;
50 import com.fasterxml.jackson.databind.ObjectMapper;
51 import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
53 public class DeploymentHandlerControllerTest extends MockitoTestSuite {
56 DeploymentHandlerClient restClient;
59 DeploymentHandlerController subject = new DeploymentHandlerController();
61 protected final ObjectMapper objectMapper = new ObjectMapper();
63 BadRequestException badReqError;
64 ServiceAlreadyExistsException srvcExistError;
65 ServerErrorException serverError;
66 DownstreamException downStrmError;
67 JsonProcessingException jsonError;
68 DeploymentNotFoundException notFoundError;
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");
84 public void tearDown() throws Exception {
87 @SuppressWarnings("unchecked")
89 public final void testPutDeployment_create() throws Exception {
90 DeploymentRequestObject expectReq =
91 new DeploymentRequestObject("dep1", "dep1", null, "tenant1", "create");
93 DeploymentResponseLinks expectLink = new DeploymentResponseLinks("self", "status");
94 DeploymentResponse expectResp = new DeploymentResponse("req1", expectLink);
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);
101 String actualResp = subject.putDeployment(mockedRequest, expectReq);
102 assertTrue(actualResp.contains("req1"));
104 actualResp = subject.putDeployment(mockedRequest, expectReq);
105 assertTrue(actualResp.contains("error"));
107 actualResp = subject.putDeployment(mockedRequest, expectReq);
108 assertTrue(actualResp.contains("error"));
110 actualResp = subject.putDeployment(mockedRequest, expectReq);
111 assertTrue(actualResp.contains("error"));
113 actualResp = subject.putDeployment(mockedRequest, expectReq);
114 assertTrue(actualResp.contains("error"));
116 actualResp = subject.putDeployment(mockedRequest, expectReq);
117 assertTrue(actualResp.contains("error"));
120 @SuppressWarnings("unchecked")
122 public final void testPutDeployment_update() throws Exception {
123 DeploymentRequestObject expectReq =
124 new DeploymentRequestObject("dep1", "dep1", null, "tenant1", "update");
126 DeploymentResponseLinks expectLink = new DeploymentResponseLinks("self", "status");
127 DeploymentResponse expectResp = new DeploymentResponse("req1", expectLink);
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);
134 String actualResp = subject.putDeployment(mockedRequest, expectReq);
135 assertTrue(actualResp.contains("req1"));
137 actualResp = subject.putDeployment(mockedRequest, expectReq);
138 assertTrue(actualResp.contains("error"));
140 actualResp = subject.putDeployment(mockedRequest, expectReq);
141 assertTrue(actualResp.contains("error"));
143 actualResp = subject.putDeployment(mockedRequest, expectReq);
144 assertTrue(actualResp.contains("error"));
146 actualResp = subject.putDeployment(mockedRequest, expectReq);
147 assertTrue(actualResp.contains("error"));
149 actualResp = subject.putDeployment(mockedRequest, expectReq);
150 assertTrue(actualResp.contains("error"));
154 public final void testDeleteDeployment() throws Exception {
156 doNothing().doThrow(badReqError).doThrow(serverError).doThrow(downStrmError)
157 .doThrow(notFoundError).doThrow(Exception.class).when(restClient)
158 .deleteDeployment(Matchers.anyString(), Matchers.anyString());
160 StringBuffer expectedStrBuff = new StringBuffer();
161 expectedStrBuff.append("http://oom.s2.com");
162 when(mockedRequest.getRequestURL()).thenReturn(expectedStrBuff);
164 String actual = subject.deleteDeployment("dep1", mockedRequest, "tenant1", mockedResponse);
165 assertFalse(actual.contains("error"));
167 actual = subject.deleteDeployment("dep1", mockedRequest, "tenant1", mockedResponse);
168 assertTrue(actual.contains("error"));
170 actual = subject.deleteDeployment("dep1", mockedRequest, "tenant1", mockedResponse);
171 assertTrue(actual.contains("error"));
173 actual = subject.deleteDeployment("dep1", mockedRequest, "tenant1", mockedResponse);
174 assertTrue(actual.contains("error"));
176 actual = subject.deleteDeployment("dep1", mockedRequest, "tenant1", mockedResponse);
177 assertTrue(actual.contains("error"));
179 actual = subject.deleteDeployment("dep1", mockedRequest, "tenant1", mockedResponse);
180 assertTrue(actual.contains("error"));