-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into MODINV-1069
- Loading branch information
Showing
8 changed files
with
182 additions
and
7 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
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
42 changes: 42 additions & 0 deletions
42
src/main/java/org/folio/inventory/domain/instances/Dates.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,42 @@ | ||
package org.folio.inventory.domain.instances; | ||
|
||
import io.vertx.core.json.JsonObject; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
import static org.apache.commons.lang3.ObjectUtils.anyNotNull; | ||
import static org.folio.inventory.support.JsonHelper.includeIfPresent; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public class Dates { | ||
// JSON property names | ||
public static final String DATE_TYPE_ID_KEY = "dateTypeId"; | ||
public static final String DATE1_KEY = "date1"; | ||
public static final String DATE2_KEY = "date2"; | ||
|
||
public final String dateTypeId; | ||
public final String date1; | ||
public final String date2; | ||
|
||
public static JsonObject datesToJson(Dates dates) { | ||
if (dates == null || (dates.getDate1() == null && dates.getDate2() == null && dates.getDateTypeId() == null)) { | ||
return null; | ||
} | ||
var json = new JsonObject(); | ||
includeIfPresent(json, DATE_TYPE_ID_KEY, dates.getDateTypeId()); | ||
includeIfPresent(json, DATE1_KEY, dates.getDate1()); | ||
includeIfPresent(json, DATE2_KEY, dates.getDate2()); | ||
return json; | ||
} | ||
|
||
public static Dates datesFromJson(JsonObject datesJson) { | ||
if (datesJson == null) { | ||
return null; | ||
} | ||
var dateTypeId = datesJson.getString(DATE_TYPE_ID_KEY); | ||
var date1 = datesJson.getString(DATE1_KEY); | ||
var date2 = datesJson.getString(DATE2_KEY); | ||
return anyNotNull(dateTypeId, date1, date2) ? new Dates(dateTypeId, date1, date2) : null; | ||
} | ||
} |
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
74 changes: 74 additions & 0 deletions
74
src/test/java/org/folio/inventory/domain/instances/DatesTest.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,74 @@ | ||
package org.folio.inventory.domain.instances; | ||
|
||
import io.vertx.core.json.JsonObject; | ||
import junitparams.JUnitParamsRunner; | ||
import junitparams.Parameters; | ||
import junitparams.converters.Nullable; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
import static org.folio.inventory.domain.instances.Dates.datesFromJson; | ||
import static org.folio.inventory.domain.instances.Dates.datesToJson; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.hamcrest.Matchers.nullValue; | ||
|
||
@RunWith(JUnitParamsRunner.class) | ||
public class DatesTest { | ||
|
||
@Parameters({ | ||
"1, 1990, 2002", | ||
"1, 1990, null", | ||
"1, null, 2022", | ||
"null, 1990, 2002", | ||
"null, 1990, null", | ||
}) | ||
@Test | ||
public void shouldCreateDatesFromJson(@Nullable String dateTypeId, @Nullable String date1, @Nullable String date2) { | ||
var dates = datesFromJson(datesJson(dateTypeId, date1, date2)); | ||
|
||
assertThat(dates.getDateTypeId(), is(dateTypeId)); | ||
assertThat(dates.getDate1(), is(date1)); | ||
assertThat(dates.getDate2(), is(date2)); | ||
} | ||
|
||
@Test | ||
public void shouldNotCreateDatesFromJsonWhenJsonIsNull() { | ||
assertThat(datesFromJson(null), nullValue()); | ||
} | ||
|
||
@Test | ||
public void shouldNotCreateDatesFromJsonWhenAllFieldsAreNull() { | ||
assertThat(datesFromJson(datesJson(null, null, null)), nullValue()); | ||
} | ||
|
||
@Parameters({ | ||
"1, 1990, 2002", | ||
"1, 1990, null", | ||
"1, null, 2022", | ||
"null, 1990, 2002", | ||
"null, 1990, null", | ||
}) | ||
@Test | ||
public void shouldConvertDatesToJson(@Nullable String dateTypeId, @Nullable String date1, @Nullable String date2) { | ||
var json = datesToJson(new Dates(dateTypeId, date1, date2)); | ||
|
||
assertThat(json.getString("dateTypeId"), is(dateTypeId)); | ||
assertThat(json.getString("date1"), is(date1)); | ||
assertThat(json.getString("date2"), is(date2)); | ||
} | ||
|
||
@Test | ||
public void shouldNotConvertDatesToJsonWhenItIsNull() { | ||
assertThat(datesToJson(null), nullValue()); | ||
} | ||
|
||
@Test | ||
public void shouldNotConvertDatesToJsonWhenAllFieldsAreNull() { | ||
assertThat(datesToJson(new Dates(null, null, null)), nullValue()); | ||
} | ||
|
||
private JsonObject datesJson(String dateTypeId, String date1, String date2) { | ||
return new JsonObject().put("dateTypeId", dateTypeId).put("date1", date1).put("date2", date2); | ||
} | ||
} |