a2df06ef0e6d4dac5af26486cb2647a75c7cbfb8
[cps.git] / cps-ri / src / test / groovy / org / onap / cps / spi / utils / SessionManagerSpec.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 com.google.common.util.concurrent.TimeLimiter
24 import org.hibernate.HibernateException
25 import org.hibernate.Transaction
26 import org.onap.cps.spi.entities.AnchorEntity
27 import org.onap.cps.spi.exceptions.SessionManagerException
28 import org.onap.cps.spi.repository.AnchorRepository
29 import org.onap.cps.spi.repository.DataspaceRepository
30 import org.testcontainers.shaded.com.google.common.util.concurrent.UncheckedExecutionException
31 import spock.lang.Specification
32 import org.hibernate.Session
33
34 import java.util.concurrent.ExecutionException
35
36 class SessionManagerSpec extends Specification {
37
38     def spiedTimeLimiterProvider = Spy(TimeLimiterProvider)
39     def mockDataspaceRepository = Mock(DataspaceRepository)
40     def mockAnchorRepository = Mock(AnchorRepository)
41     def mockSession = Mock(Session)
42
43     def objectUnderTest = new SessionManager(spiedTimeLimiterProvider, mockDataspaceRepository, mockAnchorRepository)
44
45     def 'Lock anchor entity with #exceptionDuringTest exception.'(){
46         given: 'a dummy session'
47             objectUnderTest.sessionMap.put('dummySession', mockSession)
48         and: 'the anchor name can be resolved'
49             def mockAnchorEntity = Mock(AnchorEntity)
50             mockAnchorEntity.getId() > 456
51             mockAnchorRepository.getByDataspaceAndName(_, _) >> mockAnchorEntity
52         and: 'timeLimiter throws an #exceptionDuringTest exception'
53             def mockTimeLimiter = Mock(TimeLimiter)
54             spiedTimeLimiterProvider.getTimeLimiter(_) >> mockTimeLimiter
55             mockTimeLimiter.callWithTimeout(*_) >> { throw exceptionDuringTest }
56         when: 'session tries to acquire anchor lock'
57             objectUnderTest.lockAnchor('dummySession', 'some-dataspace','some-anchor', 123L)
58         then: 'a session manager exception is thrown with the expected detail'
59             def thrown = thrown(SessionManagerException)
60             thrown.details.contains(expectedExceptionDetail)
61         where:
62             exceptionDuringTest               || expectedExceptionDetail
63             new InterruptedException()        || 'interrupted'
64             new ExecutionException()          || 'aborted'
65     }
66
67     def 'Close session that does not exist.'() {
68         when: 'attempt to close session that does not exist'
69             objectUnderTest.closeSession('unknown session id')
70         then: 'a session manager exception is thrown with the unknown id in the details'
71             def thrown = thrown(SessionManagerException)
72             assert thrown.details.contains('unknown session id')
73     }
74
75     def 'Hibernate exception while closing session.'() {
76         given: 'a test session with a transaction'
77             objectUnderTest.sessionMap.put('testSessionId', mockSession)
78             mockSession.getTransaction() >> Mock(Transaction)
79         and: 'an hibernate exception when closing that session'
80             def hibernateException = new HibernateException('test')
81             mockSession.close() >> { throw hibernateException }
82         when: 'attempt to close session'
83             objectUnderTest.closeSession('testSessionId')
84         then: 'a session manager exception is thrown with the session id in the details'
85             def thrown = thrown(SessionManagerException)
86             assert thrown.details.contains('testSessionId')
87         and: 'the original exception as cause'
88             assert thrown.cause == hibernateException
89     }
90
91     def 'Attempt to lock anchor entity with session Id that does not exists'(){
92         when: 'attempt to acquire anchor lock with session that does not exists'
93             objectUnderTest.lockAnchor('unknown session id','','',123L)
94         then: 'a session manager exception is thrown with the unknown id in the details'
95             def thrown = thrown(SessionManagerException)
96             thrown.details.contains('unknown session id')
97     }
98
99 }