071a330e8b6222cf21c8d2b4112cb6a30ed53001
[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.onap.vnfmadapter.v1.model.DeleteVnfResponse;
32 import org.springframework.boot.test.context.SpringBootTest;
33 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
34 import org.springframework.boot.test.web.client.TestRestTemplate;
35 import org.springframework.boot.web.server.LocalServerPort;
36 import org.springframework.http.MediaType;
37 import org.springframework.http.RequestEntity;
38 import org.springframework.http.ResponseEntity;
39 import org.springframework.test.context.ActiveProfiles;
40 import org.springframework.test.context.junit4.SpringRunner;
41
42 @RunWith(SpringRunner.class)
43 @SpringBootTest(classes = VnfmAdapterApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT)
44 @ActiveProfiles("test")
45 public class VnfmAdapterControllerTest {
46
47     @LocalServerPort
48     private int port;
49
50     private final TestRestTemplate restTemplate = new TestRestTemplate("test", "test");
51
52     @Test
53     public void createVnf_ValidRequest_Returns202AndJobId() throws Exception {
54         final CreateVnfRequest createVnfRequest = new CreateVnfRequest();
55         final RequestEntity<CreateVnfRequest> request =
56                 RequestEntity.post(new URI("http://localhost:" + port + "/so/vnfm-adapter/v1/vnfs/myVnfId"))
57                         .accept(MediaType.APPLICATION_JSON).contentType(MediaType.APPLICATION_JSON)
58                         .header("X-ONAP-RequestId", "myRequestId").header("X-ONAP-InvocationID", "myInvocationId")
59                         .body(createVnfRequest);
60         final ResponseEntity<CreateVnfResponse> response = restTemplate.exchange(request, CreateVnfResponse.class);
61         assertEquals(202, response.getStatusCode().value());
62         assertNotNull(response.getBody().getJobId());
63     }
64
65     @Test
66     public void createVnf_UnauthorizedUser_Returns401() throws Exception {
67         final TestRestTemplate restTemplateWrongPassword = new TestRestTemplate("test", "wrongPassword");
68         final CreateVnfRequest createVnfRequest = new CreateVnfRequest();
69         final RequestEntity<CreateVnfRequest> request =
70                 RequestEntity.post(new URI("http://localhost:" + port + "/so/vnfm-adapter/v1/vnfs/myVnfId"))
71                         .accept(MediaType.APPLICATION_JSON).contentType(MediaType.APPLICATION_JSON)
72                         .header("X-ONAP-RequestId", "myRequestId").header("X-ONAP-InvocationID", "myInvocationId")
73                         .body(createVnfRequest);
74         final ResponseEntity<CreateVnfResponse> response =
75                 restTemplateWrongPassword.exchange(request, CreateVnfResponse.class);
76         assertEquals(401, response.getStatusCode().value());
77     }
78
79     @Test
80     public void deleteVnf_ValidRequest_Returns202AndJobId() throws Exception {
81         final RequestEntity<Void> request = RequestEntity
82                 .delete(new URI("http://localhost:" + port + "/so/vnfm-adapter/v1/vnfs/myVnfId"))
83                 .accept(MediaType.APPLICATION_JSON).header("X-ONAP-RequestId", "myRequestId")
84                 .header("X-ONAP-InvocationID", "myInvocationId").header("Content-Type", "application/json").build();
85         final ResponseEntity<DeleteVnfResponse> response = restTemplate.exchange(request, DeleteVnfResponse.class);
86         assertEquals(202, response.getStatusCode().value());
87         assertNotNull(response.getBody().getJobId());
88     }
89
90 }