Skip to content

Commit

Permalink
Polish and Refactor failing database Unit Tests #79
Browse files Browse the repository at this point in the history
Signed-off-by: Donald F. Coffin <[email protected]>
  • Loading branch information
dfcoffin committed Jan 25, 2024
1 parent 97283c2 commit d0511e2
Show file tree
Hide file tree
Showing 4 changed files with 237 additions and 171 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import org.greenbuttonalliance.gbaresourceserver.common.model.DateTimeInterval;
import org.greenbuttonalliance.gbaresourceserver.usage.model.ApplicationInformation;
import org.greenbuttonalliance.gbaresourceserver.usage.model.Authorization;
import org.greenbuttonalliance.gbaresourceserver.usage.model.RetailCustomer;
import org.greenbuttonalliance.gbaresourceserver.usage.model.Subscription;
import org.greenbuttonalliance.gbaresourceserver.usage.model.enums.AuthorizationStatus;
import org.greenbuttonalliance.gbaresourceserver.usage.model.enums.DataCustodianApplicationStatus;
import org.greenbuttonalliance.gbaresourceserver.usage.model.enums.GrantType;
Expand All @@ -31,7 +33,6 @@
import org.greenbuttonalliance.gbaresourceserver.usage.model.enums.TokenEndpointMethod;
import org.greenbuttonalliance.gbaresourceserver.usage.model.enums.TokenType;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -54,14 +55,32 @@
import static org.assertj.core.api.Assertions.*;

@Testcontainers
@DataJpaTest(showSql = true)
@DataJpaTest(showSql = false)
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class AuthorizationRepositoryTest {
private final AuthorizationRepository authorizationRepository;

private static final String PRESENT_SELF_LINK =
private static final String AUTHORIZATION_SELF_LINK =
"https://localhost:8080/DataCustodian/espi/1_1/resource/Authorization/123456";
private static final String AUTHORIZATION_UP_LINK =
"https://localhost:8080/DataCustodian/espi/1_1/resource/Authorization";

private static final String RETAILCUSTOMER_SELF_LINK =
"https://localhost:8080/DataCustodian/espi/1_1/resource/RetailCustomer/654321";
private static final String RETAILCUSTOMER_UP_LINK =
"https://localhost:8080/DataCustodian/espi/1_1/resource/RetailCustomer";

private static final String APPLICATIONINFORMATION_SELF_LINK =
"https://localhost:8080/DataCustodian/espi/1_1/resource/ApplicationInformation/234567";
private static final String APPLICATIONINFORMATION_UP_LINK =
"https://localhost:8080/DataCustodian/espi/1_1/resource/ApplicationInformation";

private static final String SUBSCRIPTION_SELF_LINK =
"https://localhost:8080/DataCustodian/espi/1_1/resource/Subscription/234651";
private static final String SUBSCRIPTION_UP_LINK =
"https://localhost:8080/DataCustodian/espi/1_1/resource/Subscription";

private static final String NOT_PRESENT_SELF_LINK = "foobar";
private static final String DUMMY_STRING = "test1";
private static final DateTimeFormatter SQL_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
Expand All @@ -84,7 +103,7 @@ void connectionEstablished() {

@Test
public void findByPresentId_returnsMatching() {
UUID presentUuid = UuidCreator.getNameBasedSha1(UuidCreator.NAMESPACE_URL, PRESENT_SELF_LINK);
UUID presentUuid = UuidCreator.getNameBasedSha1(UuidCreator.NAMESPACE_URL, AUTHORIZATION_SELF_LINK);
UUID foundUuid = authorizationRepository.findById(presentUuid).map(Authorization::getUuid).orElse(null);

Assertions.assertEquals(
Expand Down Expand Up @@ -118,31 +137,57 @@ public void findAll_returnsAll() {
}

//TODO: Fix code in order to compile successfully
@Test
public void entityMappings_areNotNull() {

// Extract fetching authorization logic into a method
Authorization fullyMappedAuthorization = authorizationRepository.
findById(UuidCreator.getNameBasedSha1(UuidCreator.NAMESPACE_URL, PRESENT_SELF_LINK)).orElse(null);
Assumptions.assumeTrue(fullyMappedAuthorization != null);

// @Test
// public void entityMappings_areNotNull() {
//
// // Extract fetching authorization logic into a method
// Authorization fullyMappedAuthorization = authorizationRepository.
// findById(UuidCreator.getNameBasedSha1(UuidCreator.NAMESPACE_URL, AUTHORIZATION_SELF_LINK)).orElse(null);
// Assumptions.assumeTrue(fullyMappedAuthorization != null);
//
// Assertions.assertAll(
// STR."Entity mapping failures for customer account \{fullyMappedAuthorization.getUuid()}",
// Stream.of((Function<Authorization, Optional<Subscription>>) auth -> {
// return Optional.ofNullable(auth.getSubscription());
// })
// .map(mappingFunc -> () -> Assertions.assertTrue(mappingFunc.apply(fullyMappedAuthorization).isPresent()))
// );
// }

@Test
public void entityMappings_areNotNull_Copilot_Test() {
// Fetch the first Authorization object from the test data
Authorization fullyMappedAuthorization = authorizationRepository.findAll().get(0);

// Assert that the Subscription object in the Authorization object is not null
Assertions.assertNotNull(fullyMappedAuthorization.getSubscription(),
"Subscription in Authorization is null");
}

private static List<Authorization> buildTestData() {
RetailCustomer rc = RetailCustomer.builder()
.uuid(UuidCreator.getNameBasedSha1(UuidCreator.NAMESPACE_URL, RETAILCUSTOMER_SELF_LINK))
.description("Retail Customer Description")
.published(LocalDateTime.parse("2014-01-02 05:00:00", SQL_FORMATTER))
.selfLinkHref(RETAILCUSTOMER_SELF_LINK)
.upLinkHref(RETAILCUSTOMER_UP_LINK)
.updated(LocalDateTime.parse("2014-01-02 05:00:00", SQL_FORMATTER))
.enabled(Boolean.TRUE)
.firstName("John")
.lastName("Doe")
.password("password")
.role("ROLE_USER")
.username("jdoe")
.build();


List<Authorization> authorizations = Collections.singletonList(
Authorization.builder()
.uuid(UUID.randomUUID())
.uuid(UuidCreator.getNameBasedSha1(UuidCreator.NAMESPACE_URL, AUTHORIZATION_SELF_LINK))
.description("Green Button Alliance Data Custodian Authorization")
.published(LocalDateTime.parse("2014-01-02 05:00:00", SQL_FORMATTER))
.selfLinkHref(PRESENT_SELF_LINK)
.upLinkHref("https://localhost:8080/DataCustodian/espi/1_1/resource/Authorization")
.selfLinkHref(AUTHORIZATION_SELF_LINK)
.upLinkHref(AUTHORIZATION_UP_LINK)
.updated(LocalDateTime.parse("2014-01-02 05:00:00", SQL_FORMATTER))
.accessToken(DUMMY_STRING)
.authorizedPeriod(new DateTimeInterval()
Expand All @@ -158,25 +203,22 @@ private static List<Authorization> buildTestData() {
.resourceUri("resourceUri")
.authorizationUri("authorizationUri")
.customerResourceUri("customerResourceUri")
// .applicationInformationId(UUID.randomUUID())
// .retailCustomerId(UUID.randomUUID())
// .subscriptionId(UUID.randomUUID())
.build()
);

// hydrate UUIDs and entity mappings
authorizations.forEach(auth -> {
auth.setUuid(UuidCreator.getNameBasedSha1(UuidCreator.NAMESPACE_URL, auth.getSelfLinkHref()));

// Subscription sub = auth.getSubscription();
// sub.setUuid(UuidCreator.getNameBasedSha1(UuidCreator.NAMESPACE_URL, auth.getSelfLinkHref()));
// sub.setAuthorization(auth);
Subscription sub = auth.getSubscription();
sub.setUuid(UuidCreator.getNameBasedSha1(UuidCreator.NAMESPACE_URL, auth.getSelfLinkHref()));
sub.setAuthorization(auth);

ApplicationInformation ai = ApplicationInformation.builder()
.uuid(UuidCreator.getNameBasedSha1(UuidCreator.NAMESPACE_URL, APPLICATIONINFORMATION_SELF_LINK))
.description(DUMMY_STRING)
.published(LocalDateTime.parse("2014-01-02 05:00:00", SQL_FORMATTER))
.selfLinkHref("https://localhost:8080/DataCustodian/espi/1_1/resource/ApplicationInformation/234567")
.upLinkHref("https://localhost:8080/DataCustodian/espi/1_1/resource/ApplicationInformation")
.selfLinkHref(APPLICATIONINFORMATION_SELF_LINK)
.upLinkHref(APPLICATIONINFORMATION_UP_LINK)
.updated(LocalDateTime.parse("2014-01-02 05:00:00", SQL_FORMATTER))
.authorizationServerAuthorizationEndpoint(DUMMY_STRING)
.authorizationServerRegistrationEndpoint(null)
Expand Down Expand Up @@ -223,7 +265,6 @@ private static List<Authorization> buildTestData() {
"Scope4"
)))
.build();
ai.setUuid(UuidCreator.getNameBasedSha1(UuidCreator.NAMESPACE_URL, auth.getSelfLinkHref()));
auth.setApplicationInformation(ai);
// sub.setRetailCustomer(RetailCustomer.builder().build());
// sub.getRetailCustomer().setUuid(UuidCreator.getNameBasedSha1(UuidCreator.NAMESPACE_URL, sub.getSelfLinkHref()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ void connectionEstablished() {

@Test
public void findByPresentId_returnsMatching() {
Long presentId = PRESENT;
Long presentId = PRESENT + 1;
Long foundId = lineItemRepository.findById(presentId).map(LineItem::getId).orElse(null);

Assertions.assertEquals(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public class RetailCustomerRepositoryTest {
private final RetailCustomerRepository retailCustomerRepository;

// for testing findById
private static final String PRESENT_SELF_LINK = "https://{domain}/espi/1_1/resource/RetailCustomer/08";
private static final String PRESENT_SELF_LINK = "https://localhost:8080/DataCustodian/espi/1_1/resource/RetailCustomer/08";
private static final String NOT_PRESENT_SELF_LINK = "foobar";
private static final DateTimeFormatter SQL_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
private static final String DUMMY_STRING = "dummy";
Expand Down Expand Up @@ -148,21 +148,21 @@ private static List<RetailCustomer> buildTestData() {
.description("Type of Meter Reading Data")
.published(LocalDateTime.parse("2012-10-24 04:00:00", SQL_FORMATTER))
.selfLinkHref(PRESENT_SELF_LINK)
.upLinkHref("https://{domain}/espi/1_1/resource/RetailCustomer")
.upLinkHref("https://localhost:8080/DataCustodian/espi/1_1/resource/RetailCustomer")
.updated(LocalDateTime.parse("2012-10-24 04:00:00", SQL_FORMATTER))
.enabled(Boolean.TRUE)
.firstName("hello")
.lastName("last")
.password("password")
.role("hatever")
.role("whatever")
.username("Username")
.subscriptions(new HashSet<>(
Collections.singletonList(
Subscription.builder()
.description("description")
.published(LocalDateTime.parse("2012-10-24 04:00:00", SQL_FORMATTER))
.selfLinkHref("https://{domain}/espi/1_1/resource/Subscription/176")
.upLinkHref("https://{domain}/DataCustodian/espi/1_1/resource/Subscription")
.selfLinkHref("https://localhost:8080/DataCustodian/espi/1_1/resource/Subscription/176")
.upLinkHref("https://localhost:8080/DataCustodian/DataCustodian/espi/1_1/resource/Subscription")
.updated(LocalDateTime.parse("2012-10-24 04:00:00", SQL_FORMATTER))
.hashedId("hashedId")
.lastUpdate(LocalDateTime.parse("2012-10-24 04:00:00", SQL_FORMATTER))
Expand All @@ -172,7 +172,7 @@ private static List<RetailCustomer> buildTestData() {
.description("Green Button Alliance Data Custodian Authorization")
.published(LocalDateTime.parse("2014-01-02 05:00:00", SQL_FORMATTER))
.selfLinkHref(PRESENT_SELF_LINK)
.upLinkHref("https://{domain}/DataCustodian/espi/1_1/resource/ApplicationInformation")
.upLinkHref("https://localhost:8080/DataCustodian/DataCustodian/espi/1_1/resource/ApplicationInformation")
.updated(LocalDateTime.parse("2014-01-02 05:00:00", SQL_FORMATTER))
.authorizedPeriod(new DateTimeInterval()
.setDuration(21L)
Expand Down Expand Up @@ -200,7 +200,7 @@ private static List<RetailCustomer> buildTestData() {
.description("Type of Meter Reading Data")
.published(LocalDateTime.parse("2012-10-24 04:00:00", SQL_FORMATTER))
.selfLinkHref(PRESENT_SELF_LINK)
.upLinkHref("https://{domain}/espi/1_1/resource/RetailCustomer")
.upLinkHref("https://localhost:8080/DataCustodian/espi/1_1/resource/RetailCustomer")
.updated(LocalDateTime.parse("2012-10-24 04:00:00", SQL_FORMATTER))
.enabled(Boolean.TRUE)
.firstName("hello")
Expand All @@ -219,8 +219,8 @@ private static List<RetailCustomer> buildTestData() {
RetailCustomer.builder()
.description("Hourly Wh Received")
.published(LocalDateTime.parse("2014-01-31 05:00:00", SQL_FORMATTER))
.selfLinkHref("https://{domain}/espi/1_1/resource/RetailCustomer/1")
.upLinkHref("https://{domain}/espi/1_1/resource/RetailCustomer")
.selfLinkHref("https://localhost:8080/DataCustodian/espi/1_1/resource/RetailCustomer/1")
.upLinkHref("https://localhost:8080/DataCustodian/espi/1_1/resource/RetailCustomer")
.updated(LocalDateTime.parse("2014-01-31 05:00:00", SQL_FORMATTER))
.enabled(Boolean.FALSE)
.firstName("First")
Expand All @@ -233,8 +233,8 @@ private static List<RetailCustomer> buildTestData() {
Subscription.builder()
.description("description")
.published(LocalDateTime.parse("2012-10-24 04:00:00", SQL_FORMATTER))
.selfLinkHref("https://{domain}/espi/1_1/resource/Subscription/176")
.upLinkHref("https://{domain}/DataCustodian/espi/1_1/resource/Subscription")
.selfLinkHref("https://localhost:8080/DataCustodian/espi/1_1/resource/Subscription/176")
.upLinkHref("https://localhost:8080/DataCustodian/DataCustodian/espi/1_1/resource/Subscription")
.updated(LocalDateTime.parse("2012-10-24 04:00:00", SQL_FORMATTER))
.hashedId("hashedId")
.lastUpdate(LocalDateTime.parse("2012-10-24 04:00:00", SQL_FORMATTER))
Expand All @@ -244,7 +244,7 @@ private static List<RetailCustomer> buildTestData() {
.description("Green Button Alliance Data Custodian Authorization")
.published(LocalDateTime.parse("2014-01-02 05:00:00", SQL_FORMATTER))
.selfLinkHref(PRESENT_SELF_LINK)
.upLinkHref("https://{domain}/DataCustodian/espi/1_1/resource/ApplicationInformation")
.upLinkHref("https://localhost:8080/DataCustodian/DataCustodian/espi/1_1/resource/ApplicationInformation")
.updated(LocalDateTime.parse("2014-01-02 05:00:00", SQL_FORMATTER))
.authorizedPeriod(new DateTimeInterval()
.setDuration(21L)
Expand Down Expand Up @@ -272,7 +272,7 @@ private static List<RetailCustomer> buildTestData() {
.description("Type of Meter Reading Data")
.published(LocalDateTime.parse("2012-10-24 04:00:00", SQL_FORMATTER))
.selfLinkHref(PRESENT_SELF_LINK)
.upLinkHref("https://{domain}/espi/1_1/resource/RetailCustomer")
.upLinkHref("https://localhost:8080/DataCustodian/espi/1_1/resource/RetailCustomer")
.updated(LocalDateTime.parse("2012-10-24 04:00:00", SQL_FORMATTER))
.enabled(Boolean.TRUE)
.firstName("hello")
Expand All @@ -291,22 +291,22 @@ private static List<RetailCustomer> buildTestData() {
RetailCustomer.builder()
.description("Hourly Wh Delivered")
.published(LocalDateTime.parse("2013-05-28 07:00:00", SQL_FORMATTER))
.selfLinkHref("https://{domain}/espi/1_1/resource/RetailCustomer/2")
.upLinkHref("https://{domain}/espi/1_1/resource/RetailCustomer")
.selfLinkHref("https://localhost:8080/DataCustodian/espi/1_1/resource/RetailCustomer/2")
.upLinkHref("https://localhost:8080/DataCustodian/espi/1_1/resource/RetailCustomer")
.updated(LocalDateTime.parse("2013-05-28 07:00:00", SQL_FORMATTER))
.enabled(Boolean.TRUE)
.firstName("First")
.lastName("again")
.password("password")
.role("whatever")
.username("aUsernam")
.username("aUsername")
.subscriptions(new HashSet<>(
Collections.singletonList(
Subscription.builder()
.description("description")
.published(LocalDateTime.parse("2012-10-24 04:00:00", SQL_FORMATTER))
.selfLinkHref("https://{domain}/espi/1_1/resource/Subscription/176")
.upLinkHref("https://{domain}/DataCustodian/espi/1_1/resource/Subscription")
.selfLinkHref("https://localhost:8080/DataCustodian/espi/1_1/resource/Subscription/176")
.upLinkHref("https://localhost:8080/DataCustodian/DataCustodian/espi/1_1/resource/Subscription")
.updated(LocalDateTime.parse("2012-10-24 04:00:00", SQL_FORMATTER))
.hashedId("hashedId")
.lastUpdate(LocalDateTime.parse("2012-10-24 04:00:00", SQL_FORMATTER))
Expand All @@ -316,7 +316,7 @@ private static List<RetailCustomer> buildTestData() {
.description("Green Button Alliance Data Custodian Authorization")
.published(LocalDateTime.parse("2014-01-02 05:00:00", SQL_FORMATTER))
.selfLinkHref(PRESENT_SELF_LINK)
.upLinkHref("https://{domain}/DataCustodian/espi/1_1/resource/ApplicationInformation")
.upLinkHref("https://localhost:8080/DataCustodian/DataCustodian/espi/1_1/resource/ApplicationInformation")
.updated(LocalDateTime.parse("2014-01-02 05:00:00", SQL_FORMATTER))
.authorizedPeriod(new DateTimeInterval()
.setDuration(21L)
Expand All @@ -341,10 +341,10 @@ private static List<RetailCustomer> buildTestData() {
.build()
)
.retailCustomer(RetailCustomer.builder()
.description("Type of Meter Reading Data")
.description("Retail Customer Data")
.published(LocalDateTime.parse("2012-10-24 04:00:00", SQL_FORMATTER))
.selfLinkHref(PRESENT_SELF_LINK)
.upLinkHref("https://{domain}/espi/1_1/resource/RetailCustomer")
.upLinkHref("https://localhost:8080/DataCustodian/espi/1_1/resource/RetailCustomer")
.updated(LocalDateTime.parse("2012-10-24 04:00:00", SQL_FORMATTER))
.enabled(Boolean.TRUE)
.firstName("hello")
Expand Down Expand Up @@ -375,8 +375,8 @@ private static List<RetailCustomer> buildTestData() {
ApplicationInformation ai = ApplicationInformation.builder()
.description(DUMMY_STRING)
.published(LocalDateTime.parse("2014-01-02 05:00:00", SQL_FORMATTER))
.selfLinkHref("https://{domain}/DataCustodian/espi/1_1/resource/ApplicationInformation/2")
.upLinkHref("https://{domain}/DataCustodian/espi/1_1/resource/ApplicationInformation")
.selfLinkHref("https://localhost:8080/DataCustodian/DataCustodian/espi/1_1/resource/ApplicationInformation/2")
.upLinkHref("https://localhost:8080/DataCustodian/DataCustodian/espi/1_1/resource/ApplicationInformation")
.updated(LocalDateTime.parse("2014-01-02 05:00:00", SQL_FORMATTER))
.authorizationServerAuthorizationEndpoint(DUMMY_STRING)
.authorizationServerRegistrationEndpoint(null)
Expand Down
Loading

0 comments on commit d0511e2

Please sign in to comment.