Merge "moved object declarations from class level"
[so.git] / adapters / mso-vnfm-adapter / mso-vnfm-etsi-adapter / src / test / java / org / onap / so / adapters / vnfmadapter / rest / VnfmAdapterControllerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.adapters.vnfmadapter.rest;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import java.net.URI;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.onap.so.adapters.vnfmadapter.VnfmAdapterApplication;
29 import org.onap.vnfmadapter.v1.model.CreateVnfRequest;
30 import org.onap.vnfmadapter.v1.model.CreateVnfResponse;
31 import org.springframework.boot.test.context.SpringBootTest;
32 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
33 import org.springframework.boot.test.web.client.TestRestTemplate;
34 import org.springframework.boot.web.server.LocalServerPort;
35 import org.springframework.http.MediaType;
36 import org.springframework.http.RequestEntity;
37 import org.springframework.http.ResponseEntity;
38 import org.springframework.test.context.ActiveProfiles;
39 import org.springframework.test.context.junit4.SpringRunner;
40
41 @RunWith(SpringRunner.class)
42 @SpringBootTest(classes = VnfmAdapterApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT)
43 @ActiveProfiles("test")
44 public class VnfmAdapterControllerTest {
45
46     @LocalServerPort
47     private int port;
48
49     private final TestRestTemplate restTemplate = new TestRestTemplate("test", "test");
50
51     @Test
52     public void createVnf_ValidRequest_Returns202AndJobId() throws Exception {
53         final CreateVnfRequest createVnfRequest = new CreateVnfRequest();
54         final RequestEntity<CreateVnfRequest> request =
55                 RequestEntity.post(new URI("http://localhost:" + port + "/so/vnfm-adapter/v1/vnfs/myVnfId"))
56                         .accept(MediaType.APPLICATION_JSON).contentType(MediaType.APPLICATION_JSON)
57                         .header("X-ONAP-RequestId", "myRequestId").header("X-ONAP-InvocationID", "myInvocationId")
58                         .body(createVnfRequest);
59         final ResponseEntity<CreateVnfResponse> response = restTemplate.exchange(request, CreateVnfResponse.class);
60         assertEquals(202, response.getStatusCode().value());
61         assertNotNull(response.getBody().getJobId());
62     }
63
64     @Test
65     public void createVnf_UnauthorizedUser_Returns401() throws Exception {
66         final TestRestTemplate restTemplateWrongPassword = new TestRestTemplate("test", "wrongPassword");
67         final CreateVnfRequest createVnfRequest = new CreateVnfRequest();
68         final RequestEntity<CreateVnfRequest> request =
69                 RequestEntity.post(new URI("http://localhost:" + port + "/so/vnfm-adapter/v1/vnfs/myVnfId"))
70                         .accept(MediaType.APPLICATION_JSON).contentType(MediaType.APPLICATION_JSON)
71                         .header("X-ONAP-RequestId", "myRequestId").header("X-ONAP-InvocationID", "myInvocationId")
72                         .body(createVnfRequest);
73         final ResponseEntity<CreateVnfResponse> response =
74                 restTemplateWrongPassword.exchange(request, CreateVnfResponse.class);
75         assertEquals(401, response.getStatusCode().value());
76     }
77
78 }