Skip to content

Commit

Permalink
Merge pull request #13 from opencredo/feature/near_complete_coverage
Browse files Browse the repository at this point in the history
Near complete code coverage
  • Loading branch information
pegerto authored Jan 31, 2019
2 parents 5921e60 + 9b3b000 commit b099f10
Show file tree
Hide file tree
Showing 11 changed files with 283 additions and 29 deletions.
3 changes: 3 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.21.0</version>
<configuration>
<reportFormat>plain</reportFormat>
</configuration>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
Expand Down
3 changes: 3 additions & 0 deletions src/assembly/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
<useProjectArtifact>true</useProjectArtifact>
<useTransitiveFiltering>true</useTransitiveFiltering>
<excludes>
<exclude>io.confluent.*</exclude>
<exclude>org.apache,kafka.connect.*</exclude>
<exclude>org.apache.kafka.connect.*</exclude>
<exclude>org.apache.kafka:connect-api</exclude>
</excludes>
</dependencySet>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,39 @@ public class DotNetDateDeserializer implements JsonDeserializer<ZonedDateTime> {

public static final int SLASH_DATE_LENGTH = "/DATE(".length();
public static final int CLOSE_BRACKET_SLASH_LENGTH = ")/".length();
public static final String MINUS = "-";
public static final String PLUS = "+";
public static final String REGEX_ESCAPE = "\\";


@Override
public ZonedDateTime deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
if (jsonElement.isJsonNull()) {
return null;
if (jsonElement.isJsonNull() || jsonElement.getAsString().isEmpty() || isStringTooShortToWork(jsonElement)) {
throw new JsonParseException("Unable to deserialize [" + jsonElement + "] to a ZoneDateTime.");
}
String json = jsonElement.getAsString();
String dateString = json.substring(SLASH_DATE_LENGTH, json.length() - CLOSE_BRACKET_SLASH_LENGTH);
ZoneOffset offset = ZoneOffset.UTC;

if (dateString.contains("+")) {
String[] dateParts = dateString.split("\\+");
String[] dateParts = dateString.split(REGEX_ESCAPE + PLUS);
dateString = dateParts[0];
offset = ZoneOffset.of("+" + dateParts[1]);
offset = ZoneOffset.of(PLUS + dateParts[1]);
} else if (dateString.contains("-")) {
String[] dateParts = dateString.split("\\-");
String[] dateParts = dateString.split(REGEX_ESCAPE + MINUS);
dateString = dateParts[0];
offset = ZoneOffset.of("+" + dateParts[1]);
offset = ZoneOffset.of(MINUS + dateParts[1]);
}
Instant i = Instant.ofEpochMilli(Long.parseLong(dateString));

return ZonedDateTime.ofInstant(i, offset);
}

private boolean isStringTooShortToWork(JsonElement jsonElement) {
return jsonElement.getAsString().length() - SLASH_DATE_LENGTH - CLOSE_BRACKET_SLASH_LENGTH <= 0;
}

// private generateOffset(String dateString){
//
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import org.apache.kafka.connect.errors.ConnectException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -32,17 +31,16 @@ private static ZonedDateTime getParsedDate(String dateTimeString) {
return ZonedDateTime.parse(dateTimeString);
} catch (DateTimeParseException e) {
log.debug("Failed to parse to ZonedDateTime format", e);
throw new ConnectException(e);
throw new JsonParseException("Unable to deserialize [" + dateTimeString + "] to a ZoneDateTime.", e);
}
}

@Override
public ZonedDateTime deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
if (jsonElement.isJsonNull()) {
return null;
if (jsonElement.isJsonNull() || jsonElement.getAsString().isEmpty()) {
throw new JsonParseException("Unable to deserialize [" + jsonElement + "] to a ZoneDateTime.");
}
String json = jsonElement.getAsString();

return getParsedDate(json);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,6 @@ public TppLogSourceConfig(Map<String, ?> props) {
super(CONFIG_DEF, props);
}

private TppLogSourceConfig(ConfigDef definition, Map<?, ?> originals, boolean doLog) {
super(definition, originals, doLog);
}

private TppLogSourceConfig(ConfigDef definition, Map<?, ?> originals) {
super(definition, originals);
}

Map<String, String> returnPropertiesWithDefaultsValuesIfMissing() {
Map<String, ?> uncastProperties = this.values();
Map<String, String> config = new HashMap<>(uncastProperties.size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class TppLogSourceTask extends SourceTask {
public static final String LAST_READ = "last_read";
public static final String LAST_API_OFFSET = "last_api_offset";
public static final String DEFAULT_FROM_TIME = "1984-05-04T00:00:00.0000000Z";
private static final org.slf4j.Logger log = LoggerFactory.getLogger(TppLogSourceConnector.class);
private static final org.slf4j.Logger log = LoggerFactory.getLogger(TppLogSourceTask.class);
private String fromDate = DEFAULT_FROM_TIME;
private String baseUrl;
private String topic;
Expand Down Expand Up @@ -55,7 +55,7 @@ public void start(Map<String, String> props) {
}

Object lastApiOffset = persistedMap.get(LAST_API_OFFSET);
if (lastApiOffset != null && lastApiOffset instanceof Long) {
if (lastApiOffset != null) {
apiOffset = (Long) lastApiOffset;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package com.opencredo.connect.venafi.tpp.log.Deserializer;

import com.google.gson.JsonElement;
import com.google.gson.JsonNull;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;

import java.time.ZoneOffset;
import java.time.ZonedDateTime;

import static com.opencredo.connect.venafi.tpp.log.TppLogSourceTask.DEFAULT_FROM_TIME;
import static org.junit.jupiter.api.Assertions.*;

class DotNetDateDeserializerTest {

public static final String MILLISECOND_SINCE_EPOCH_UNTIL_1945_MAY_04_Z = "452476800000";
public static final String SLASH_DATE = "/DATE(";
public static final String CLOSE_BRACKET_SLASH = ")/";
public static final String NEGATIVE_OFFSET = "-0100";
public static final String POSITIVE_OFFSET = "+0100";
public static final ZonedDateTime ZONED_DATE_TIME_1945_05_04_Z = ZonedDateTime.parse(DEFAULT_FROM_TIME);
public static final DotNetDateDeserializer DESERIALIZER = new DotNetDateDeserializer();

@Test
void deserialize_empty_string() {
JsonElement jsonElem = given_an_empty_string_json_elem();
Executable dateTimeExecutable = when_the_date_is_deserialized_which_may_throw_exception(jsonElem);
assertThrows(JsonParseException.class, dateTimeExecutable);
}

@Test
void deserialize_json_null() {
JsonElement jsonElem = given_a_json_null();
Executable dateTimeExecutable = when_the_date_is_deserialized_which_may_throw_exception(jsonElem);
assertThrows(JsonParseException.class, dateTimeExecutable);
}

@Test
void deserialize_invalid_null() {
JsonElement jsonElem = given_json_of_milliseconds("");
Executable dateTimeExecutable = when_the_date_is_deserialized_which_may_throw_exception(jsonElem);
assertThrows(JsonParseException.class, dateTimeExecutable);
}

@Test
void deserialize_normal_format() {
JsonElement jsonElem = given_json_of_milliseconds(MILLISECOND_SINCE_EPOCH_UNTIL_1945_MAY_04_Z);
ZonedDateTime dateTime = when_the_date_is_deserialized(jsonElem);
assertNotNull(dateTime);
assertTrue(dateTime.isEqual(ZONED_DATE_TIME_1945_05_04_Z));
assertEquals(dateTime.getOffset(), ZoneOffset.ofHours(0));
}

@Test
void deserialize_positive_timezone() {
JsonElement jsonElem = given_json_of_milliseconds_with_offset(MILLISECOND_SINCE_EPOCH_UNTIL_1945_MAY_04_Z, POSITIVE_OFFSET);
ZonedDateTime dateTime = when_the_date_is_deserialized(jsonElem);
assertNotNull(dateTime);
assertTrue(dateTime.isEqual(ZONED_DATE_TIME_1945_05_04_Z));
assertEquals(dateTime.getOffset(), ZoneOffset.ofHours(1));
}

@Test
void deserialize_negative_timezone() {
JsonElement jsonElem = given_json_of_milliseconds_with_offset(MILLISECOND_SINCE_EPOCH_UNTIL_1945_MAY_04_Z, NEGATIVE_OFFSET);
ZonedDateTime dateTime = when_the_date_is_deserialized(jsonElem);
assertNotNull(dateTime);
assertTrue(dateTime.isEqual(ZONED_DATE_TIME_1945_05_04_Z));
assertEquals(dateTime.getOffset(), ZoneOffset.ofHours(-1));
}

private JsonElement given_an_empty_string_json_elem() {
return new JsonPrimitive("");
}

private JsonElement given_a_json_null() {
return JsonNull.INSTANCE;
}

private JsonElement given_json_of_milliseconds(String milliseconds) {
return new JsonPrimitive(SLASH_DATE + milliseconds + CLOSE_BRACKET_SLASH);
}

private JsonElement given_json_of_milliseconds_with_offset(String milliseconds, String offset) {
return new JsonPrimitive(SLASH_DATE + milliseconds + offset + CLOSE_BRACKET_SLASH);
}

private ZonedDateTime when_the_date_is_deserialized(JsonElement jsonElem) {
return DESERIALIZER.deserialize(jsonElem, null, null);
}

private Executable when_the_date_is_deserialized_which_may_throw_exception(JsonElement jsonElem) {
return () -> DESERIALIZER.deserialize(jsonElem, null, null);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package com.opencredo.connect.venafi.tpp.log.Deserializer;

import com.google.gson.JsonElement;
import com.google.gson.JsonNull;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;

import java.time.ZoneOffset;
import java.time.ZonedDateTime;

import static com.opencredo.connect.venafi.tpp.log.Deserializer.DotNetDateDeserializerTest.ZONED_DATE_TIME_1945_05_04_Z;
import static org.junit.jupiter.api.Assertions.*;

class ZonedDateTimeDeserializerTest {
public static final ZonedDateTimeDeserializer DESERIALIZER = new ZonedDateTimeDeserializer();
public static final String LOCAL_DATE_TIME = "1984-05-04T00:00:00.0000000";
public static final String ZONED_DATE_TIME = LOCAL_DATE_TIME + "Z";
public static final String GIBBERISH = "sadkjalskjdalksjd";

@Test
void json_null() {
JsonElement jsonElem = given_a_json_null();
Executable dateTimeExecutable = when_the_date_is_deserialized_which_may_throw_exception(jsonElem);
assertThrows(JsonParseException.class, dateTimeExecutable);
}

@Test
void empty_string() {
JsonElement jsonElem = given_an_empty_string_json_elem();
Executable dateTimeExecutable = when_the_date_is_deserialized_which_may_throw_exception(jsonElem);
assertThrows(JsonParseException.class, dateTimeExecutable);
}

@Test
void invalid_string() {
JsonElement jsonElem = given_json_of_(GIBBERISH);
Executable dateTimeExecutable = when_the_date_is_deserialized_which_may_throw_exception(jsonElem);
assertThrows(JsonParseException.class, dateTimeExecutable);
}

@Test
void localDateTime() {
JsonElement jsonElem = given_json_of_(LOCAL_DATE_TIME);
ZonedDateTime dateTime = when_the_date_is_deserialized(jsonElem);
assertNotNull(dateTime);
assertTrue(dateTime.isEqual(ZONED_DATE_TIME_1945_05_04_Z));
assertEquals(dateTime.getOffset(), ZoneOffset.ofHours(0));
}

@Test
void zonedDateTime() {
JsonElement jsonElem = given_json_of_(ZONED_DATE_TIME);
ZonedDateTime dateTime = when_the_date_is_deserialized(jsonElem);
assertNotNull(dateTime);
assertTrue(dateTime.isEqual(ZONED_DATE_TIME_1945_05_04_Z));
assertEquals(dateTime.getOffset(), ZoneOffset.ofHours(0));
}

private JsonElement given_an_empty_string_json_elem() {
return new JsonPrimitive("");
}

private JsonElement given_a_json_null() {
return JsonNull.INSTANCE;
}

private JsonElement given_json_of_(String milliseconds) {
return new JsonPrimitive(milliseconds);
}

private ZonedDateTime when_the_date_is_deserialized(JsonElement jsonElem) {
return DESERIALIZER.deserialize(jsonElem, null, null);
}

private Executable when_the_date_is_deserialized_which_may_throw_exception(JsonElement jsonElem) {
return () -> DESERIALIZER.deserialize(jsonElem, null, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,21 @@ public void as_a_client_I_want_to_not_make_a_logs_call_if_I_get_an_exception_fro
wireMockServer.verify(0, getRequestedFor(urlPathMatching(LOG_API_REGEX_PATH)));
}

@Test
public void as_a_task_I_want_to_make_only_one_logs_call_per_the_poll_interval() {

given_the_mock_will_respond_to_auth();
given_the_mock_will_respond_to_log();
TppLogSourceTask task = given_a_task_is_setup_with(1000000);

List<SourceRecord> logs = when_the_task_is_polled(task);
then_the_logs_are_of_size(logs, 2);
List<SourceRecord> logs2 = when_the_task_is_polled(task);
then_the_logs_are_of_size(logs2, 0);
wireMockServer.verify(1, postRequestedFor(urlPathMatching(AUTHORIZE_API_REGEX_PATH)));
wireMockServer.verify(1, getRequestedFor(urlPathMatching(LOG_API_REGEX_PATH)));
}

@Test
public void as_a_client_I_want_a_token_to_only_generate_once_while_before_token_expiry() {

Expand Down Expand Up @@ -154,7 +169,7 @@ public void as_a_task_I_want_a_valid_context() {
given_the_mock_will_respond_to_auth();
given_the_mock_will_respond_to_log_for_offsetsStorage();

TppLogSourceTask task = given_a_task_is_setup_with(mockSourceTaskContext);
TppLogSourceTask task = given_a_task_is_setup(mockSourceTaskContext);


List<SourceRecord> sourceRecords1 = when_the_task_is_polled(task);
Expand All @@ -171,7 +186,7 @@ public void as_a_task_I_want_to_handle_an_empty_context() {
given_the_mock_will_respond_to_auth();
given_the_mock_will_respond_to_log_for_empty_offsetsStorage();

TppLogSourceTask task = given_a_task_is_setup_with(mockSourceTaskContext);
TppLogSourceTask task = given_a_task_is_setup(mockSourceTaskContext);

List<SourceRecord> sourceRecords1 = when_the_task_is_polled(task);
then_the_logs_are_of_size(sourceRecords1, 3);
Expand Down Expand Up @@ -281,7 +296,7 @@ private String when_a_token_is_got(TppLogSourceTask task) {
return task.getToken();
}

private TppLogSourceTask given_a_task_is_setup_with(SourceTaskContext context) {
private TppLogSourceTask given_a_task_is_setup(SourceTaskContext context) {
TppLogSourceTask task = new TppLogSourceTask();
Map<String, String> config = getTaskConfig();
if (context != null) {
Expand All @@ -291,8 +306,16 @@ private TppLogSourceTask given_a_task_is_setup_with(SourceTaskContext context) {
return task;
}

private TppLogSourceTask given_a_task_is_setup_with(Integer pollInterval) {
TppLogSourceTask task = new TppLogSourceTask();
Map<String, String> config = getTaskConfig();
config.put(POLL_INTERVAL, String.valueOf(pollInterval));
task.start(config);
return task;
}

private TppLogSourceTask given_a_task_is_setup() {
return given_a_task_is_setup_with(null);
return given_a_task_is_setup(null);
}

private Map<String, String> getTaskConfig() {
Expand Down
Loading

0 comments on commit b099f10

Please sign in to comment.