NcmpCloudEventBuilder refactoring
[cps.git] / cps-ncmp-service / src / test / groovy / org / onap / cps / ncmp / api / impl / config / NcmpConfigurationSpec.groovy
1 /*
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
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20 package org.onap.cps.ncmp.api.impl.config
21
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
32
33 @SpringBootTest
34 @ContextConfiguration(classes = [NcmpConfiguration.DmiProperties, HttpClientConfiguration])
35 class NcmpConfigurationSpec extends Specification{
36
37     @Autowired
38     NcmpConfiguration.DmiProperties dmiProperties
39     
40     @Autowired
41     HttpClientConfiguration httpClientConfiguration
42     
43     def mockRestTemplateBuilder = new RestTemplateBuilder()
44
45     def 'NcmpConfiguration Construction.'() {
46         expect: 'the system can create an instance'
47              new NcmpConfiguration() != null
48     }
49
50     def 'DMI Properties.'() {
51         expect: 'properties are set to values in test configuration yaml file'
52             dmiProperties.authUsername == 'some-user'
53             dmiProperties.authPassword == 'some-password'
54     }
55
56     def 'Rest Template creation with CloseableHttpClient and MappingJackson2HttpMessageConverter.'() {
57         when: 'a rest template is created'
58             def result = NcmpConfiguration.restTemplate(mockRestTemplateBuilder, httpClientConfiguration)
59         then: 'the rest template is returned'
60             assert result instanceof RestTemplate
61         and: 'the rest template is created with httpclient5'
62             assert result.getRequestFactory() instanceof HttpComponentsClientHttpRequestFactory
63             assert ((HttpComponentsClientHttpRequestFactory) result.getRequestFactory()).getHttpClient() instanceof CloseableHttpClient;
64         and: 'a jackson media converter has been added'
65             def lastMessageConverter = result.getMessageConverters().get(result.getMessageConverters().size()-1)
66             lastMessageConverter instanceof MappingJackson2HttpMessageConverter
67         and: 'the jackson media converters supports the expected media types'
68             lastMessageConverter.getSupportedMediaTypes() == [MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN];
69     }
70 }