X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=cps-ri%2Fsrc%2Ftest%2Fgroovy%2Forg%2Fonap%2Fcps%2Fspi%2Futils%2FSessionManagerIntegrationSpec.groovy;h=a1f6d580fd6b3d4a2040bc904071f196c25d06fe;hb=0b80343610a215f26a7d764cc849f8e9ca44fea0;hp=c46092f075139742af887821317d9c7b7882e613;hpb=c9275522f95f6cd94f28286f5e86f86dd451a19b;p=cps.git diff --git a/cps-ri/src/test/groovy/org/onap/cps/spi/utils/SessionManagerIntegrationSpec.groovy b/cps-ri/src/test/groovy/org/onap/cps/spi/utils/SessionManagerIntegrationSpec.groovy index c46092f07..a1f6d580f 100644 --- a/cps-ri/src/test/groovy/org/onap/cps/spi/utils/SessionManagerIntegrationSpec.groovy +++ b/cps-ri/src/test/groovy/org/onap/cps/spi/utils/SessionManagerIntegrationSpec.groovy @@ -1,6 +1,6 @@ /* * ============LICENSE_START======================================================= - * Copyright (C) 2021-2022 Nordix Foundation + * Copyright (C) 2022 Nordix Foundation * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,37 +20,64 @@ package org.onap.cps.spi.utils -import org.hibernate.SessionException +import org.onap.cps.spi.config.CpsSessionFactory +import org.onap.cps.spi.exceptions.SessionManagerException import org.onap.cps.spi.impl.CpsPersistenceSpecBase +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.test.context.jdbc.Sql class SessionManagerIntegrationSpec extends CpsPersistenceSpecBase{ - def objectUnderTest = new SessionManager(); + final static String SET_DATA = '/data/anchor.sql' - def 'start session'() { - when: 'start session' - def result = objectUnderTest.startSession() - then: 'session ID is returned' - assert result instanceof String - objectUnderTest.closeSession(result) + @Autowired + SessionManager objectUnderTest + + @Autowired + CpsSessionFactory cpsSessionFactory + + def sessionId + def shortTimeoutForTesting = 200L + + def setup(){ + sessionId = objectUnderTest.startSession() + } + + def cleanup(){ + objectUnderTest.closeSession(sessionId, objectUnderTest.WITH_COMMIT) } - def 'close session'(){ - given: 'session Id from calling the start session method' - def sessionId = objectUnderTest.startSession() - when: 'close session method is called' - objectUnderTest.closeSession(sessionId) + @Sql([CLEAR_DATA, SET_DATA]) + def 'Lock anchor.'(){ + when: 'session tries to acquire anchor lock by passing anchor entity details' + objectUnderTest.lockAnchor(sessionId, DATASPACE_NAME, ANCHOR_NAME1, shortTimeoutForTesting) then: 'no exception is thrown' noExceptionThrown() } - def 'close session that does not exist' (){ - given: 'session Id that does not exist' - def unknownSessionId = 'unknown session id' - when: 'close session method is called' - objectUnderTest.closeSession(unknownSessionId) - then: 'a session exception is thrown' - def thrown = thrown(SessionException) - assert thrown.message.contains(unknownSessionId) + @Sql([CLEAR_DATA, SET_DATA]) + def 'Attempt to lock anchor when another session is holding the lock.'(){ + given: 'another session that holds an anchor lock' + def otherSessionId = objectUnderTest.startSession() + objectUnderTest.lockAnchor(otherSessionId,DATASPACE_NAME,ANCHOR_NAME1,shortTimeoutForTesting) + when: 'a session tries to acquire the same anchor lock' + objectUnderTest.lockAnchor(sessionId,DATASPACE_NAME,ANCHOR_NAME1,shortTimeoutForTesting) + then: 'a session manager exception is thrown specifying operation reached timeout' + def thrown = thrown(SessionManagerException) + thrown.message.contains('Timeout') + then: 'when the other session holding the lock is closed, lock can finally be acquired' + objectUnderTest.closeSession(otherSessionId, objectUnderTest.WITH_COMMIT) + objectUnderTest.lockAnchor(sessionId,DATASPACE_NAME,ANCHOR_NAME1,shortTimeoutForTesting) } + + @Sql([CLEAR_DATA, SET_DATA]) + def 'Lock anchor twice using the same session.'(){ + given: 'session that already holds an anchor lock' + objectUnderTest.lockAnchor(sessionId, DATASPACE_NAME, ANCHOR_NAME1, shortTimeoutForTesting) + when: 'same session tries to acquire same anchor lock' + objectUnderTest.lockAnchor(sessionId, DATASPACE_NAME, ANCHOR_NAME1, shortTimeoutForTesting) + then: 'no exception is thrown' + noExceptionThrown() + } + }