Add graceful shutdown for Session Manager
[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.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
28
29 class SessionManagerIntegrationSpec extends CpsPersistenceSpecBase{
30
31     final static String SET_DATA = '/data/anchor.sql'
32
33     @Autowired
34     SessionManager objectUnderTest
35
36     @Autowired
37     CpsSessionFactory cpsSessionFactory
38
39     def sessionId
40     def shortTimeoutForTesting = 200L
41
42     def setup(){
43         sessionId = objectUnderTest.startSession()
44     }
45
46     def cleanup(){
47         objectUnderTest.closeSession(sessionId, objectUnderTest.WITH_COMMIT)
48     }
49
50     @Sql([CLEAR_DATA, SET_DATA])
51     def 'Lock anchor.'(){
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'
55             noExceptionThrown()
56     }
57
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)
71     }
72
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'
80             noExceptionThrown()
81     }
82
83 }