37ec14a0b5a3252266f3aa088584b1662c0aaa09
[so.git] / mso-api-handlers / mso-api-handler-infra / src / test / java / org / onap / so / apihandlerinfra / tenantisolation / ModelDistributionRequestTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
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
21 package org.onap.so.apihandlerinfra.tenantisolation;
22
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.Matchers.any;
29 import static org.mockito.Matchers.anyString;
30 import static org.mockito.Matchers.contains;
31 import static org.mockito.Mockito.doNothing;
32
33 import javax.inject.Provider;
34 import javax.ws.rs.core.Response;
35
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.runners.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;
55
56
57 public class ModelDistributionRequestTest extends BaseTest{
58
59         private static final String requestJSON = "{\"status\": \"DISTRIBUTION_COMPLETE_ERROR\", \"errorReason\": \"Distribution failed in AAI\" }";
60
61     @Rule
62     public ExpectedException thrown = ExpectedException.none();
63         @Mock
64         private Provider<TenantIsolationRunnable> thread;
65         @InjectMocks
66         @Spy
67         private ModelDistributionRequest request = new ModelDistributionRequest();
68         @Mock
69         private TenantIsolationRunnable runnable = new TenantIsolationRunnable();
70         
71         @Before
72         public void beforeTest() {
73                 Mockito.when(thread.get()).thenReturn(runnable);
74         }
75         
76         @Test
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);
83         }
84         
85         @Test
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);
93         }
94         
95         @Test
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);
103         }
104         
105         @Test
106         public void testSuccess() throws ApiException{
107                 doNothing().when(runnable).run(any(Action.class), anyString(), any(CloudOrchestrationRequest.class), anyString());
108                 
109                 Response response = request.updateModelDistributionStatus(requestJSON, null, null);
110                 
111                 assertEquals(200, response.getStatus());
112         }
113
114 }