Replace gson mapper with jackson mapper
[cps.git] / cps-ri / src / test / groovy / org / onap / cps / spi / impl / CpsDataPersistenceServiceSpec.groovy
index 5ed3ae3..7166008 100644 (file)
 
 package org.onap.cps.spi.impl
 
+import com.fasterxml.jackson.databind.ObjectMapper
 import org.hibernate.StaleStateException
+import org.onap.cps.spi.FetchDescendantsOption
 import org.onap.cps.spi.entities.FragmentEntity
 import org.onap.cps.spi.exceptions.ConcurrencyException
+import org.onap.cps.spi.exceptions.DataValidationException
 import org.onap.cps.spi.model.DataNodeBuilder
 import org.onap.cps.spi.repository.AnchorRepository
 import org.onap.cps.spi.repository.DataspaceRepository
 import org.onap.cps.spi.repository.FragmentRepository
+import org.onap.cps.utils.JsonObjectMapper
 import spock.lang.Specification
 
 
@@ -33,9 +37,10 @@ class CpsDataPersistenceServiceSpec extends Specification {
     def mockDataspaceRepository = Mock(DataspaceRepository)
     def mockAnchorRepository = Mock(AnchorRepository)
     def mockFragmentRepository = Mock(FragmentRepository)
+    def jsonObjectMapper = new JsonObjectMapper(new ObjectMapper())
 
     def objectUnderTest = new CpsDataPersistenceServiceImpl(
-            mockDataspaceRepository, mockAnchorRepository, mockFragmentRepository)
+            mockDataspaceRepository, mockAnchorRepository, mockFragmentRepository, jsonObjectMapper)
 
     def 'Handling of StaleStateException (caused by concurrent updates) during data node tree update.'() {
 
@@ -68,5 +73,43 @@ class CpsDataPersistenceServiceSpec extends Specification {
             assert concurrencyException.getDetails().contains(parentXpath)
     }
 
+    def 'Retrieving a data node with a property JSON value of #scenario'() {
+        given: 'a fragment with a property JSON value of #scenario'
+            mockFragmentRepository.getByDataspaceAndAnchorAndXpath(_, _, _) >> {
+                new FragmentEntity(childFragments: Collections.emptySet(),
+                        attributes: "{\"some attribute\": ${dataString}}")
+            }
+        when: 'getting the data node represented by this fragment'
+            def dataNode = objectUnderTest.getDataNode('my-dataspace', 'my-anchor',
+                    'parent-01', FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS)
+        then: 'the leaf is of the correct value and data type'
+            def attributeValue = dataNode.leaves.get('some attribute')
+            assert attributeValue == expectedValue
+            assert attributeValue.class == expectedDataClass
+        where: 'the following Data Type is passed'
+            scenario                              | dataString            || expectedValue     | expectedDataClass
+            'just numbers'                        | '15174'               || 15174             | Integer
+            'number with dot'                     | '15174.32'            || 15174.32          | Double
+            'number with 0 value after dot'       | '15174.0'             || 15174.0           | Double
+            'number with 0 value before dot'      | '0.32'                || 0.32              | Double
+            'number higher than max int'          | '2147483648'          || 2147483648        | Long
+            'just text'                           | '"Test"'              || 'Test'            | String
+            'number with exponent'                | '1.2345e5'            || 1.2345e5          | Double
+            'number higher than max int with dot' | '123456789101112.0'   || 123456789101112.0 | Double
+            'text and numbers'                    | '"String = \'1234\'"' || "String = '1234'" | String
+            'number as String'                    | '"12345"'             || '12345'           | String
+    }
+
+    def 'Retrieving a data node with invalid JSON'() {
+        given: 'a fragment with invalid JSON'
+            mockFragmentRepository.getByDataspaceAndAnchorAndXpath(_, _, _) >> {
+                new FragmentEntity(childFragments: Collections.emptySet(), attributes: '{invalid json')
+            }
+        when: 'getting the data node represented by this fragment'
+            def dataNode = objectUnderTest.getDataNode('my-dataspace', 'my-anchor',
+                'parent-01', FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS)
+        then: 'a data validation exception is thrown'
+            thrown(DataValidationException)
+    }
 
 }