|
| 1 | +/* |
| 2 | + * Copyright 2023 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.data.cassandra.observability; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.*; |
| 19 | +import static org.mockito.Mockito.*; |
| 20 | + |
| 21 | +import io.micrometer.observation.ObservationRegistry; |
| 22 | + |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | +import org.springframework.data.cassandra.ReactiveSession; |
| 25 | +import org.springframework.data.cassandra.core.cql.session.DefaultBridgedReactiveSession; |
| 26 | +import org.springframework.test.util.ReflectionTestUtils; |
| 27 | + |
| 28 | +import com.datastax.oss.driver.api.core.CqlSession; |
| 29 | +import com.datastax.oss.driver.api.core.CqlSessionBuilder; |
| 30 | + |
| 31 | +/** |
| 32 | + * Unit tests for {@link ObservableReactiveSessionFactoryBean}. |
| 33 | + * |
| 34 | + * @author Mark Paluch |
| 35 | + */ |
| 36 | +class ObservableReactiveSessionFactoryBeanUnitTests { |
| 37 | + |
| 38 | + @Test // GH-1366 |
| 39 | + void sessionFactoryBeanUnwrapsObservationProxy() throws Exception { |
| 40 | + |
| 41 | + CqlSession sessionMock = mock(CqlSession.class); |
| 42 | + ObservationRegistry registry = ObservationRegistry.NOOP; |
| 43 | + |
| 44 | + CqlSession wrapped = ObservableCqlSessionFactory.wrap(sessionMock, registry); |
| 45 | + |
| 46 | + ObservableReactiveSessionFactoryBean bean = new ObservableReactiveSessionFactoryBean(wrapped, registry); |
| 47 | + bean.afterPropertiesSet(); |
| 48 | + |
| 49 | + ReactiveSession object = bean.getObject(); |
| 50 | + Object cqlSession = ReflectionTestUtils.getField(ReflectionTestUtils.getField(object, "delegate"), "session"); |
| 51 | + |
| 52 | + assertThat(cqlSession).isSameAs(sessionMock); |
| 53 | + } |
| 54 | + |
| 55 | + @Test // GH-1366 |
| 56 | + void reactiveSessionDelegateBeanUnwrapsObservationProxy() throws Exception { |
| 57 | + |
| 58 | + CqlSession sessionMock = mock(CqlSession.class); |
| 59 | + ObservationRegistry registry = ObservationRegistry.NOOP; |
| 60 | + |
| 61 | + CqlSession wrapped = ObservableCqlSessionFactory.wrap(sessionMock, registry); |
| 62 | + |
| 63 | + ReactiveSession object = new DefaultBridgedReactiveSession(wrapped); |
| 64 | + Object cqlSession = ReflectionTestUtils.getField(object, "session"); |
| 65 | + |
| 66 | + assertThat(cqlSession).isSameAs(sessionMock); |
| 67 | + } |
| 68 | + |
| 69 | + @Test // GH-1366 |
| 70 | + void closesCqlSessionViaBuilder() throws Exception { |
| 71 | + |
| 72 | + CqlSessionBuilder builderMock = mock(CqlSessionBuilder.class); |
| 73 | + CqlSession sessionMock = mock(CqlSession.class); |
| 74 | + |
| 75 | + doReturn(sessionMock).when(builderMock).build(); |
| 76 | + |
| 77 | + ObservationRegistry registry = ObservationRegistry.NOOP; |
| 78 | + |
| 79 | + ObservableReactiveSessionFactoryBean bean = new ObservableReactiveSessionFactoryBean(builderMock, registry); |
| 80 | + bean.afterPropertiesSet(); |
| 81 | + bean.destroy(); |
| 82 | + |
| 83 | + verify(sessionMock).close(); |
| 84 | + } |
| 85 | + |
| 86 | + @Test // GH-1366 |
| 87 | + void doesNotCloseCqlSession() throws Exception { |
| 88 | + |
| 89 | + CqlSession sessionMock = mock(CqlSession.class); |
| 90 | + |
| 91 | + ObservationRegistry registry = ObservationRegistry.NOOP; |
| 92 | + |
| 93 | + ObservableReactiveSessionFactoryBean bean = new ObservableReactiveSessionFactoryBean(sessionMock, registry); |
| 94 | + bean.afterPropertiesSet(); |
| 95 | + bean.destroy(); |
| 96 | + |
| 97 | + verifyNoInteractions(sessionMock); |
| 98 | + } |
| 99 | +} |
0 commit comments