d1fda0eee6ae2a71c432ea51c08a147c816d124d
[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.aai.domain.yang.EsrSystemInfo;
35 import org.onap.so.adapters.vevnfm.configuration.StartupConfiguration;
36 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.LccnSubscriptionRequest;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.beans.factory.annotation.Value;
39 import org.springframework.boot.test.context.SpringBootTest;
40 import org.springframework.http.HttpMethod;
41 import org.springframework.http.HttpStatus;
42 import org.springframework.http.MediaType;
43 import org.springframework.test.context.ActiveProfiles;
44 import org.springframework.test.context.junit4.SpringRunner;
45 import org.springframework.test.web.client.MockRestServiceServer;
46 import org.springframework.web.client.RestTemplate;
47
48 @SpringBootTest
49 @RunWith(SpringRunner.class)
50 @ActiveProfiles(StartupConfiguration.TEST_PROFILE)
51 public class SubscribeSenderTest {
52
53     private static final String SLASH = "/";
54     private static final String MINIMAL_JSON_CONTENT = "{}";
55
56     private static final Gson GSON;
57
58     static {
59         final GsonBuilder builder = new GsonBuilder();
60         builder.serializeNulls();
61         GSON = builder.create();
62     }
63
64     @Value("${vnfm.subscription}")
65     private String vnfmSubscription;
66
67     @Autowired
68     private SubscribeSender sender;
69
70     @Autowired
71     private RestTemplate restTemplate;
72
73     private MockRestServiceServer mockRestServer;
74
75     @Before
76     public void init() {
77         mockRestServer = MockRestServiceServer.bindTo(restTemplate).build();
78     }
79
80     @Test
81     public void testSuccess() {
82         // given
83         final EsrSystemInfo info = new EsrSystemInfo();
84         info.setServiceUrl("lh");
85         final LccnSubscriptionRequest request = new LccnSubscriptionRequest();
86
87         mockRestServer.expect(once(), requestTo(SLASH + info.getServiceUrl() + vnfmSubscription))
88                 .andExpect(header(CONTENT_TYPE, CoreMatchers.containsString(MediaType.APPLICATION_JSON_VALUE)))
89                 .andExpect(method(HttpMethod.POST)).andExpect(content().json(GSON.toJson(request)))
90                 .andRespond(withStatus(HttpStatus.CREATED).body(MINIMAL_JSON_CONTENT));
91
92         // when
93         final boolean done = sender.send(info, request);
94
95         // then
96         assertTrue(done);
97         mockRestServer.verify();
98     }
99 }