Skip to content

Commit

Permalink
Created test for OffsetDateTime instead of using Instant
Browse files Browse the repository at this point in the history
  • Loading branch information
revijay committed Nov 27, 2024
1 parent 4f69228 commit 302125e
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*******************************************************************************
* Copyright (c) 2024 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
package io.openliberty.jpa.data.tests.models;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;

/**
* Recreate from io.openliberty.data.internal_fat_jpa
* Same as DemographicInfo but uses OffsetDateTime instead of Instance
*/
@Entity
public class DemographicInfoWithOffset {

@Column
public OffsetDateTime collectedOn;

@GeneratedValue
@Id
public BigInteger id;

@Column
public BigDecimal publicDebt;

@Column
public BigDecimal intragovernmentalDebt;

@Column
public BigInteger numFullTimeWorkers;

public static DemographicInfoWithOffset of(int year, int month, int day,
long numFullTimeWorkers,
double intragovernmentalDebt, double publicDebt) {
DemographicInfoWithOffset inst = new DemographicInfoWithOffset();
inst.collectedOn = ZonedDateTime.of(year, month, day, 12, 0, 0, 0, ZoneId.of("America/New_York")).toOffsetDateTime();
inst.numFullTimeWorkers = BigInteger.valueOf(numFullTimeWorkers);
inst.intragovernmentalDebt = BigDecimal.valueOf(intragovernmentalDebt);
inst.publicDebt = BigDecimal.valueOf(publicDebt);
return inst;
}

@Override
public String toString() {
return "DemographicInfo from " + collectedOn;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

import java.math.BigDecimal;
import java.math.BigInteger;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;

Expand All @@ -28,7 +27,7 @@
public class DemographicInformation {

@Column
public OffsetDateTime collectedOn;
public ZonedDateTime collectedOn;

@GeneratedValue
@Id
Expand All @@ -47,7 +46,7 @@ public static DemographicInformation of(int year, int month, int day,
long numFullTimeWorkers,
double intragovernmentalDebt, double publicDebt) {
DemographicInformation inst = new DemographicInformation();
inst.collectedOn = ZonedDateTime.of(year, month, day, 12, 0, 0, 0, ZoneId.of("America/New_York")).toOffsetDateTime();
inst.collectedOn = ZonedDateTime.of(year, month, day, 12, 0, 0, 0, ZoneId.of("America/New_York"));
inst.numFullTimeWorkers = BigInteger.valueOf(numFullTimeWorkers);
inst.intragovernmentalDebt = BigDecimal.valueOf(intragovernmentalDebt);
inst.publicDebt = BigDecimal.valueOf(publicDebt);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import io.openliberty.jpa.data.tests.models.CityId;
import io.openliberty.jpa.data.tests.models.Coordinate;
import io.openliberty.jpa.data.tests.models.DemographicInfo;
import io.openliberty.jpa.data.tests.models.DemographicInfoWithOffset;
import io.openliberty.jpa.data.tests.models.DemographicInformation;
import io.openliberty.jpa.data.tests.models.Item;
import io.openliberty.jpa.data.tests.models.Line;
Expand Down Expand Up @@ -1520,12 +1521,12 @@ public void testOLGH29443() throws Exception {
}

@Test //Original issue: https://github.com/OpenLiberty/open-liberty/issues/29443
//@Ignore("Additional Issue: ZonedDateTime stored as blob, cannot do comparison of blobs on most databases")
@Ignore("Additional Issue: ZonedDateTime stored as blob, cannot do comparison of blobs on most databases")
public void testOLGH29443ZonedDateTime() throws Exception {
deleteAllEntities(DemographicInformation.class);

ZoneId ET = ZoneId.of("America/New_York");
OffsetDateTime when = ZonedDateTime.of(2022, 4, 29, 12, 0, 0, 0, ET).toOffsetDateTime();
ZonedDateTime when = ZonedDateTime.of(2022, 4, 29, 12, 0, 0, 0, ET);

DemographicInformation US2022 = DemographicInformation.of(2022, 4, 29, 132250000, 6526909395140.41, 23847245116757.60);
DemographicInformation US2007 = DemographicInformation.of(2007, 4, 30, 121090000, 3833110332444.19, 5007058051986.64);
Expand Down Expand Up @@ -1569,6 +1570,55 @@ public void testOLGH29443ZonedDateTime() throws Exception {
}
}

@Test //Original issue: https://github.com/OpenLiberty/open-liberty/issues/29443
public void testOLGH29443OffsetDateTime() throws Exception {
deleteAllEntities(DemographicInfoWithOffset.class);

ZoneId ET = ZoneId.of("America/New_York");
OffsetDateTime when = ZonedDateTime.of(2022, 4, 29, 12, 0, 0, 0, ET).toOffsetDateTime();

DemographicInfoWithOffset US2022 = DemographicInfoWithOffset.of(2022, 4, 29, 132250000, 6526909395140.41, 23847245116757.60);
DemographicInfoWithOffset US2007 = DemographicInfoWithOffset.of(2007, 4, 30, 121090000, 3833110332444.19, 5007058051986.64);

List<BigInteger> results;

tx.begin();
em.persist(US2022);
em.persist(US2007);
tx.commit();

List<Error> errors = new ArrayList<>();

Thread.sleep(Duration.ofSeconds(1).toMillis());

for (int i = 0; i < 10; i++) {
System.out.println("Executing SELECT query, iteration: " + i);

tx.begin();
results = em
.createQuery("SELECT this.numFullTimeWorkers FROM DemographicInfoWithOffset WHERE this.collectedOn=:when",
BigInteger.class)
.setParameter("when", when)
.getResultList();
tx.commit();

try {
assertNotNull("Query should not have returned null after iteration " + i, results);
// Recreate - an empty list is returned
assertFalse("Query should not have returned an empty list after iteration " + i, results.isEmpty());
assertEquals("Query should not have returned more than one result after iteration " + i, 1,
results.size());
assertEquals(US2022.numFullTimeWorkers, results.get(0));
} catch (AssertionError e) {
errors.add(e);
}
}

if (!errors.isEmpty()) {
throw new AssertionError("Executing the same query returned incorrect results " + errors.size() + " out of 10 executions", errors.get(0));
}
}

@Test
@Ignore("Reference issue: https://github.com/OpenLiberty/open-liberty/issues/29475")
public void testOLGH29475() throws Exception {
Expand Down

0 comments on commit 302125e

Please sign in to comment.