|
| 1 | +/* |
| 2 | + * Copyright 2018-2020 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.r2dbc.repository.query; |
| 17 | + |
| 18 | +import org.junit.Before; |
| 19 | +import org.junit.Rule; |
| 20 | +import org.junit.Test; |
| 21 | +import org.junit.rules.ExpectedException; |
| 22 | +import org.junit.runner.RunWith; |
| 23 | +import org.mockito.Mock; |
| 24 | +import org.mockito.junit.MockitoJUnitRunner; |
| 25 | +import org.springframework.data.r2dbc.core.DatabaseClient; |
| 26 | +import org.springframework.data.relational.repository.query.RelationalParameterAccessor; |
| 27 | + |
| 28 | +import static org.mockito.ArgumentMatchers.anyInt; |
| 29 | +import static org.mockito.Mockito.*; |
| 30 | + |
| 31 | +/** |
| 32 | + * @author Roman Chigvintsev |
| 33 | + */ |
| 34 | +@RunWith(MockitoJUnitRunner.class) |
| 35 | +public class PartTreeBindableQueryTest { |
| 36 | + @Mock |
| 37 | + private RelationalParameterAccessor parameterAccessor; |
| 38 | + @Mock |
| 39 | + private ParameterMetadataProvider parameterMetadataProvider; |
| 40 | + @Mock |
| 41 | + private ParameterMetadata parameterMetadata; |
| 42 | + |
| 43 | + @Rule |
| 44 | + public ExpectedException thrown = ExpectedException.none(); |
| 45 | + |
| 46 | + @SuppressWarnings("ResultOfMethodCallIgnored") |
| 47 | + @Before |
| 48 | + public void setUp() { |
| 49 | + doReturn(String.class).when(parameterMetadata).getType(); |
| 50 | + when(parameterMetadata.prepare(any())).thenCallRealMethod(); |
| 51 | + when(parameterMetadataProvider.getParameterMetadata(anyInt())).thenReturn(parameterMetadata); |
| 52 | + } |
| 53 | + |
| 54 | + @SuppressWarnings({"rawtypes", "unchecked"}) |
| 55 | + @Test |
| 56 | + public void bindsNonNullValueToQueryParameterByIndex() { |
| 57 | + when(parameterAccessor.getValues()).thenReturn(new Object[]{"test"}); |
| 58 | + DatabaseClient.BindSpec bindSpecMock = mock(DatabaseClient.BindSpec.class); |
| 59 | + PartTreeBindableQuery query = new PartTreeBindableQuery("SELECT", parameterAccessor, parameterMetadataProvider); |
| 60 | + query.bind(bindSpecMock); |
| 61 | + verify(bindSpecMock, times(1)).bind(0, "test"); |
| 62 | + } |
| 63 | + |
| 64 | + @SuppressWarnings({"rawtypes", "unchecked"}) |
| 65 | + @Test |
| 66 | + public void bindsNonNullValueToQueryParameterByName() { |
| 67 | + when(parameterAccessor.getValues()).thenReturn(new Object[]{"test"}); |
| 68 | + when(parameterMetadata.getName()).thenReturn("param0"); |
| 69 | + DatabaseClient.BindSpec bindSpecMock = mock(DatabaseClient.BindSpec.class); |
| 70 | + |
| 71 | + PartTreeBindableQuery query = new PartTreeBindableQuery("SELECT", parameterAccessor, parameterMetadataProvider); |
| 72 | + query.bind(bindSpecMock); |
| 73 | + verify(bindSpecMock, times(1)).bind("param0", "test"); |
| 74 | + } |
| 75 | + |
| 76 | + @SuppressWarnings({"rawtypes", "unchecked"}) |
| 77 | + @Test |
| 78 | + public void bindsNullValueToQueryParameterByIndex() { |
| 79 | + when(parameterAccessor.getValues()).thenReturn(new Object[1]); |
| 80 | + when(parameterMetadata.isIsNullParameter()).thenReturn(true); |
| 81 | + DatabaseClient.BindSpec bindSpecMock = mock(DatabaseClient.BindSpec.class); |
| 82 | + |
| 83 | + PartTreeBindableQuery query = new PartTreeBindableQuery("SELECT", parameterAccessor, parameterMetadataProvider); |
| 84 | + query.bind(bindSpecMock); |
| 85 | + verify(bindSpecMock, times(1)).bindNull(0, String.class); |
| 86 | + } |
| 87 | + |
| 88 | + @SuppressWarnings({"rawtypes", "unchecked"}) |
| 89 | + @Test |
| 90 | + public void bindsNullValueToQueryParameterByName() { |
| 91 | + when(parameterAccessor.getValues()).thenReturn(new Object[1]); |
| 92 | + when(parameterMetadata.getName()).thenReturn("param0"); |
| 93 | + when(parameterMetadata.isIsNullParameter()).thenReturn(true); |
| 94 | + DatabaseClient.BindSpec bindSpecMock = mock(DatabaseClient.BindSpec.class); |
| 95 | + |
| 96 | + PartTreeBindableQuery query = new PartTreeBindableQuery("SELECT", parameterAccessor, parameterMetadataProvider); |
| 97 | + query.bind(bindSpecMock); |
| 98 | + verify(bindSpecMock, times(1)).bindNull("param0", String.class); |
| 99 | + } |
| 100 | + |
| 101 | + @SuppressWarnings({"rawtypes", "unchecked"}) |
| 102 | + @Test |
| 103 | + public void throwsExceptionWhenNullIsNotAllowedAsIndexedQueryParameter() { |
| 104 | + thrown.expect(IllegalArgumentException.class); |
| 105 | + thrown.expectMessage("Value of parameter with index 0 must not be null!"); |
| 106 | + |
| 107 | + when(parameterAccessor.getValues()).thenReturn(new Object[1]); |
| 108 | + DatabaseClient.BindSpec bindSpecMock = mock(DatabaseClient.BindSpec.class); |
| 109 | + |
| 110 | + PartTreeBindableQuery query = new PartTreeBindableQuery("SELECT", parameterAccessor, parameterMetadataProvider); |
| 111 | + query.bind(bindSpecMock); |
| 112 | + } |
| 113 | + |
| 114 | + @SuppressWarnings({"rawtypes", "unchecked"}) |
| 115 | + @Test |
| 116 | + public void throwsExceptionWhenNullIsNotAllowedAsNamedQueryParameter() { |
| 117 | + thrown.expect(IllegalArgumentException.class); |
| 118 | + thrown.expectMessage("Value of parameter with name param0 must not be null!"); |
| 119 | + |
| 120 | + when(parameterAccessor.getValues()).thenReturn(new Object[1]); |
| 121 | + when(parameterMetadata.getName()).thenReturn("param0"); |
| 122 | + DatabaseClient.BindSpec bindSpecMock = mock(DatabaseClient.BindSpec.class); |
| 123 | + |
| 124 | + PartTreeBindableQuery query = new PartTreeBindableQuery("SELECT", parameterAccessor, parameterMetadataProvider); |
| 125 | + query.bind(bindSpecMock); |
| 126 | + } |
| 127 | +} |
0 commit comments