4e5cfaba4892362379bbb82df82966a8ada07298
[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
32 import java.io.IOException;
33
34 import javax.inject.Provider;
35 import javax.ws.rs.core.MediaType;
36 import javax.ws.rs.core.Response;
37
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;
58
59 import com.fasterxml.jackson.databind.ObjectMapper;
60
61 public class ModelDistributionRequestTest extends BaseTest{
62
63         private static final String requestJSON = "{\"status\": \"DISTRIBUTION_COMPLETE_ERROR\", \"errorReason\": \"Distribution failed in AAI\" }";
64
65     @Rule
66     public ExpectedException thrown = ExpectedException.none();
67         @Mock
68         private Provider<TenantIsolationRunnable> thread;
69         @InjectMocks
70         @Spy
71         private ModelDistributionRequest request = new ModelDistributionRequest();
72         @Mock
73         private TenantIsolationRunnable runnable = new TenantIsolationRunnable();
74         
75         @Before
76         public void beforeTest() {
77                 Mockito.when(thread.get()).thenReturn(runnable);
78         }
79         
80         @Test
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);
87         }
88         
89         @Test
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);
97         }
98         
99         @Test
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);
107         }
108         
109         @Test
110         public void testSuccess() throws ApiException{
111                 doNothing().when(runnable).run(any(Action.class), anyString(), any(CloudOrchestrationRequest.class), anyString());
112                 
113                 Response response = request.updateModelDistributionStatus(requestJSON, null, null);
114                 
115                 assertEquals(200, response.getStatus());
116         }
117
118         @Test
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);
127         
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());
133                 
134         }
135         
136 }