Merge "Allow Module Re-Sync"
[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 com.fasterxml.jackson.databind.SerializationFeature
26 import groovy.json.JsonSlurper
27 import org.onap.cps.TestUtils
28 import org.onap.cps.spi.exceptions.DataValidationException
29 import spock.lang.Specification
30
31 class JsonObjectMapperSpec extends Specification {
32
33     def spiedObjectMapper = Spy(ObjectMapper)
34     def jsonObjectMapper = new JsonObjectMapper(spiedObjectMapper)
35
36     def 'Map a structured object to json String.'() {
37         given: 'an object model'
38             def object = spiedObjectMapper.readValue(TestUtils.getResourceFileContent('bookstore.json'), Object)
39         when: 'the object is mapped to string'
40             def content = jsonObjectMapper.asJsonString(object);
41         then: 'the result is a valid json string (can be parsed)'
42             def contentMap = new JsonSlurper().parseText(content)
43         and: 'the parsed content is as expected'
44             assert contentMap.'test:bookstore'.'bookstore-name' == 'Chapters'
45     }
46
47     def 'Map a structurally compatible object to class object of specific class type T.'() {
48         given: 'a map object model'
49             def contentMap = new JsonSlurper().parseText(TestUtils.getResourceFileContent('bookstore.json'))
50         when: 'converted into a Map'
51             def result = jsonObjectMapper.convertToValueType(contentMap, Map);
52         then: 'the result is a mapped into class of type Map'
53             assert result instanceof Map
54         and: 'the map contains the expected key'
55             assert result.containsKey('test:bookstore')
56             assert result.'test:bookstore'.categories[0].name == 'SciFi'
57
58     }
59
60     def 'Mapping an unstructured json string to class object of specific class type T.'() {
61         given: 'Unstructured json string'
62             def content = '{ "nest": { "birds": "bird"] } }'
63         when: 'mapping json string to given class type'
64             def contentMap = jsonObjectMapper.convertJsonString(content, Map);
65         then: 'an exception is thrown'
66             thrown(DataValidationException)
67     }
68
69     def 'Map an incompatible object to class object of specific class type T.'() {
70         given: 'a map object model'
71             def contentMap = new JsonSlurper().parseText(TestUtils.getResourceFileContent('bookstore.json'))
72         and: 'Object mapper throws an exception'
73             spiedObjectMapper.convertValue(*_) >> { throw new IllegalArgumentException() }
74         when: 'converted into specific class type'
75             jsonObjectMapper.convertToValueType(contentMap, Object);
76         then: 'an exception is thrown'
77             thrown(DataValidationException)
78     }
79
80     def 'Map a unstructured object to json String.'() {
81         given: 'Unstructured object'
82             def object = new Object()
83         and: 'disable serialization failure on empty bean'
84             spiedObjectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
85         when: 'the object is mapped to string'
86             jsonObjectMapper.asJsonString(object);
87         then: 'no exception is thrown'
88             noExceptionThrown()
89     }
90
91     def 'Map a structurally compatible json String to JsonNode.'() {
92         given: 'Unstructured object'
93             def content = '{ "nest": { "birds": "bird" } }'
94         when: 'the object is mapped to string'
95             def result = jsonObjectMapper.convertToJsonNode(content);
96         then: 'the result is a valid JsonNode'
97             result.fieldNames().next() == "nest"
98     }
99
100     def 'Map a unstructured json String to JsonNode.'() {
101         given: 'Unstructured object'
102             def content = '{ "nest": { "birds": "bird" }] }'
103         when: 'the object is mapped to string'
104             jsonObjectMapper.convertToJsonNode(content);
105         then: 'a data validation exception is thrown'
106             thrown(DataValidationException)
107     }
108 }