SqlHandler { <K> EntryColumnData getEntryColumnData(TableMetaDataView tableMetaData, K key, PdxInstance value, Operation operation) { List<ColumnData> keyColumnData = createKeyColumnDataList(tableMetaData, key); List<ColumnData> valueColumnData = null; if (operation.isCreate() || operation.isUpdate()) { valueColumnData = createValueColumnDataList(tableMetaData, value); } return new EntryColumnData(keyColumnData, valueColumnData); } SqlHandler(InternalCache cache, String regionName,
      TableMetaDataManager tableMetaDataManager, JdbcConnectorService configService,
      DataSourceFactory dataSourceFactory);  SqlHandler(InternalCache cache, String regionName,
      TableMetaDataManager tableMetaDataManager, JdbcConnectorService configService); PdxInstance read(Region<K, V> region, K key); void write(Region<K, V> region, Operation operation, K key, PdxInstance value);  }
@Test public void returnsCorrectColumnForGet() throws Exception { EntryColumnData entryColumnData = handler.getEntryColumnData(tableMetaDataView, key, value, Operation.GET); assertThat(entryColumnData.getEntryKeyColumnData()).isNotNull(); assertThat(entryColumnData.getEntryValueColumnData()).isEmpty(); assertThat(entryColumnData.getEntryKeyColumnData()).hasSize(1); assertThat(entryColumnData.getEntryKeyColumnData().get(0).getColumnName()) .isEqualTo(KEY_COLUMN); }
@Test public void returnsCorrectColumnForGetGivenCompositeKey() throws Exception { Object compositeKeyFieldValueOne = "fieldValueOne"; Object compositeKeyFieldValueTwo = "fieldValueTwo"; PdxInstance compositeKey = mock(PdxInstance.class); when(compositeKey.isDeserializable()).thenReturn(false); when(compositeKey.getFieldNames()).thenReturn(Arrays.asList("fieldOne", "fieldTwo")); when(compositeKey.getField("fieldOne")).thenReturn(compositeKeyFieldValueOne); when(compositeKey.getField("fieldTwo")).thenReturn(compositeKeyFieldValueTwo); when(tableMetaDataView.getKeyColumnNames()).thenReturn(Arrays.asList("fieldOne", "fieldTwo")); FieldMapping fieldMapping1 = mock(FieldMapping.class); when(fieldMapping1.getJdbcName()).thenReturn("fieldOne"); when(fieldMapping1.getPdxName()).thenReturn("fieldOne"); when(fieldMapping1.getJdbcType()).thenReturn(JDBCType.VARCHAR.getName()); FieldMapping fieldMapping2 = mock(FieldMapping.class); when(fieldMapping2.getJdbcName()).thenReturn("fieldTwo"); when(fieldMapping2.getPdxName()).thenReturn("fieldTwo"); when(fieldMapping2.getJdbcType()).thenReturn(JDBCType.VARCHAR.getName()); columnNames.clear(); columnNames.add("fieldOne"); columnNames.add("fieldTwo"); when(tableMetaDataView.getColumnDataType("fieldOne")).thenReturn(JDBCType.VARCHAR); when(tableMetaDataView.getColumnDataType("fieldTwo")).thenReturn(JDBCType.VARCHAR); when(regionMapping.getFieldMappings()).thenReturn(Arrays.asList(fieldMapping1, fieldMapping2)); when(regionMapping.getIds()).thenReturn("fieldOne,fieldTwo"); createSqlHandler(); EntryColumnData entryColumnData = handler.getEntryColumnData(tableMetaDataView, compositeKey, value, Operation.GET); assertThat(entryColumnData.getEntryKeyColumnData()).isNotNull(); assertThat(entryColumnData.getEntryValueColumnData()).isEmpty(); assertThat(entryColumnData.getEntryKeyColumnData()).hasSize(2); assertThat(entryColumnData.getEntryKeyColumnData().get(0).getColumnName()) .isEqualTo("fieldOne"); assertThat(entryColumnData.getEntryKeyColumnData().get(1).getColumnName()) .isEqualTo("fieldTwo"); }
@Test public void getEntryColumnDataGivenWrongNumberOfCompositeKeyFieldsFails() { PdxInstance compositeKey = mock(PdxInstance.class); when(compositeKey.isDeserializable()).thenReturn(false); when(compositeKey.getFieldNames()).thenReturn(Arrays.asList("fieldOne")); when(tableMetaDataView.getKeyColumnNames()).thenReturn(Arrays.asList("fieldOne", "fieldTwo")); FieldMapping fieldMapping1 = mock(FieldMapping.class); when(fieldMapping1.getJdbcName()).thenReturn("fieldOne"); when(fieldMapping1.getPdxName()).thenReturn("fieldOne"); when(fieldMapping1.getJdbcType()).thenReturn(JDBCType.VARCHAR.getName()); FieldMapping fieldMapping2 = mock(FieldMapping.class); when(fieldMapping2.getJdbcName()).thenReturn("fieldTwo"); when(fieldMapping2.getPdxName()).thenReturn("fieldTwo"); when(fieldMapping2.getJdbcType()).thenReturn(JDBCType.VARCHAR.getName()); when(regionMapping.getFieldMappings()).thenReturn(Arrays.asList(fieldMapping1, fieldMapping2)); columnNames.clear(); columnNames.add("fieldOne"); columnNames.add("fieldTwo"); when(tableMetaDataView.getColumnDataType("fieldOne")).thenReturn(JDBCType.VARCHAR); when(tableMetaDataView.getColumnDataType("fieldTwo")).thenReturn(JDBCType.VARCHAR); when(regionMapping.getIds()).thenReturn("fieldOne,fieldTwo"); createSqlHandler(); assertThatThrownBy( () -> handler.getEntryColumnData(tableMetaDataView, compositeKey, value, Operation.GET)) .isInstanceOf(JdbcConnectorException.class).hasMessageContaining( "The key \"" + compositeKey + "\" should have 2 fields but has 1 fields."); }
@Test public void getEntryColumnDataGivenWrongFieldNameInCompositeKeyFails() { Object compositeKeyFieldValueOne = "fieldValueOne"; Object compositeKeyFieldValueTwo = "fieldValueTwo"; PdxInstance compositeKey = mock(PdxInstance.class); when(compositeKey.isDeserializable()).thenReturn(false); when(compositeKey.getFieldNames()).thenReturn(Arrays.asList("fieldOne", "fieldTwoWrong")); when(compositeKey.getField("fieldOne")).thenReturn(compositeKeyFieldValueOne); when(compositeKey.getField("fieldTwoWrong")).thenReturn(compositeKeyFieldValueTwo); when(tableMetaDataView.getKeyColumnNames()).thenReturn(Arrays.asList("fieldOne", "fieldTwo")); FieldMapping fieldMapping1 = mock(FieldMapping.class); when(fieldMapping1.getJdbcName()).thenReturn("fieldOne"); when(fieldMapping1.getPdxName()).thenReturn("fieldOne"); when(fieldMapping1.getJdbcType()).thenReturn(JDBCType.VARCHAR.getName()); FieldMapping fieldMapping2 = mock(FieldMapping.class); when(fieldMapping2.getJdbcName()).thenReturn("fieldTwo"); when(fieldMapping2.getPdxName()).thenReturn("fieldTwo"); when(fieldMapping2.getJdbcType()).thenReturn(JDBCType.VARCHAR.getName()); FieldMapping fieldMapping3 = mock(FieldMapping.class); String nonKeyColumn = "fieldTwoWrong"; when(fieldMapping3.getJdbcName()).thenReturn(nonKeyColumn); when(fieldMapping3.getPdxName()).thenReturn(nonKeyColumn); when(fieldMapping3.getJdbcType()).thenReturn(JDBCType.VARCHAR.getName()); when(regionMapping.getFieldMappings()) .thenReturn(Arrays.asList(fieldMapping1, fieldMapping2, fieldMapping3)); columnNames.clear(); columnNames.add("fieldOne"); columnNames.add("fieldTwo"); columnNames.add("fieldTwoWrong"); when(tableMetaDataView.getColumnDataType("fieldOne")).thenReturn(JDBCType.VARCHAR); when(tableMetaDataView.getColumnDataType("fieldTwo")).thenReturn(JDBCType.VARCHAR); when(tableMetaDataView.getColumnDataType("fieldTwoWrong")).thenReturn(JDBCType.VARCHAR); when(regionMapping.getIds()).thenReturn("fieldOne,fieldTwo"); createSqlHandler(); assertThatThrownBy(() -> handler.getEntryColumnData(tableMetaDataView, compositeKey, value, Operation.GET)).isInstanceOf(JdbcConnectorException.class).hasMessageContaining("The key \"" + compositeKey + "\" has the field \"fieldTwoWrong\" which does not match any of the key columns: [fieldOne, fieldTwo]"); }
@Test public void getEntryColumnDataGivenUnknownFieldNameInCompositeKeyFails() { Object compositeKeyFieldValueOne = "fieldValueOne"; Object compositeKeyFieldValueTwo = "fieldValueTwo"; PdxInstance compositeKey = mock(PdxInstance.class); when(compositeKey.isDeserializable()).thenReturn(false); when(compositeKey.getFieldNames()).thenReturn(Arrays.asList("fieldOne", "fieldTwoUnknown")); when(compositeKey.getField("fieldOne")).thenReturn(compositeKeyFieldValueOne); when(compositeKey.getField("fieldTwoUnknown")).thenReturn(compositeKeyFieldValueTwo); when(tableMetaDataView.getKeyColumnNames()).thenReturn(Arrays.asList("fieldOne", "fieldTwo")); FieldMapping fieldMapping1 = mock(FieldMapping.class); when(fieldMapping1.getJdbcName()).thenReturn("fieldOne"); when(fieldMapping1.getPdxName()).thenReturn("fieldOne"); when(fieldMapping1.getJdbcType()).thenReturn(JDBCType.VARCHAR.getName()); FieldMapping fieldMapping2 = mock(FieldMapping.class); when(fieldMapping2.getJdbcName()).thenReturn("fieldTwo"); when(fieldMapping2.getPdxName()).thenReturn("fieldTwo"); when(fieldMapping2.getJdbcType()).thenReturn(JDBCType.VARCHAR.getName()); FieldMapping fieldMapping3 = mock(FieldMapping.class); String nonKeyColumn = "fieldTwoWrong"; when(fieldMapping3.getJdbcName()).thenReturn(nonKeyColumn); when(fieldMapping3.getPdxName()).thenReturn(nonKeyColumn); when(fieldMapping3.getJdbcType()).thenReturn(JDBCType.VARCHAR.getName()); when(regionMapping.getFieldMappings()) .thenReturn(Arrays.asList(fieldMapping1, fieldMapping2, fieldMapping3)); columnNames.clear(); columnNames.add("fieldOne"); columnNames.add("fieldTwo"); columnNames.add("fieldTwoWrong"); when(tableMetaDataView.getColumnDataType("fieldOne")).thenReturn(JDBCType.VARCHAR); when(tableMetaDataView.getColumnDataType("fieldTwo")).thenReturn(JDBCType.VARCHAR); when(tableMetaDataView.getColumnDataType("fieldTwoWrong")).thenReturn(JDBCType.VARCHAR); when(regionMapping.getIds()).thenReturn("fieldOne,fieldTwo"); createSqlHandler(); assertThatThrownBy( () -> handler.getEntryColumnData(tableMetaDataView, compositeKey, value, Operation.GET)) .isInstanceOf(JdbcConnectorException.class) .hasMessageContaining("The key \"" + compositeKey + "\" has the field \"fieldTwoUnknown\" which does not match any of the key columns: [fieldOne, fieldTwo]"); }
@Test public void returnsCorrectColumnForDestroyWithCompositeKey() { Object compositeKeyFieldValueOne = "fieldValueOne"; Object compositeKeyFieldValueTwo = "fieldValueTwo"; PdxInstance compositeKey = mock(PdxInstance.class); when(compositeKey.isDeserializable()).thenReturn(false); when(compositeKey.getFieldNames()).thenReturn(Arrays.asList("fieldOne", "fieldTwo")); when(compositeKey.getField("fieldOne")).thenReturn(compositeKeyFieldValueOne); when(compositeKey.getField("fieldTwo")).thenReturn(compositeKeyFieldValueTwo); when(tableMetaDataView.getKeyColumnNames()).thenReturn(Arrays.asList("fieldOne", "fieldTwo")); FieldMapping fieldMapping1 = mock(FieldMapping.class); when(fieldMapping1.getJdbcName()).thenReturn("fieldOne"); when(fieldMapping1.getPdxName()).thenReturn("fieldOne"); when(fieldMapping1.getJdbcType()).thenReturn(JDBCType.VARCHAR.getName()); FieldMapping fieldMapping2 = mock(FieldMapping.class); when(fieldMapping2.getJdbcName()).thenReturn("fieldTwo"); when(fieldMapping2.getPdxName()).thenReturn("fieldTwo"); when(fieldMapping2.getJdbcType()).thenReturn(JDBCType.VARCHAR.getName()); columnNames.clear(); columnNames.add("fieldOne"); columnNames.add("fieldTwo"); when(tableMetaDataView.getColumnDataType("fieldOne")).thenReturn(JDBCType.VARCHAR); when(tableMetaDataView.getColumnDataType("fieldTwo")).thenReturn(JDBCType.VARCHAR); when(regionMapping.getFieldMappings()).thenReturn(Arrays.asList(fieldMapping1, fieldMapping2)); when(regionMapping.getIds()).thenReturn("fieldOne,fieldTwo"); createSqlHandler(); EntryColumnData entryColumnData = handler.getEntryColumnData(tableMetaDataView, compositeKey, value, Operation.DESTROY); assertThat(entryColumnData.getEntryKeyColumnData()).isNotNull(); assertThat(entryColumnData.getEntryValueColumnData()).isEmpty(); assertThat(entryColumnData.getEntryKeyColumnData()).hasSize(2); assertThat(entryColumnData.getEntryKeyColumnData().get(0).getColumnName()) .isEqualTo("fieldOne"); assertThat(entryColumnData.getEntryKeyColumnData().get(1).getColumnName()) .isEqualTo("fieldTwo"); assertThat(entryColumnData.getEntryKeyColumnData().get(0).getValue()) .isEqualTo(compositeKeyFieldValueOne); assertThat(entryColumnData.getEntryKeyColumnData().get(1).getValue()) .isEqualTo(compositeKeyFieldValueTwo); }
@Test public void returnsCorrectColumnForDestroy() { EntryColumnData entryColumnData = handler.getEntryColumnData(tableMetaDataView, key, value, Operation.DESTROY); assertThat(entryColumnData.getEntryKeyColumnData()).isNotNull(); assertThat(entryColumnData.getEntryValueColumnData()).isEmpty(); assertThat(entryColumnData.getEntryKeyColumnData()).hasSize(1); assertThat(entryColumnData.getEntryKeyColumnData().get(0).getColumnName()) .isEqualTo(KEY_COLUMN); }
@Test public void getEntryColumnDataWhenMultipleIdColumnsGivenNonPdxInstanceFails() { when(tableMetaDataView.getKeyColumnNames()).thenReturn(Arrays.asList("fieldOne", "fieldTwo")); Object nonCompositeKey = Integer.valueOf(123); assertThatThrownBy(() -> handler.getEntryColumnData(tableMetaDataView, nonCompositeKey, value, Operation.DESTROY)).isInstanceOf(JdbcConnectorException.class).hasMessageContaining( "The key \"123\" of class \"java.lang.Integer\" must be a PdxInstance because multiple columns are configured as ids."); }
@Test public void getEntryColumnDataWhenMultipleIdColumnsGivenDeserializablePdxInstanceFails() throws Exception { when(tableMetaDataView.getKeyColumnNames()).thenReturn(Arrays.asList("fieldOne", "fieldTwo")); PdxInstance nonCompositeKey = mock(PdxInstance.class); when(nonCompositeKey.isDeserializable()).thenReturn(true); assertThatThrownBy(() -> handler.getEntryColumnData(tableMetaDataView, nonCompositeKey, value, Operation.DESTROY)).isInstanceOf(JdbcConnectorException.class) .hasMessageContaining("The key \"" + nonCompositeKey + "\" must be a PdxInstance created with PdxInstanceFactory.neverDeserialize"); }