|
| 1 | +/* |
| 2 | + * Copyright © 2022 Cask Data, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | + * use this file except in compliance with the License. You may obtain a copy of |
| 6 | + * the License at |
| 7 | + * |
| 8 | + * http://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, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations under |
| 14 | + * the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.cdap.delta.sqlserver; |
| 18 | + |
| 19 | +import com.microsoft.sqlserver.jdbc.SQLServerDriver; |
| 20 | +import io.cdap.delta.api.DeltaFailureRuntimeException; |
| 21 | +import io.cdap.delta.api.DeltaSourceContext; |
| 22 | +import io.cdap.delta.api.Offset; |
| 23 | +import io.cdap.delta.plugin.mock.MockContext; |
| 24 | +import io.cdap.delta.plugin.mock.MockEventEmitter; |
| 25 | +import org.apache.kafka.connect.data.ConnectSchema; |
| 26 | +import org.apache.kafka.connect.data.Field; |
| 27 | +import org.apache.kafka.connect.data.Schema; |
| 28 | +import org.apache.kafka.connect.data.Struct; |
| 29 | +import org.apache.kafka.connect.source.SourceRecord; |
| 30 | +import org.junit.Test; |
| 31 | +import org.junit.runner.RunWith; |
| 32 | +import org.mockito.Mockito; |
| 33 | +import org.mockito.junit.MockitoJUnitRunner; |
| 34 | + |
| 35 | +import java.util.Arrays; |
| 36 | +import java.util.HashMap; |
| 37 | +import java.util.HashSet; |
| 38 | +import java.util.List; |
| 39 | + |
| 40 | +@RunWith(MockitoJUnitRunner.class) |
| 41 | +public class SqlServerRecordConsumerTest { |
| 42 | + private static final String DATABASE = "AdventureWorks2014"; |
| 43 | + private static final String TOPICNAME = "dbo.testreplication.npe"; |
| 44 | + |
| 45 | + @Test(expected = DeltaFailureRuntimeException.class) |
| 46 | + public void testTableWithoutPrimaryKey() { |
| 47 | + DeltaSourceContext context = new MockContext(SQLServerDriver.class); |
| 48 | + MockEventEmitter eventEmitter = new MockEventEmitter(5); |
| 49 | + SqlServerRecordConsumer sqlServerRecordConsumer = new SqlServerRecordConsumer |
| 50 | + (context, eventEmitter, DATABASE, new HashSet<>(), new HashMap<>(), new Offset(), true); |
| 51 | + SourceRecord sourceRecordMock = Mockito.mock(SourceRecord.class); |
| 52 | + // the topic name should be in this form: [db.server.name].[schema].[table] |
| 53 | + Mockito.when(sourceRecordMock.topic()).thenReturn(TOPICNAME); |
| 54 | + List<Field> fields = Arrays.asList(new Field("op", 0, new ConnectSchema(Schema.Type.STRING))); |
| 55 | + Struct valueStruct = new Struct(new ConnectSchema(Schema.Type.STRUCT, false, null, |
| 56 | + null, null, null, null, fields, null, null)); |
| 57 | + valueStruct.put("op", "c"); |
| 58 | + Mockito.when(sourceRecordMock.value()).thenReturn(valueStruct); |
| 59 | + Mockito.when(sourceRecordMock.sourceOffset()).thenReturn(new HashMap() {{ |
| 60 | + put("snapshot", true); |
| 61 | + }}); |
| 62 | + //setting primary key as NULL |
| 63 | + Mockito.when(sourceRecordMock.key()).thenReturn(null); |
| 64 | + sqlServerRecordConsumer.accept(sourceRecordMock); |
| 65 | + } |
| 66 | + |
| 67 | +} |
0 commit comments