2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021-2023 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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
20 package org.onap.cps.ncmp.api.impl.config
22 import org.apache.hc.client5.http.impl.classic.CloseableHttpClient
23 import org.springframework.beans.factory.annotation.Autowired
24 import org.springframework.boot.test.context.SpringBootTest
25 import org.springframework.boot.web.client.RestTemplateBuilder
26 import org.springframework.http.MediaType
27 import org.springframework.http.client.HttpComponentsClientHttpRequestFactory
28 import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter
29 import org.springframework.test.context.ContextConfiguration
30 import org.springframework.web.client.RestTemplate
31 import spock.lang.Specification
34 @ContextConfiguration(classes = [ HttpClientConfiguration])
35 class NcmpConfigurationSpec extends Specification{
38 HttpClientConfiguration httpClientConfiguration
40 def mockRestTemplateBuilder = new RestTemplateBuilder()
42 def 'NcmpConfiguration Construction.'() {
43 expect: 'the system can create an instance'
44 new NcmpConfiguration() != null
47 def 'Rest Template creation with CloseableHttpClient and MappingJackson2HttpMessageConverter.'() {
48 when: 'a rest template is created'
49 def result = NcmpConfiguration.restTemplate(mockRestTemplateBuilder, httpClientConfiguration)
50 then: 'the rest template is returned'
51 assert result instanceof RestTemplate
52 and: 'the rest template is created with httpclient5'
53 assert result.getRequestFactory() instanceof HttpComponentsClientHttpRequestFactory
54 assert ((HttpComponentsClientHttpRequestFactory) result.getRequestFactory()).getHttpClient() instanceof CloseableHttpClient;
55 and: 'a jackson media converter has been added'
56 def lastMessageConverter = result.getMessageConverters().get(result.getMessageConverters().size()-1)
57 lastMessageConverter instanceof MappingJackson2HttpMessageConverter
58 and: 'the jackson media converters supports the expected media types'
59 lastMessageConverter.getSupportedMediaTypes() == [MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN];