-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* #20: Refactored `ColumnAdapterNotes`.
- Loading branch information
1 parent
4456ed8
commit 1777376
Showing
9 changed files
with
177 additions
and
153 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
src/main/java/com/exasol/adapter/adapternotes/ColumnAdapterNotesJsonConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package com.exasol.adapter.adapternotes; | ||
|
||
import javax.json.*; | ||
|
||
import com.exasol.adapter.AdapterException; | ||
import com.exasol.utils.JsonHelper; | ||
|
||
/** | ||
* Converts column adapter Notes into JSON format and back. | ||
*/ | ||
public final class ColumnAdapterNotesJsonConverter { | ||
private static final ColumnAdapterNotesJsonConverter COLUMN_ADAPTER_NOTES_JSON_CONVERTER = new ColumnAdapterNotesJsonConverter(); | ||
|
||
/** | ||
* Returns instance of {@link ColumnAdapterNotesJsonConverter} singleton class. | ||
* | ||
* @return {@link ColumnAdapterNotesJsonConverter} instance | ||
*/ | ||
public static ColumnAdapterNotesJsonConverter getInstance() { | ||
return COLUMN_ADAPTER_NOTES_JSON_CONVERTER; | ||
} | ||
|
||
private ColumnAdapterNotesJsonConverter() { | ||
// intentionally left blank | ||
} | ||
|
||
/** | ||
* Converts column adapter notes into a JSON format. | ||
* | ||
* @param columnAdapterNotes column adapter notes to be converted | ||
* @return string representation of a JSON Object | ||
*/ | ||
public String convertToJson(final ColumnAdapterNotes columnAdapterNotes) { | ||
final JsonBuilderFactory factory = JsonHelper.getBuilderFactory(); | ||
final JsonObjectBuilder builder = factory.createObjectBuilder().add("jdbcDataType", | ||
columnAdapterNotes.getJdbcDataType()); | ||
return builder.build().toString(); | ||
} | ||
|
||
/** | ||
* Converts JSON representation of column adapter notes into instance of {@link ColumnAdapterNotes} class. | ||
* | ||
* @param adapterNotes JSON representation of schema adapter notes | ||
* @param columnName name of the column | ||
* @return instance of {@link ColumnAdapterNotes} | ||
* @throws AdapterException if the adapter notes are missing or cannot be parsed | ||
*/ | ||
public ColumnAdapterNotes convertFromJsonToColumnAdapterNotes(final String adapterNotes, final String columnName) | ||
throws AdapterException { | ||
if ((adapterNotes == null) || adapterNotes.isEmpty()) { | ||
throw new AdapterException("Adapter notes for column " + columnName + " are empty or NULL. " // | ||
+ "Please refresh the virtual schema."); | ||
} | ||
final JsonObject root; | ||
try { | ||
root = JsonHelper.getJsonObject(adapterNotes); | ||
} catch (final Exception exception) { | ||
throw new AdapterException("Could not parse the column adapter notes of column \"" + columnName + "\"." // | ||
+ "Please refresh the virtual schema.", exception); | ||
} | ||
return new ColumnAdapterNotes(root.getInt("jdbcDataType")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/test/java/com/exasol/adapter/adapternotes/ColumnAdapterNotesJsonConverterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.exasol.adapter.adapternotes; | ||
|
||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.junit.Assert.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
import java.sql.Types; | ||
|
||
import org.json.JSONException; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.skyscreamer.jsonassert.JSONAssert; | ||
|
||
import com.exasol.adapter.AdapterException; | ||
|
||
class ColumnAdapterNotesJsonConverterTest { | ||
private static final String JDBC_DATA_TYPE = "jdbcDataType"; | ||
private ColumnAdapterNotesJsonConverter converter; | ||
|
||
@BeforeEach | ||
void beforeEach() { | ||
this.converter = ColumnAdapterNotesJsonConverter.getInstance(); | ||
} | ||
|
||
@Test | ||
void testConvertToJson() throws JSONException { | ||
final int expectedType = Types.DATE; | ||
final ColumnAdapterNotes adapterNotes = new ColumnAdapterNotes(expectedType); | ||
JSONAssert.assertEquals("{\"" + JDBC_DATA_TYPE + "\":" + expectedType + "}", | ||
this.converter.convertToJson(adapterNotes), false); | ||
} | ||
|
||
@Test | ||
void testConvertFromJsonToColumnAdapterNotes() throws AdapterException { | ||
final int expectedType = Types.VARCHAR; | ||
final String adapterNotesAsJson = "{\"" + JDBC_DATA_TYPE + "\":" + expectedType + "}"; | ||
final ColumnAdapterNotes expectedAdapterNotes = new ColumnAdapterNotes(expectedType); | ||
assertThat(this.converter.convertFromJsonToColumnAdapterNotes(adapterNotesAsJson, "C1"), | ||
equalTo(expectedAdapterNotes)); | ||
} | ||
|
||
@Test | ||
void testConvertFromJsonToColumnAdapterNotesThrowsExceptionWhenAdapterNotesAreNull() { | ||
assertThrows(AdapterException.class, () -> this.converter.convertFromJsonToColumnAdapterNotes(null, "")); | ||
} | ||
|
||
@Test | ||
void testConvertFromJsonToColumnAdapterNotesThrowsExceptionWithEmptyAdapterNotes() { | ||
assertThrows(AdapterException.class, () -> this.converter.convertFromJsonToColumnAdapterNotes("", "")); | ||
} | ||
|
||
@Test | ||
void testconvertFromJsonToColumnAdapterNotesThrowsExceptionWithWrongAdapterNotes() { | ||
assertThrows(AdapterException.class, () -> this.converter.convertFromJsonToColumnAdapterNotes("testNotes", "")); | ||
} | ||
} |
Oops, something went wrong.