2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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=========================================================
21 package org.onap.so.apihandlerinfra.tenantisolation;
23 import static org.hamcrest.Matchers.hasProperty;
24 import static org.hamcrest.Matchers.is;
25 import static org.hamcrest.Matchers.startsWith;
26 import static org.junit.Assert.assertEquals;
27 import static org.mockito.ArgumentMatchers.any;
28 import static org.mockito.ArgumentMatchers.anyString;
29 import static org.mockito.Mockito.doNothing;
30 import java.io.IOException;
31 import javax.inject.Provider;
32 import javax.ws.rs.core.MediaType;
33 import javax.ws.rs.core.Response;
34 import org.apache.http.HttpStatus;
35 import org.junit.Before;
36 import org.junit.Rule;
37 import org.junit.Test;
38 import org.junit.rules.ExpectedException;
39 import org.mockito.InjectMocks;
40 import org.mockito.Mock;
41 import org.mockito.Mockito;
42 import org.mockito.Spy;
43 import org.onap.so.apihandler.common.ErrorNumbers;
44 import org.onap.so.apihandlerinfra.BaseTest;
45 import org.onap.so.apihandlerinfra.exceptions.ApiException;
46 import org.onap.so.apihandlerinfra.exceptions.ValidateException;
47 import org.onap.so.apihandlerinfra.tenantisolationbeans.Action;
48 import org.onap.so.apihandlerinfra.tenantisolationbeans.Distribution;
49 import org.springframework.http.HttpEntity;
50 import org.springframework.http.HttpHeaders;
51 import org.springframework.http.HttpMethod;
52 import org.springframework.http.ResponseEntity;
53 import org.springframework.web.util.UriComponentsBuilder;
54 import com.fasterxml.jackson.databind.ObjectMapper;
56 public class ModelDistributionRequestTest extends BaseTest {
58 private static final String requestJSON =
59 "{\"status\": \"DISTRIBUTION_COMPLETE_ERROR\", \"errorReason\": \"Distribution failed in AAI\" }";
62 public ExpectedException thrown = ExpectedException.none();
64 private Provider<TenantIsolationRunnable> thread;
67 private ModelDistributionRequest request = new ModelDistributionRequest();
69 private TenantIsolationRunnable runnable = new TenantIsolationRunnable();
72 public void beforeTest() {
73 Mockito.when(thread.get()).thenReturn(runnable);
77 public void testObjectMapperError() throws ApiException {
78 thrown.expect(ValidateException.class);
79 thrown.expectMessage(startsWith("Mapping of request to JSON object failed"));
80 thrown.expect(hasProperty("httpResponseCode", is(HttpStatus.SC_BAD_REQUEST)));
81 thrown.expect(hasProperty("messageID", is(ErrorNumbers.SVC_BAD_PARAMETER)));
82 request.updateModelDistributionStatus("", null, null);
86 public void testParseError1() throws ApiException {
87 thrown.expect(ValidateException.class);
88 thrown.expectMessage(startsWith("No valid status is specified"));
89 thrown.expect(hasProperty("httpResponseCode", is(HttpStatus.SC_BAD_REQUEST)));
90 thrown.expect(hasProperty("messageID", is(ErrorNumbers.SVC_BAD_PARAMETER)));
91 String requestErrorJSON = "{\"errorReason\": \"Distribution failed in AAI\" }";
92 request.updateModelDistributionStatus(requestErrorJSON, null, null);
96 public void testParseError2() throws ApiException {
97 thrown.expect(ValidateException.class);
98 thrown.expectMessage(startsWith("No valid errorReason is specified"));
99 thrown.expect(hasProperty("httpResponseCode", is(HttpStatus.SC_BAD_REQUEST)));
100 thrown.expect(hasProperty("messageID", is(ErrorNumbers.SVC_BAD_PARAMETER)));
101 String requestErrorJSON = "{\"status\": \"DISTRIBUTION_COMPLETE_ERROR\"}";
102 request.updateModelDistributionStatus(requestErrorJSON, null, null);
106 public void testSuccess() throws ApiException {
107 doNothing().when(runnable).run(any(Action.class), anyString(), any(CloudOrchestrationRequest.class),
110 Response response = request.updateModelDistributionStatus(requestJSON, null, null);
112 assertEquals(200, response.getStatus());
116 public void testSuccess_PATCH() throws ApiException, IOException {
117 String path = "/onap/so/infra/modelDistributions/v1/distributions/ff3514e3-5a33-55df-13ab-12abad84e7fa";
118 ObjectMapper mapper = new ObjectMapper();
119 Distribution distRequest = mapper.readValue(requestJSON, Distribution.class);
120 HttpHeaders headers = new HttpHeaders();
121 headers.set("Accept", MediaType.APPLICATION_JSON);
122 headers.set("Content-Type", MediaType.APPLICATION_JSON);
123 HttpEntity<Distribution> entity = new HttpEntity<Distribution>(distRequest, headers);
125 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path));
126 ResponseEntity<String> response =
127 restTemplate.exchange(builder.toUriString(), HttpMethod.PATCH, entity, String.class);
128 assertEquals(200, response.getStatusCodeValue());