-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#20: Refactored ColumnAdapterNotes. #25
Merged
redcatbear
merged 3 commits into
master
from
refactoring/20_Refactor_ColumnAdapterNotes
Jan 31, 2020
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We usually use JSONAssert.assertEquals, don't we?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Depends on the direction. This is reverse (i.e. deserialization). JSONAssert does not help here.