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