Merge "Use constants for magic numbers in perf tests"
[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 import com.fasterxml.jackson.core.JsonProcessingException
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 #type.'() {
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 = type == 'String' ? jsonObjectMapper.asJsonString(object) : jsonObjectMapper.asJsonBytes(object)
41         then: 'the result is a valid json string (can be parsed)'
42             def contentMap = new JsonSlurper().parseText(new String(content))
43         and: 'the parsed content is as expected'
44             assert contentMap.'test:bookstore'.'bookstore-name' == 'Chapters/Easons'
45         where: 'the following data stores are used'
46             type << ['String', 'bytes']
47     }
48
49     def 'Convert to bytes with processing exception.'() {
50         given: 'the object mapper throws an processing exception'
51             spiedObjectMapper.writeValueAsBytes(_) >> { throw new JsonProcessingException('message from cause')}
52         when: 'attempt to convert an object to bytes'
53             jsonObjectMapper.asJsonBytes('does not matter')
54         then: 'a data validation exception is thrown with the original exception message as details'
55             def thrown = thrown(DataValidationException)
56             assert thrown.details == 'message from cause'
57     }
58
59     def 'Map a structured object to json String error.'() {
60         given: 'some object'
61             def object = new Object()
62         and: 'the Object mapper throws an exception'
63             spiedObjectMapper.writeValueAsString(object) >> { throw new JsonProcessingException('Sample problem'){} }
64         when: 'attempting to convert the object to a string'
65             jsonObjectMapper.asJsonString(object)
66         then: 'a Data Validation Exception is thrown'
67             def thrown = thrown(DataValidationException)
68         and: 'the details containing the original error message'
69             assert thrown.details == 'Sample problem'
70     }
71
72     def 'Map a structurally compatible object to class object of specific class type T.'() {
73         given: 'a map object model'
74             def contentMap = new JsonSlurper().parseText(TestUtils.getResourceFileContent('bookstore.json'))
75         when: 'converted into a Map'
76             def result = jsonObjectMapper.convertToValueType(contentMap, Map)
77         then: 'the result is a mapped into class of type Map'
78             assert result instanceof Map
79         and: 'the map contains the expected key'
80             assert result.containsKey('test:bookstore')
81             assert result.'test:bookstore'.categories[0].name == 'SciFi'
82     }
83
84     def 'Mapping a valid json string to class object of specific class type T.'() {
85         given: 'a json string representing a map'
86             def content = '{"key":"value"}'
87         expect: 'the string is converted correctly to a map'
88             jsonObjectMapper.convertJsonString(content, Map) == [ key: 'value' ]
89     }
90
91     def 'Mapping an unstructured json string to class object of specific class type T.'() {
92         given: 'Unstructured json string'
93             def content = '{invalid json'
94         when: 'mapping json string to given class type'
95             jsonObjectMapper.convertJsonString(content, Map)
96         then: 'a data validation exception is thrown'
97             thrown(DataValidationException)
98     }
99
100     def 'Map an incompatible object to class object of specific class type T.'() {
101         given: 'a map object model'
102             def contentMap = new JsonSlurper().parseText(TestUtils.getResourceFileContent('bookstore.json'))
103         and: 'Object mapper throws an exception'
104             spiedObjectMapper.convertValue(*_) >> { throw new IllegalArgumentException() }
105         when: 'converted into specific class type'
106             jsonObjectMapper.convertToValueType(contentMap, Object)
107         then: 'an exception is thrown'
108             thrown(DataValidationException)
109     }
110
111     def 'Map a unstructured object to json String.'() {
112         given: 'Unstructured object'
113             def object = new Object()
114         and: 'disable serialization failure on empty bean'
115             spiedObjectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS)
116         when: 'the object is mapped to string'
117             jsonObjectMapper.asJsonString(object)
118         then: 'no exception is thrown'
119             noExceptionThrown()
120     }
121
122     def 'Map a structurally compatible json String to JsonNode.'() {
123         given: 'Unstructured object'
124             def content = '{ "nest": { "birds": "bird" } }'
125         when: 'the object is mapped to string'
126             def result = jsonObjectMapper.convertToJsonNode(content)
127         then: 'the result is a valid JsonNode'
128             result.fieldNames().next() == 'nest'
129     }
130
131     def 'Map a unstructured json String to JsonNode.'() {
132         given: 'Unstructured object'
133             def content = '{ "nest": { "birds": "bird" }] }'
134         when: 'the object is mapped to string'
135             jsonObjectMapper.convertToJsonNode(content)
136         then: 'a data validation exception is thrown'
137             thrown(DataValidationException)
138     }
139 }