Replace gson mapper with jackson mapper
[cps.git] / cps-service / src / test / groovy / org / onap / cps / utils / JsonObjectMapperSpec.groovy
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2022 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
21 package org.onap.cps.utils
22
23
24 import com.fasterxml.jackson.databind.ObjectMapper
25 import groovy.json.JsonSlurper
26 import org.onap.cps.TestUtils
27 import org.onap.cps.spi.exceptions.DataValidationException
28 import spock.lang.Specification
29
30 class JsonObjectMapperSpec extends Specification {
31
32     def spiedObjectMapper = Spy(ObjectMapper)
33     def jsonObjectMapper = new JsonObjectMapper(spiedObjectMapper)
34
35     def 'Map a structured object to json String.'() {
36         given: 'an object model'
37             def object = spiedObjectMapper.readValue(TestUtils.getResourceFileContent('bookstore.json'), Object)
38         when: 'the object is mapped to string'
39             def content = jsonObjectMapper.asJsonString(object);
40         then: 'the result is a valid json string (can be parsed)'
41             def contentMap = new JsonSlurper().parseText(content)
42         and: 'the parsed content is as expected'
43             assert contentMap.'test:bookstore'.'bookstore-name' == 'Chapters'
44     }
45
46     def 'Map a structurally compatible object to class object of specific class type T.'() {
47         given: 'a map object model'
48             def contentMap = new JsonSlurper().parseText(TestUtils.getResourceFileContent('bookstore.json'))
49         when: 'converted into a Map'
50             def result = jsonObjectMapper.convertToValueType(contentMap, Map);
51         then: 'the result is a mapped into class of type Map'
52             assert result instanceof Map
53         and: 'the map contains the expected key'
54             assert result.containsKey('test:bookstore')
55             assert result.'test:bookstore'.categories[0].name == 'SciFi'
56
57     }
58
59     def 'Mapping an unstructured json string to class object of specific class type T.'() {
60         given: 'Unstructured json string'
61             def content = '{ "nest": { "birds": "bird"] } }'
62         when: 'mapping json string to given class type'
63             def contentMap = jsonObjectMapper.convertJsonString(content, Map);
64         then: 'an exception is thrown'
65             thrown(DataValidationException)
66     }
67
68     def 'Map an incompatible object to class object of specific class type T.'() {
69         given: 'a map object model'
70             def contentMap = new JsonSlurper().parseText(TestUtils.getResourceFileContent('bookstore.json'))
71         and: 'Object mapper throws an exception'
72             spiedObjectMapper.convertValue(*_) >> { throw new IllegalArgumentException() }
73         when: 'converted into specific class type'
74             jsonObjectMapper.convertToValueType(contentMap, Object);
75         then: 'an exception is thrown'
76             thrown(DataValidationException)
77     }
78
79     def 'Map a unstructured object to json String.'() {
80         given: 'Unstructured object'
81             def object = new Object()
82         when: 'the object is mapped to string'
83             jsonObjectMapper.asJsonString(object);
84         then: 'an exception is thrown'
85             thrown(DataValidationException)
86     }
87 }