Skip to content

Commit

Permalink
Feature: Hibernate Setup (#62)
Browse files Browse the repository at this point in the history
* Added hibernate, postgres, lombok and flyway. Set java version to 17.

* Updated Open API config

* Added local docker compose to generate local db

* Updated application.yaml with settings for hibernate and flyway

* Added placeholder code for variables needed from key vault

* Added SQL script to generate the database via flyway

* Updated values.yaml to get correct environment variables from key vault

* Fixed environment vars in docker-compose-local.yml

* Updated README with details for running local docker image

* Hibernate and rest controller for recording

* checkstyle fix

* Remove old code for old db + created enums for new db types

* Remove old db schema

* Fix Application.java re-added

* Add updated hibernate entities

* Add updated hibernate repositories

* Move id field out of each entity into base entity class

* Remove unused imports

* Remove setting fields to false

* Fix Class cannot have field of same name

* Add PMD suppressing on the enums

* Fix spacing

* Fix editInstruction column with type json

* Add tests for each of the entities and a helper class to generate entities

* Fix checkstyle and pmd

* Added hibernate, postgres, lombok and flyway. Set java version to 17.

* Updated Open API config

* Added local docker compose to generate local db

* Updated application.yaml with settings for hibernate and flyway

* Added placeholder code for variables needed from key vault

* Added SQL script to generate the database via flyway

* Fixed environment vars in docker-compose-local.yml

* Updated README with details for running local docker image

* Hibernate and rest controller for recording

* checkstyle fix

* removed duplicate flyway gradle tasks

* Bumping chart version/ fixing aliases

* Remove old code for old db

* Remove old code for old db

* Fix Enum types work correctly now

* Remove BookingParticipant entity and replaced with many to many

* Update entities to match new schema changes

* Update entities to match new schema changes (deleted -> deleted_at)

* Fix Tests for updated entities

* Styling

* Styling

* Update entities to match new schema changes (capture session, recording)

* Revert readme changes

* Update build.gradle

* Move schema file db.migrations -> db.migration

* Revert springboot version to fix enum errors

* Move tests to integration testing

* Remove timezone setting

* Add comment for downgraded spring plugin

* Add default test user to entity factory

* Capitalize enums in db and code

* Update column definition default

---------

Co-authored-by: Jason Paige <[email protected]>
Co-authored-by: hmcts-jenkins-cnp <60659747+hmcts-jenkins-cnp[bot]@users.noreply.github.com>
Co-authored-by: Jason Paige <[email protected]>
  • Loading branch information
4 people authored Nov 30, 2023
1 parent 6a93fed commit 5c171a5
Show file tree
Hide file tree
Showing 64 changed files with 1,978 additions and 9 deletions.
11 changes: 8 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ plugins {
id 'pmd'
id 'jacoco'
id 'io.spring.dependency-management' version '1.1.4'
id 'org.springframework.boot' version '3.2.0'
id 'org.springframework.boot' version '3.1.5' // issues with 3.2.0 https://github.com/spring-projects/spring-framework/issues/31705
id 'org.owasp.dependencycheck' version '9.0.1'
id 'com.github.ben-manes.versions' version '0.50.0'
id 'org.sonarqube' version '4.4.1.3373'
id "io.freefair.lombok" version "8.3"
id 'org.flywaydb.flyway' version '9.22.3' // issues with 10.0.x https://github.com/spring-projects/spring-boot/issues/38164
}

Expand Down Expand Up @@ -226,8 +227,8 @@ dependencies {
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-actuator'
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-aop'
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-json'
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa'
implementation group: 'org.springdoc', name: 'springdoc-openapi-starter-webmvc-ui', version: '2.2.0'
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa'

implementation group: 'com.github.hmcts.java-logging', name: 'logging', version: '6.0.1'

Expand All @@ -236,12 +237,16 @@ dependencies {

implementation group: 'org.flywaydb', name: 'flyway-core', version: '9.22.3'
implementation group: 'org.postgresql', name: 'postgresql', version: '42.7.0'
implementation group: 'io.hypersistence', name: 'hypersistence-utils-hibernate-62', version: '3.6.1'

implementation group: 'io.rest-assured', name: 'rest-assured'

annotationProcessor('org.hibernate.orm:hibernate-jpamodelgen:6.3.1.Final')

testImplementation 'com.github.hmcts:fortify-client:1.3.0:all'
testImplementation 'org.springframework.security:spring-security-test:6.2.0'
testImplementation libraries.junit5

testImplementation group: 'org.springframework.boot', name: 'spring-boot-starter-test', {
exclude group: 'junit', module: 'junit'
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
Expand Down Expand Up @@ -291,5 +296,5 @@ rootProject.tasks.named("processSmokeTestResources") {
}

wrapper {
distributionType = Wrapper.DistributionType.ALL
distributionType = Wrapper.DistributionType.ALL
}
2 changes: 1 addition & 1 deletion charts/pre-api/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ appVersion: "1.0"
description: A Helm chart for pre-api App
name: pre-api
home: https://github.com/hmcts/pre-api
version: 0.0.19
version: 0.0.20
maintainers:
- name: HMCTS pre team
dependencies:
Expand Down
1 change: 0 additions & 1 deletion charts/pre-api/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ java:
image: 'sdshmctspublic.azurecr.io/pre/api:latest'
ingressHost: pre-api-{{ .Values.global.environment }}.service.core-compute-{{ .Values.global.environment }}.internal
aadIdentityName: pre
# Uncomment once the vault containing the app insights key has been set up
keyVaults:
pre:
secrets:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package uk.gov.hmcts.reform.preapi.entities;

import jakarta.persistence.EntityManager;
import jakarta.transaction.Transactional;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import uk.gov.hmcts.reform.preapi.Application;
import uk.gov.hmcts.reform.preapi.enums.CourtType;

import java.sql.Date;
import java.sql.Timestamp;

import static org.junit.jupiter.api.Assertions.assertEquals;

@SpringBootTest(classes = Application.class)
class AppAccessTest {

@Autowired
private EntityManager entityManager;

@Test
@Transactional
public void testSaveAndRetrieveAppAccess() { //NOPMD - suppressed JUnit5TestShouldBePackagePrivate
User user = HelperFactory.createUser(
"Test",
"User",
"[email protected]",
new Timestamp(System.currentTimeMillis()),
null,
null
);
entityManager.persist(user);

Court court = HelperFactory.createCourt(CourtType.CROWN, "Test Court", "Test123");
entityManager.persist(court);

Role role = HelperFactory.createRole("TestRole");
entityManager.persist(role);

AppAccess appAccess = HelperFactory.createAppAccess(
user,
court,
role,
true,
new Timestamp(System.currentTimeMillis()),
new Date(System.currentTimeMillis())
);

entityManager.persist(appAccess);
entityManager.flush();

AppAccess retrievedAppAccess = entityManager.find(AppAccess.class, appAccess.getId());

assertEquals(appAccess.getId(), retrievedAppAccess.getId(), "Ids should match");
assertEquals(appAccess.getUser(), retrievedAppAccess.getUser(), "Users should match");
assertEquals(appAccess.getCourt(), retrievedAppAccess.getCourt(), "Courts should match");
assertEquals(appAccess.getRole(), retrievedAppAccess.getRole(), "Roles should match");
assertEquals(appAccess.getLastAccess(), retrievedAppAccess.getLastAccess(), "Last access should match");
assertEquals(appAccess.isActive(), retrievedAppAccess.isActive(), "Active status should match");
assertEquals(appAccess.getDeletedAt(), retrievedAppAccess.getDeletedAt(), "Deleted at should match");
assertEquals(appAccess.getCreatedAt(), retrievedAppAccess.getCreatedAt(), "Created at should match");
assertEquals(appAccess.getModifiedAt(), retrievedAppAccess.getModifiedAt(), "Modified at should match");
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package uk.gov.hmcts.reform.preapi.entities;

import jakarta.persistence.EntityManager;
import jakarta.transaction.Transactional;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import uk.gov.hmcts.reform.preapi.Application;
import uk.gov.hmcts.reform.preapi.enums.AuditLogSource;
import uk.gov.hmcts.reform.preapi.enums.AuditLogType;

import java.sql.Timestamp;
import java.util.UUID;

import static org.junit.jupiter.api.Assertions.assertEquals;

@SpringBootTest(classes = Application.class)
class AuditTest {

@Autowired
private EntityManager entityManager;

@Test
@Transactional
public void testSaveAndRetrieveAudit() { //NOPMD - suppressed JUnit5TestShouldBePackagePrivate
Audit audit = new Audit();
audit.setTableName("TestTable");
audit.setTableRecordId(UUID.randomUUID());
audit.setSource(AuditLogSource.PORTAL);
audit.setType(AuditLogType.CREATE);
audit.setCategory("TestCategory");
audit.setActivity("TestActivity");
audit.setFunctionalArea("TestFunctionalArea");
audit.setAuditDetails("TestAuditDetails");
audit.setCreatedBy("TestUser");
audit.setUpdatedAt(new Timestamp(System.currentTimeMillis()));
entityManager.persist(audit);
entityManager.flush();

Audit retrievedAudit = entityManager.find(Audit.class, audit.getId());

assertEquals(audit.getId(), retrievedAudit.getId(), "Id should match");
assertEquals(audit.getTableName(), retrievedAudit.getTableName(), "Table names should match");
assertEquals(audit.getTableRecordId(), retrievedAudit.getTableRecordId(), "Record ids should match");
assertEquals(audit.getSource(), retrievedAudit.getSource(), "Source should match");
assertEquals(audit.getType(), retrievedAudit.getType(), "Type should match");
assertEquals(audit.getCategory(), retrievedAudit.getCategory(), "Category should match");
assertEquals(audit.getActivity(), retrievedAudit.getActivity(), "Activity should match");
assertEquals(audit.getFunctionalArea(), retrievedAudit.getFunctionalArea(), "Functional area should match");
assertEquals(audit.getAuditDetails(), retrievedAudit.getAuditDetails(), "Audit details should match");
assertEquals(audit.getCreatedBy(), retrievedAudit.getCreatedBy(), "Created by should match");
assertEquals(audit.getCreatedAt(), retrievedAudit.getCreatedAt(), "Created at should match");
assertEquals(audit.getUpdatedAt(), retrievedAudit.getUpdatedAt(), "Updated at should match");
assertEquals(audit.getDeletedAt(), retrievedAudit.getDeletedAt(), "Deleted at should match");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package uk.gov.hmcts.reform.preapi.entities;


import jakarta.persistence.EntityManager;
import jakarta.transaction.Transactional;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import uk.gov.hmcts.reform.preapi.Application;
import uk.gov.hmcts.reform.preapi.enums.CourtType;

import java.sql.Timestamp;

import static org.junit.jupiter.api.Assertions.assertEquals;

@SpringBootTest(classes = Application.class)
class BookingTest {

@Autowired
private EntityManager entityManager;

@Test
@Transactional
public void testSaveAndRetrieveBooking() { //NOPMD - suppressed JUnit5TestShouldBePackagePrivate
Timestamp now = new Timestamp(System.currentTimeMillis());
Court court = HelperFactory.createCourt(CourtType.CROWN, "Test Court", null);
entityManager.persist(court);

Case testCase = HelperFactory.createCase(court, "ref1234", true, now);
entityManager.persist(testCase);

Booking booking = HelperFactory.createBooking(testCase, now, now);
entityManager.persist(booking);
entityManager.flush();

Booking retrievedBooking = entityManager.find(Booking.class, booking.getId());

assertEquals(booking.getId(), retrievedBooking.getId(), "Id should match");
assertEquals(booking.getCaseId(), retrievedBooking.getCaseId(), "Case should match");
assertEquals(booking.getScheduledFor(), retrievedBooking.getScheduledFor(), "Scheduled for should match");
assertEquals(booking.getDeletedAt(), retrievedBooking.getDeletedAt(), "Deleted at should match");
assertEquals(booking.getCreatedAt(), retrievedBooking.getCreatedAt(), "Created at should match");
assertEquals(booking.getModifiedAt(), retrievedBooking.getModifiedAt(), "Modified at should match");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package uk.gov.hmcts.reform.preapi.entities;

import jakarta.persistence.EntityManager;
import jakarta.transaction.Transactional;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import uk.gov.hmcts.reform.preapi.Application;
import uk.gov.hmcts.reform.preapi.enums.CourtType;
import uk.gov.hmcts.reform.preapi.enums.RecordingOrigin;
import uk.gov.hmcts.reform.preapi.enums.RecordingStatus;

import java.sql.Timestamp;
import java.time.LocalDateTime;

import static org.junit.jupiter.api.Assertions.assertEquals;

@SpringBootTest(classes = Application.class)
class CaptureSessionTest {

@Autowired
private EntityManager entityManager;

@Test
@Transactional
public void testSaveAndRetrieveCaptureSession() { //NOPMD - suppressed JUnit5TestShouldBePackagePrivate
Court court = HelperFactory.createCourt(CourtType.CROWN, "Test Court", null);
entityManager.persist(court);

Case testCase = HelperFactory.createCase(court, "ref1234", true, new Timestamp(System.currentTimeMillis()));
entityManager.persist(testCase);

Booking booking = HelperFactory.createBooking(
testCase,
Timestamp.valueOf(LocalDateTime.now()),
new Timestamp(System.currentTimeMillis())
);
entityManager.persist(booking);

User user = HelperFactory.createDefaultTestUser();
entityManager.persist(user);

CaptureSession captureSession = HelperFactory.createCaptureSession(
booking,
RecordingOrigin.PRE,
"TestIngrestAddress",
"TestLiveOutputAddress",
new Timestamp(System.currentTimeMillis()),
user,
new Timestamp(System.currentTimeMillis()),
user,
RecordingStatus.FINISHED,
new Timestamp(System.currentTimeMillis())
);
entityManager.persist(captureSession);
entityManager.flush();

CaptureSession retrievedCaptureSession = entityManager.find(CaptureSession.class, captureSession.getId());

assertEquals(captureSession.getId(), retrievedCaptureSession.getId(), "Id should match");
assertEquals(captureSession.getBooking(), retrievedCaptureSession.getBooking(), "Booking should match");
assertEquals(captureSession.getOrigin(), retrievedCaptureSession.getOrigin(), "Origin should match");
assertEquals(
captureSession.getIngestAddress(),
retrievedCaptureSession.getIngestAddress(),
"Ingest address should match"
);
assertEquals(
captureSession.getLiveOutputUrl(),
retrievedCaptureSession.getLiveOutputUrl(),
"Live output url should match"
);
assertEquals(captureSession.getStartedAt(), retrievedCaptureSession.getStartedAt(), "Started at should match");
assertEquals(
captureSession.getStartedByUser(),
retrievedCaptureSession.getStartedByUser(),
"Started by by user should match"
);
assertEquals(
captureSession.getFinishedAt(),
retrievedCaptureSession.getFinishedAt(),
"Finished at should match"
);
assertEquals(
captureSession.getFinishedByUserId(),
retrievedCaptureSession.getFinishedByUserId(),
"Finished by user should match"
);
assertEquals(captureSession.getStatus(), retrievedCaptureSession.getStatus(), "Status should match");
assertEquals(captureSession.getDeletedAt(), retrievedCaptureSession.getDeletedAt(), "Deleted at should match");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package uk.gov.hmcts.reform.preapi.entities;

import jakarta.persistence.EntityManager;
import jakarta.transaction.Transactional;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import uk.gov.hmcts.reform.preapi.Application;
import uk.gov.hmcts.reform.preapi.enums.CourtType;

import java.sql.Timestamp;

import static org.junit.jupiter.api.Assertions.assertEquals;

@SpringBootTest(classes = Application.class)
class CaseTest {

@Autowired
private EntityManager entityManager;

@Test
@Transactional
public void testSaveAndRetrieveCase() { //NOPMD - suppressed JUnit5TestShouldBePackagePrivate
Court court = HelperFactory.createCourt(CourtType.CROWN, "Test Court", null);
entityManager.persist(court);

Case testCase = HelperFactory.createCase(court, "ref1234", true, new Timestamp(System.currentTimeMillis()));
entityManager.persist(testCase);
entityManager.flush();

Case retrievedCase = entityManager.find(Case.class, testCase.getId());

assertEquals(testCase.getId(), retrievedCase.getId(), "Id should match");
assertEquals(testCase.getCourt(), retrievedCase.getCourt(), "Court should match");
assertEquals(testCase.getReference(), retrievedCase.getReference(), "Case reference should match");
assertEquals(testCase.isTest(), retrievedCase.isTest(), "Test status should match");
assertEquals(testCase.getDeletedAt(), retrievedCase.getDeletedAt(), "Deleted at should match");
assertEquals(testCase.getCreatedAt(), retrievedCase.getCreatedAt(), "Created at should match");
assertEquals(testCase.getModifiedAt(), retrievedCase.getModifiedAt(), "Modified at should match");
}
}
Loading

0 comments on commit 5c171a5

Please sign in to comment.