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
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.spi.utils
23 import org.onap.cps.spi.config.CpsSessionFactory
24 import org.onap.cps.spi.exceptions.SessionManagerException
25 import org.onap.cps.spi.impl.CpsPersistenceSpecBase
26 import org.springframework.beans.factory.annotation.Autowired
27 import org.springframework.test.context.jdbc.Sql
29 class SessionManagerIntegrationSpec extends CpsPersistenceSpecBase{
31 final static String SET_DATA = '/data/anchor.sql'
34 SessionManager objectUnderTest
37 CpsSessionFactory cpsSessionFactory
40 def shortTimeoutForTesting = 200L
43 sessionId = objectUnderTest.startSession()
47 objectUnderTest.closeSession(sessionId, objectUnderTest.WITH_COMMIT)
50 @Sql([CLEAR_DATA, SET_DATA])
52 when: 'session tries to acquire anchor lock by passing anchor entity details'
53 objectUnderTest.lockAnchor(sessionId, DATASPACE_NAME, ANCHOR_NAME1, shortTimeoutForTesting)
54 then: 'no exception is thrown'
58 @Sql([CLEAR_DATA, SET_DATA])
59 def 'Attempt to lock anchor when another session is holding the lock.'(){
60 given: 'another session that holds an anchor lock'
61 def otherSessionId = objectUnderTest.startSession()
62 objectUnderTest.lockAnchor(otherSessionId,DATASPACE_NAME,ANCHOR_NAME1,shortTimeoutForTesting)
63 when: 'a session tries to acquire the same anchor lock'
64 objectUnderTest.lockAnchor(sessionId,DATASPACE_NAME,ANCHOR_NAME1,shortTimeoutForTesting)
65 then: 'a session manager exception is thrown specifying operation reached timeout'
66 def thrown = thrown(SessionManagerException)
67 thrown.message.contains('Timeout')
68 then: 'when the other session holding the lock is closed, lock can finally be acquired'
69 objectUnderTest.closeSession(otherSessionId, objectUnderTest.WITH_COMMIT)
70 objectUnderTest.lockAnchor(sessionId,DATASPACE_NAME,ANCHOR_NAME1,shortTimeoutForTesting)
73 @Sql([CLEAR_DATA, SET_DATA])
74 def 'Lock anchor twice using the same session.'(){
75 given: 'session that already holds an anchor lock'
76 objectUnderTest.lockAnchor(sessionId, DATASPACE_NAME, ANCHOR_NAME1, shortTimeoutForTesting)
77 when: 'same session tries to acquire same anchor lock'
78 objectUnderTest.lockAnchor(sessionId, DATASPACE_NAME, ANCHOR_NAME1, shortTimeoutForTesting)
79 then: 'no exception is thrown'