9b58c8bc3231378393b04cfeeff326f9f381a924
[cps.git] / cps-ri / src / test / groovy / org / onap / cps / spi / utils / SessionManagerIntegrationSpec.groovy
1 /*
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
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.spi.utils
22
23 import org.onap.cps.spi.exceptions.SessionManagerException
24 import org.onap.cps.spi.impl.CpsPersistenceSpecBase
25 import org.springframework.beans.factory.annotation.Autowired
26 import org.springframework.test.context.jdbc.Sql
27
28 class SessionManagerIntegrationSpec extends CpsPersistenceSpecBase{
29
30     final static String SET_DATA = '/data/anchor.sql'
31
32     @Autowired
33     SessionManager objectUnderTest
34
35     def sessionId
36     def shortTimeoutForTesting = 200L
37
38     def setup(){
39         sessionId = objectUnderTest.startSession()
40     }
41
42     def cleanup(){
43         objectUnderTest.closeSession(sessionId)
44     }
45
46     @Sql([CLEAR_DATA, SET_DATA])
47     def 'Lock anchor.'(){
48         when: 'session tries to acquire anchor lock by passing anchor entity details'
49             objectUnderTest.lockAnchor(sessionId, DATASPACE_NAME, ANCHOR_NAME1, shortTimeoutForTesting)
50         then: 'no exception is thrown'
51             noExceptionThrown()
52     }
53
54     @Sql([CLEAR_DATA, SET_DATA])
55     def 'Attempt to lock anchor when another session is holding the lock.'(){
56         given: 'another session that holds an anchor lock'
57             def otherSessionId = objectUnderTest.startSession()
58             objectUnderTest.lockAnchor(otherSessionId,DATASPACE_NAME,ANCHOR_NAME1,shortTimeoutForTesting)
59         when: 'a session tries to acquire the same anchor lock'
60             objectUnderTest.lockAnchor(sessionId,DATASPACE_NAME,ANCHOR_NAME1,shortTimeoutForTesting)
61         then: 'a session manager exception is thrown specifying operation reached timeout'
62             def thrown = thrown(SessionManagerException)
63             thrown.message.contains('Timeout')
64         then: 'when the other session holding the lock is closed, lock can finally be acquired'
65             objectUnderTest.closeSession(otherSessionId)
66             objectUnderTest.lockAnchor(sessionId,DATASPACE_NAME,ANCHOR_NAME1,shortTimeoutForTesting)
67     }
68
69 }