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.junit.Assert.assertTrue;
28 import static org.mockito.ArgumentMatchers.any;
29 import static org.mockito.Matchers.anyString;
30 import static org.mockito.Matchers.contains;
31 import static org.mockito.Mockito.doNothing;
33 import javax.inject.Provider;
34 import javax.ws.rs.core.Response;
36 import org.apache.http.HttpStatus;
37 import org.junit.Before;
38 import org.junit.Rule;
39 import org.junit.Test;
40 import org.junit.rules.ExpectedException;
41 import org.junit.runner.RunWith;
42 import org.mockito.InjectMocks;
43 import org.mockito.Mock;
44 import org.mockito.Mockito;
45 import org.mockito.Spy;
46 import org.mockito.junit.MockitoJUnitRunner;
47 import org.onap.so.apihandler.common.ErrorNumbers;
48 import org.onap.so.apihandlerinfra.ApiHandlerApplication;
49 import org.onap.so.apihandlerinfra.BaseTest;
50 import org.onap.so.apihandlerinfra.exceptions.ApiException;
51 import org.onap.so.apihandlerinfra.exceptions.ValidateException;
52 import org.onap.so.apihandlerinfra.tenantisolationbeans.Action;
53 import org.springframework.boot.test.context.SpringBootTest;
54 import org.springframework.test.context.ActiveProfiles;
57 public class ModelDistributionRequestTest extends BaseTest{
59 private static final String requestJSON = "{\"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), anyString());
109 Response response = request.updateModelDistributionStatus(requestJSON, null, null);
111 assertEquals(200, response.getStatus());