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