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.ArgumentMatchers.contains;
30 import static org.mockito.Mockito.doNothing;
32 import java.io.IOException;
34 import javax.inject.Provider;
35 import javax.ws.rs.core.MediaType;
36 import javax.ws.rs.core.Response;
38 import org.apache.http.HttpStatus;
39 import org.junit.Before;
40 import org.junit.Rule;
41 import org.junit.Test;
42 import org.junit.rules.ExpectedException;
43 import org.mockito.InjectMocks;
44 import org.mockito.Mock;
45 import org.mockito.Mockito;
46 import org.mockito.Spy;
47 import org.onap.so.apihandler.common.ErrorNumbers;
48 import org.onap.so.apihandlerinfra.BaseTest;
49 import org.onap.so.apihandlerinfra.exceptions.ApiException;
50 import org.onap.so.apihandlerinfra.exceptions.ValidateException;
51 import org.onap.so.apihandlerinfra.tenantisolationbeans.Action;
52 import org.onap.so.apihandlerinfra.tenantisolationbeans.Distribution;
53 import org.springframework.http.HttpEntity;
54 import org.springframework.http.HttpHeaders;
55 import org.springframework.http.HttpMethod;
56 import org.springframework.http.ResponseEntity;
57 import org.springframework.web.util.UriComponentsBuilder;
59 import com.fasterxml.jackson.databind.ObjectMapper;
61 public class ModelDistributionRequestTest extends BaseTest{
63 private static final String requestJSON = "{\"status\": \"DISTRIBUTION_COMPLETE_ERROR\", \"errorReason\": \"Distribution failed in AAI\" }";
66 public ExpectedException thrown = ExpectedException.none();
68 private Provider<TenantIsolationRunnable> thread;
71 private ModelDistributionRequest request = new ModelDistributionRequest();
73 private TenantIsolationRunnable runnable = new TenantIsolationRunnable();
76 public void beforeTest() {
77 Mockito.when(thread.get()).thenReturn(runnable);
81 public void testObjectMapperError() throws ApiException{
82 thrown.expect(ValidateException.class);
83 thrown.expectMessage(startsWith("Mapping of request to JSON object failed"));
84 thrown.expect(hasProperty("httpResponseCode", is(HttpStatus.SC_BAD_REQUEST)));
85 thrown.expect(hasProperty("messageID", is(ErrorNumbers.SVC_BAD_PARAMETER)));
86 request.updateModelDistributionStatus("", null, null);
90 public void testParseError1() throws ApiException{
91 thrown.expect(ValidateException.class);
92 thrown.expectMessage(startsWith("No valid status is specified"));
93 thrown.expect(hasProperty("httpResponseCode", is(HttpStatus.SC_BAD_REQUEST)));
94 thrown.expect(hasProperty("messageID", is(ErrorNumbers.SVC_BAD_PARAMETER)));
95 String requestErrorJSON = "{\"errorReason\": \"Distribution failed in AAI\" }";
96 request.updateModelDistributionStatus(requestErrorJSON, null, null);
100 public void testParseError2() throws ApiException{
101 thrown.expect(ValidateException.class);
102 thrown.expectMessage(startsWith("No valid errorReason is specified"));
103 thrown.expect(hasProperty("httpResponseCode", is(HttpStatus.SC_BAD_REQUEST)));
104 thrown.expect(hasProperty("messageID", is(ErrorNumbers.SVC_BAD_PARAMETER)));
105 String requestErrorJSON = "{\"status\": \"DISTRIBUTION_COMPLETE_ERROR\"}";
106 request.updateModelDistributionStatus(requestErrorJSON, null, null);
110 public void testSuccess() throws ApiException{
111 doNothing().when(runnable).run(any(Action.class), anyString(), any(CloudOrchestrationRequest.class), anyString());
113 Response response = request.updateModelDistributionStatus(requestJSON, null, null);
115 assertEquals(200, response.getStatus());
119 public void testSuccess_PATCH() throws ApiException, IOException{
120 String path = "/onap/so/infra/modelDistributions/v1/distributions/ff3514e3-5a33-55df-13ab-12abad84e7fa";
121 ObjectMapper mapper = new ObjectMapper();
122 Distribution distRequest = mapper.readValue(requestJSON, Distribution.class);
123 HttpHeaders headers = new HttpHeaders();
124 headers.set("Accept", MediaType.APPLICATION_JSON);
125 headers.set("Content-Type", MediaType.APPLICATION_JSON);
126 HttpEntity<Distribution> entity = new HttpEntity<Distribution>(distRequest, headers);
128 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path));
129 ResponseEntity<String> response = restTemplate.exchange(
130 builder.toUriString(),
131 HttpMethod.PATCH, entity, String.class);
132 assertEquals(200, response.getStatusCodeValue());