2  * ============LICENSE_START=======================================================
 
   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
 
  11  *      http://www.apache.org/licenses/LICENSE-2.0
 
  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=========================================================
 
  21 package org.onap.so.adapters.vevnfm.service;
 
  23 import static org.junit.Assert.assertEquals;
 
  24 import static org.onap.so.adapters.vevnfm.service.SubscribeSender.SLASH;
 
  25 import static org.springframework.http.HttpHeaders.CONTENT_TYPE;
 
  26 import static org.springframework.test.web.client.ExpectedCount.once;
 
  27 import static org.springframework.test.web.client.match.MockRestRequestMatchers.*;
 
  28 import static org.springframework.test.web.client.response.MockRestResponseCreators.withStatus;
 
  29 import com.google.gson.Gson;
 
  30 import com.google.gson.GsonBuilder;
 
  31 import org.hamcrest.CoreMatchers;
 
  32 import org.junit.Before;
 
  33 import org.junit.Test;
 
  34 import org.junit.runner.RunWith;
 
  35 import org.onap.aai.domain.yang.EsrSystemInfo;
 
  36 import org.onap.so.adapters.vevnfm.configuration.ConfigProperties;
 
  37 import org.onap.so.adapters.vevnfm.configuration.StartupConfiguration;
 
  38 import org.onap.so.adapters.vevnfm.exception.VeVnfmException;
 
  39 import org.onap.so.adapters.etsi.sol003.adapter.lcm.extclients.vnfm.model.LccnSubscriptionRequest;
 
  40 import org.springframework.beans.factory.annotation.Autowired;
 
  41 import org.springframework.boot.test.context.SpringBootTest;
 
  42 import org.springframework.http.HttpMethod;
 
  43 import org.springframework.http.HttpStatus;
 
  44 import org.springframework.http.MediaType;
 
  45 import org.springframework.test.context.ActiveProfiles;
 
  46 import org.springframework.test.context.junit4.SpringRunner;
 
  47 import org.springframework.test.web.client.MockRestServiceServer;
 
  48 import org.springframework.web.client.RestTemplate;
 
  51 @RunWith(SpringRunner.class)
 
  52 @ActiveProfiles(StartupConfiguration.TEST_PROFILE)
 
  53 public class SubscribeSenderTest {
 
  55     private static final String URL = "lh";
 
  56     private static final String ID = "1a2s3d4f";
 
  57     private static final String JSON = "{\"id\":\"" + ID + "\"}";
 
  59     private static final Gson GSON;
 
  62         final GsonBuilder builder = new GsonBuilder();
 
  63         builder.serializeNulls();
 
  64         GSON = builder.create();
 
  68     private ConfigProperties configProperties;
 
  71     private SubscribeSender sender;
 
  74     private RestTemplate restTemplate;
 
  76     private String vnfmSubscription;
 
  77     private MockRestServiceServer mockRestServer;
 
  81         vnfmSubscription = configProperties.getVnfmSubscription();
 
  82         mockRestServer = MockRestServiceServer.bindTo(restTemplate).build();
 
  86     public void testSuccess() throws VeVnfmException {
 
  88         final EsrSystemInfo info = new EsrSystemInfo();
 
  89         info.setServiceUrl(URL);
 
  90         final LccnSubscriptionRequest request = new LccnSubscriptionRequest();
 
  92         mockRestServer.expect(once(), requestTo(SLASH + info.getServiceUrl() + vnfmSubscription))
 
  93                 .andExpect(header(CONTENT_TYPE, CoreMatchers.containsString(MediaType.APPLICATION_JSON_VALUE)))
 
  94                 .andExpect(method(HttpMethod.POST)).andExpect(content().json(GSON.toJson(request)))
 
  95                 .andRespond(withStatus(HttpStatus.CREATED).body(JSON).contentType(MediaType.APPLICATION_JSON));
 
  98         final String id = sender.send(info, request);
 
 101         mockRestServer.verify();
 
 102         assertEquals(ID, id);