Merge "Add memory usage to integration tests [UPDATED]"
[cps.git] / cps-ri / src / test / groovy / org / onap / cps / spi / impl / CpsModulePersistenceServiceSpec.groovy
index 9ef9732..9696b28 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * ============LICENSE_START=======================================================
  * Copyright (c) 2021 Bell Canada.
- * Modifications Copyright (C) 2022 Nordix Foundation
+ * Modifications Copyright (C) 2022-2023 Nordix Foundation
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -28,7 +28,6 @@ import org.onap.cps.spi.repository.ModuleReferenceRepository
 import org.onap.cps.spi.repository.SchemaSetRepository
 import org.onap.cps.spi.repository.YangResourceRepository
 import org.springframework.dao.DataIntegrityViolationException
-import spock.lang.Shared
 import spock.lang.Specification
 
 import java.sql.SQLException
@@ -38,17 +37,14 @@ import java.sql.SQLException
  */
 class CpsModulePersistenceServiceSpec extends Specification {
 
-    // Instance to test
     CpsModulePersistenceService objectUnderTest
 
-    // Mocks
     def dataspaceRepositoryMock = Mock(DataspaceRepository)
     def yangResourceRepositoryMock = Mock(YangResourceRepository)
     def schemaSetRepositoryMock = Mock(SchemaSetRepository)
     def cpsAdminPersistenceServiceMock = Mock(CpsAdminPersistenceService)
     def moduleReferenceRepositoryMock = Mock(ModuleReferenceRepository)
 
-    // Constants
     def yangResourceName = 'my-yang-resource-name'
     def yangResourceContent = 'module stores {\n' +
             '    yang-version 1.1;\n' +
@@ -62,29 +58,23 @@ class CpsModulePersistenceServiceSpec extends Specification {
             '    }' +
             '}'
 
-    // Scenario data
-    @Shared
-    yangResourceChecksum = 'b13faef573ed1374139d02c40d8ce09c80ea1dc70e63e464c1ed61568d48d539'
-    @Shared
-    yangResourceChecksumDbConstraint = 'yang_resource_checksum_key'
-    @Shared
-    sqlExceptionMessage = String.format('(checksum)=(%s)', yangResourceChecksum)
-    @Shared
-    checksumIntegrityException =
-            new DataIntegrityViolationException(
-                    "checksum integrity exception",
+    static yangResourceChecksum = 'b13faef573ed1374139d02c40d8ce09c80ea1dc70e63e464c1ed61568d48d539'
+    static yangResourceChecksumDbConstraint = 'yang_resource_checksum_key'
+    static sqlExceptionMessage = String.format('(checksum)=(%s)', yangResourceChecksum)
+    static checksumIntegrityException =  new DataIntegrityViolationException('checksum integrity exception',
                     new ConstraintViolationException('', new SQLException(sqlExceptionMessage), yangResourceChecksumDbConstraint))
-    @Shared
-    anotherIntegrityException = new DataIntegrityViolationException("another integrity exception")
+    static checksumIntegrityExceptionWithoutChecksum =  new DataIntegrityViolationException('checksum integrity exception',
+                    new ConstraintViolationException('', new SQLException('no checksum'), yangResourceChecksumDbConstraint))
+    static otherIntegrityException = new DataIntegrityViolationException('another integrity exception')
 
     def setup() {
         objectUnderTest = new CpsModulePersistenceServiceImpl(yangResourceRepositoryMock, schemaSetRepositoryMock,
-            dataspaceRepositoryMock, cpsAdminPersistenceServiceMock, moduleReferenceRepositoryMock)
+            dataspaceRepositoryMock, moduleReferenceRepositoryMock)
     }
 
     def 'Store schema set error scenario: #scenario.'() {
         given: 'no yang resource are currently saved'
-            yangResourceRepositoryMock.findAllByChecksumIn(_) >> Collections.emptyList()
+            yangResourceRepositoryMock.findAllByChecksumIn(_ as Collection<String>) >> Collections.emptyList()
         and: 'persisting yang resource raises db constraint exception (in case of concurrent requests for example)'
             yangResourceRepositoryMock.saveAll(_) >> { throw dbException }
         when: 'attempt to store schema set '
@@ -92,11 +82,12 @@ class CpsModulePersistenceServiceSpec extends Specification {
             objectUnderTest.storeSchemaSet('my-dataspace', 'my-schema-set', newYangResourcesNameToContentMap)
         then: 'an #expectedThrownException is thrown'
             def e = thrown(expectedThrownException)
-            e.getMessage().contains(expectedThrownExceptionMessage)
+            assert e.getMessage().contains(expectedThrownExceptionMessage)
         where: 'the following data is used'
-            scenario                | dbException                || expectedThrownException | expectedThrownExceptionMessage
-            'checksum data failure' | checksumIntegrityException || DuplicatedYangResourceException | yangResourceChecksum
-            'other data failure'    | anotherIntegrityException  || DataIntegrityViolationException | 'another integrity exception'
+            scenario                            | dbException                               || expectedThrownException         | expectedThrownExceptionMessage
+            'checksum data failure'             | checksumIntegrityException                || DuplicatedYangResourceException | yangResourceChecksum
+            'checksum failure without checksum' | checksumIntegrityExceptionWithoutChecksum || DuplicatedYangResourceException | 'no checksum found'
+            'other data failure'                | otherIntegrityException                   || DataIntegrityViolationException | 'another integrity exception'
     }
 
 }