62a624a9833ff06e01ca286453b4ee293be61948
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SO
4  * ================================================================================
5  * Copyright (C) 2020 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.subscription;
22
23 import static org.junit.Assert.assertTrue;
24 import static org.springframework.http.HttpHeaders.CONTENT_TYPE;
25 import static org.springframework.test.web.client.ExpectedCount.once;
26 import static org.springframework.test.web.client.match.MockRestRequestMatchers.*;
27 import static org.springframework.test.web.client.response.MockRestResponseCreators.withStatus;
28 import com.google.gson.Gson;
29 import com.google.gson.GsonBuilder;
30 import org.hamcrest.CoreMatchers;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.onap.so.adapters.vevnfm.configuration.StartupConfiguration;
35 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.LccnSubscriptionRequest;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.beans.factory.annotation.Value;
38 import org.springframework.boot.test.context.SpringBootTest;
39 import org.springframework.http.HttpMethod;
40 import org.springframework.http.HttpStatus;
41 import org.springframework.http.MediaType;
42 import org.springframework.test.context.ActiveProfiles;
43 import org.springframework.test.context.junit4.SpringRunner;
44 import org.springframework.test.web.client.MockRestServiceServer;
45 import org.springframework.web.client.RestTemplate;
46
47 @SpringBootTest
48 @RunWith(SpringRunner.class)
49 @ActiveProfiles(StartupConfiguration.TEST_PROFILE)
50 public class SubscribeSenderTest {
51
52     private static final String SLASH = "/";
53     private static final String MINIMAL_JSON_CONTENT = "{}";
54
55     private static final Gson GSON;
56
57     static {
58         final GsonBuilder builder = new GsonBuilder();
59         builder.serializeNulls();
60         GSON = builder.create();
61     }
62
63     @Value("${vnfm.subscription}")
64     private String vnfmSubscription;
65
66     @Autowired
67     private SubscribeSender sender;
68
69     @Autowired
70     private RestTemplate restTemplate;
71
72     private MockRestServiceServer mockRestServer;
73
74     @Before
75     public void init() {
76         mockRestServer = MockRestServiceServer.bindTo(restTemplate).build();
77     }
78
79     @Test
80     public void testSuccess() {
81         // given
82         final String endpoint = "lh";
83         final LccnSubscriptionRequest request = new LccnSubscriptionRequest();
84
85         mockRestServer.expect(once(), requestTo(SLASH + endpoint + vnfmSubscription))
86                 .andExpect(header(CONTENT_TYPE, CoreMatchers.containsString(MediaType.APPLICATION_JSON_VALUE)))
87                 .andExpect(method(HttpMethod.POST)).andExpect(content().json(GSON.toJson(request)))
88                 .andRespond(withStatus(HttpStatus.CREATED).body(MINIMAL_JSON_CONTENT));
89
90         // when
91         final boolean done = sender.send(endpoint, request);
92
93         // then
94         assertTrue(done);
95         mockRestServer.verify();
96     }
97 }