7893b81d8e6606ee720c985171711b0709ab03ad
[so.git] /
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.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;
55
56 public class ModelDistributionRequestTest extends BaseTest {
57
58     private static final String requestJSON =
59             "{\"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),
108                 anyString());
109
110         Response response = request.updateModelDistributionStatus(requestJSON, null, null);
111
112         assertEquals(200, response.getStatus());
113     }
114
115     @Test
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);
124
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());
129
130     }
131
132 }