974e6ec5446dc96679aa2a347b81af9aeef1a277
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SO
4  * ================================================================================
5  * Copyright (C) 2019 Samsung. 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.adapters.vevnfm.controller;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.springframework.test.web.client.ExpectedCount.once;
25 import static org.springframework.test.web.client.match.MockRestRequestMatchers.anything;
26 import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.onap.so.adapters.vevnfm.configuration.StartupConfiguration;
31 import org.springframework.beans.factory.annotation.Autowired;
32 import org.springframework.beans.factory.annotation.Value;
33 import org.springframework.boot.test.context.SpringBootTest;
34 import org.springframework.http.HttpStatus;
35 import org.springframework.http.MediaType;
36 import org.springframework.mock.web.MockHttpServletResponse;
37 import org.springframework.test.context.ActiveProfiles;
38 import org.springframework.test.context.junit4.SpringRunner;
39 import org.springframework.test.web.client.MockRestServiceServer;
40 import org.springframework.test.web.servlet.MockMvc;
41 import org.springframework.test.web.servlet.MvcResult;
42 import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
43 import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
44 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
45 import org.springframework.web.client.RestTemplate;
46 import org.springframework.web.context.WebApplicationContext;
47
48 @SpringBootTest
49 @RunWith(SpringRunner.class)
50 @ActiveProfiles(StartupConfiguration.TEST_PROFILE)
51 public class NotificationControllerTest {
52
53     private static final String MINIMAL_JSON_CONTENT = "{}";
54     private static final int ZERO = 0;
55
56     @Value("${vnfm.notification}")
57     private String notification;
58
59     @Autowired
60     private WebApplicationContext webApplicationContext;
61
62     @Autowired
63     private RestTemplate restTemplate;
64
65     private MockMvc mvc;
66     private MockRestServiceServer mockRestServer;
67
68     @Before
69     public void init() {
70         mvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
71         mockRestServer = MockRestServiceServer.bindTo(restTemplate).build();
72     }
73
74     @Test
75     public void testReceiveNotification() throws Exception {
76         // given
77         final MockHttpServletRequestBuilder request = MockMvcRequestBuilders.post(notification)
78                 .contentType(MediaType.APPLICATION_JSON).content(MINIMAL_JSON_CONTENT);
79
80         mockRestServer.expect(once(), anything()).andRespond(withSuccess());
81
82         // when
83         final MvcResult mvcResult = mvc.perform(request).andReturn();
84
85         // then
86         final MockHttpServletResponse response = mvcResult.getResponse();
87         assertEquals(HttpStatus.OK.value(), response.getStatus());
88         assertEquals(ZERO, response.getContentLength());
89         mockRestServer.verify();
90     }
91 }