2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2024 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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.utils
23 import org.onap.cps.spi.exceptions.DataValidationException
24 import org.onap.cps.spi.model.Anchor
25 import org.onap.cps.yang.YangTextSchemaSourceSet
26 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode
27 import org.opendaylight.yangtools.yang.model.api.SchemaContext
28 import spock.lang.Specification
29 import org.onap.cps.api.impl.YangTextSchemaSourceSetCache
31 class YangParserSpec extends Specification {
33 def mockYangParserHelper = Mock(YangParserHelper)
34 def mockYangTextSchemaSourceSetCache = Mock(YangTextSchemaSourceSetCache)
36 def objectUnderTest = new YangParser(mockYangParserHelper, mockYangTextSchemaSourceSetCache)
38 def anchor = new Anchor(dataspaceName: 'my dataspace', schemaSetName: 'my schema')
39 def mockYangTextSchemaSourceSet = Mock(YangTextSchemaSourceSet)
40 def mockSchemaContext = Mock(SchemaContext)
41 def containerNodeFromYangUtils = Mock(ContainerNode)
46 mockYangTextSchemaSourceSetCache.get('my dataspace', 'my schema') >> mockYangTextSchemaSourceSet
47 mockYangTextSchemaSourceSet.getSchemaContext() >> mockSchemaContext
50 def 'Parsing data.'() {
51 given: 'the yang parser (utility) always returns a container node'
52 mockYangParserHelper.parseData(ContentType.JSON, 'some json', mockSchemaContext, noParent) >> containerNodeFromYangUtils
53 when: 'parsing some json data'
54 def result = objectUnderTest.parseData(ContentType.JSON, 'some json', anchor, noParent)
55 then: 'the schema source set for the correct dataspace and schema set is retrieved form the cache'
56 1 * mockYangTextSchemaSourceSetCache.get('my dataspace', 'my schema') >> mockYangTextSchemaSourceSet
57 and: 'the result is the same container node as return from yang utils'
58 assert result == containerNodeFromYangUtils
59 and: 'nothing is removed from the cache'
60 0 * mockYangTextSchemaSourceSetCache.removeFromCache(*_)
63 def 'Parsing data with exception on first attempt.'() {
64 given: 'the yang parser throws an exception on the first attempt only'
65 mockYangParserHelper.parseData(ContentType.JSON, 'some json', mockSchemaContext, noParent) >> { throw new DataValidationException(noParent, noParent) } >> containerNodeFromYangUtils
66 when: 'attempt to parse some data'
67 def result = objectUnderTest.parseData(ContentType.JSON, 'some json', anchor, noParent)
68 then: 'the cache is cleared for the correct dataspace and schema'
69 1 * mockYangTextSchemaSourceSetCache.removeFromCache('my dataspace', 'my schema')
70 and: 'the result is the same container node as return from yang utils (no exception thrown!)'
71 assert result == containerNodeFromYangUtils
74 def 'Parsing data with exception on all attempts.'() {
75 given: 'the yang parser always throws an exception'
76 mockYangParserHelper.parseData(ContentType.JSON, 'some json', mockSchemaContext, noParent) >> { throw new DataValidationException(noParent, noParent) }
77 when: 'attempt to parse some data'
78 objectUnderTest.parseData(ContentType.JSON, 'some json', anchor, noParent)
79 then: 'a data validation exception is thrown'
80 thrown(DataValidationException)
81 and: 'the cache is cleared for the correct dataspace and schema (but that did not help)'
82 1 * mockYangTextSchemaSourceSetCache.removeFromCache('my dataspace', 'my schema')