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 'Map a structured object to json String error.'() {
50         given: 'some object'
51             def object = new Object()
52         and: 'the Object mapper throws an exception'
53             spiedObjectMapper.writeValueAsString(object) >> { throw new JsonProcessingException('Sample problem'){} }
54         when: 'attempting to convert the object to a string'
55             jsonObjectMapper.asJsonString(object);
56         then: 'a Data Validation Exception is thrown'
57             def thrown = thrown(DataValidationException)
58         and: 'the details containing the original error message'
59             assert thrown.details == 'Sample problem'
60     }
61
62     def 'Map a structurally compatible object to class object of specific class type T.'() {
63         given: 'a map object model'
64             def contentMap = new JsonSlurper().parseText(TestUtils.getResourceFileContent('bookstore.json'))
65         when: 'converted into a Map'
66             def result = jsonObjectMapper.convertToValueType(contentMap, Map);
67         then: 'the result is a mapped into class of type Map'
68             assert result instanceof Map
69         and: 'the map contains the expected key'
70             assert result.containsKey('test:bookstore')
71             assert result.'test:bookstore'.categories[0].name == 'SciFi'
72
73     }
74
75     def 'Mapping an unstructured json string to class object of specific class type T.'() {
76         given: 'Unstructured json string'
77             def content = '{ "nest": { "birds": "bird"] } }'
78         when: 'mapping json string to given class type'
79             jsonObjectMapper.convertJsonString(content, Map);
80         then: 'an exception is thrown'
81             thrown(DataValidationException)
82     }
83
84     def 'Map an incompatible object to class object of specific class type T.'() {
85         given: 'a map object model'
86             def contentMap = new JsonSlurper().parseText(TestUtils.getResourceFileContent('bookstore.json'))
87         and: 'Object mapper throws an exception'
88             spiedObjectMapper.convertValue(*_) >> { throw new IllegalArgumentException() }
89         when: 'converted into specific class type'
90             jsonObjectMapper.convertToValueType(contentMap, Object);
91         then: 'an exception is thrown'
92             thrown(DataValidationException)
93     }
94
95     def 'Map a unstructured object to json String.'() {
96         given: 'Unstructured object'
97             def object = new Object()
98         and: 'disable serialization failure on empty bean'
99             spiedObjectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
100         when: 'the object is mapped to string'
101             jsonObjectMapper.asJsonString(object);
102         then: 'no exception is thrown'
103             noExceptionThrown()
104     }
105
106     def 'Map a structurally compatible json String to JsonNode.'() {
107         given: 'Unstructured object'
108             def content = '{ "nest": { "birds": "bird" } }'
109         when: 'the object is mapped to string'
110             def result = jsonObjectMapper.convertToJsonNode(content);
111         then: 'the result is a valid JsonNode'
112             result.fieldNames().next() == "nest"
113     }
114
115     def 'Map a unstructured json String to JsonNode.'() {
116         given: 'Unstructured object'
117             def content = '{ "nest": { "birds": "bird" }] }'
118         when: 'the object is mapped to string'
119             jsonObjectMapper.convertToJsonNode(content);
120         then: 'a data validation exception is thrown'
121             thrown(DataValidationException)
122     }
123 }