Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes to upcheck api #1333

Merged
merged 7 commits into from
Sep 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
query = "delete from EncryptedRawTransaction where hash.hashBytes = :hash"),
@NamedQuery(
name = "EncryptedRawTransaction.Upcheck",
query = "select count(c) from EncryptedRawTransaction c"),
query = "select count(c.timestamp) from EncryptedRawTransaction c"),
@NamedQuery(
name = "EncryptedRawTransaction.FindAll",
query = "select ert from EncryptedRawTransaction ert order by ert.timestamp, ert.hash"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
query = "select et from EncryptedTransaction et order by et.timestamp,et.hash"),
@NamedQuery(
name = "EncryptedTransaction.Upcheck",
query = "select count(c) from EncryptedTransaction c")
query = "select count(c.timestamp) from EncryptedTransaction c")
})
@Entity
@Table(name = "ENCRYPTED_TRANSACTION")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,16 @@ public static void main(final String... args) throws Exception {
LOGGER.debug("Created BatchResendManager");

LOGGER.debug("Creating txn manager");
TransactionManager.create();
TransactionManager transactionManager = TransactionManager.create();
LOGGER.debug("Created txn manager");

LOGGER.debug("Validating if transaction table exists");
if (!transactionManager.upcheck()) {
throw new RuntimeException(
"The database has not been setup correctly. Please ensure transaction tables "
+ "are present and correct");
}

LOGGER.debug("Creating ScheduledServiceFactory");
ScheduledServiceFactory scheduledServiceFactory = ScheduledServiceFactory.fromConfig(config);
scheduledServiceFactory.build();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package com.quorum.tessera.api.common;

import com.quorum.tessera.transaction.TransactionManager;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.ExampleObject;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.tags.Tags;
import java.util.Objects;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
Expand All @@ -25,13 +23,6 @@ public class UpCheckResource {
private static final Logger LOGGER = LoggerFactory.getLogger(UpCheckResource.class);

private static final String UPCHECK_RESPONSE_IS_UP = "I'm up!";
private static final String UPCHECK_RESPONSE_DB = "Database unavailable";

private final TransactionManager transactionManager;

public UpCheckResource(final TransactionManager transactionManager) {
this.transactionManager = Objects.requireNonNull(transactionManager);
}

/**
* Called to check if the application is running and responsive. Gives no details about the health
Expand All @@ -51,18 +42,12 @@ public UpCheckResource(final TransactionManager transactionManager) {
mediaType = MediaType.TEXT_PLAIN,
schema = @Schema(type = "string"),
examples = {
@ExampleObject(name = UPCHECK_RESPONSE_IS_UP, value = UPCHECK_RESPONSE_IS_UP),
@ExampleObject(name = UPCHECK_RESPONSE_DB, value = UPCHECK_RESPONSE_DB)
@ExampleObject(name = UPCHECK_RESPONSE_IS_UP, value = UPCHECK_RESPONSE_IS_UP)
}))
@GET
@Produces(MediaType.TEXT_PLAIN)
public Response upCheck() {
LOGGER.info("GET upcheck");

if (!transactionManager.upcheck()) {
return Response.ok(UPCHECK_RESPONSE_DB).build();
}

return Response.ok(UPCHECK_RESPONSE_IS_UP).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,49 +3,17 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.*;

import com.quorum.tessera.transaction.TransactionManager;
import javax.ws.rs.core.Response;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class UpCheckResourceTest {

private UpCheckResource resource;

private TransactionManager transactionManager;

@Before
public void onSetup() {
this.transactionManager = mock(TransactionManager.class);

this.resource = new UpCheckResource(transactionManager);
}

@After
public void onTearDown() {
verifyNoMoreInteractions(transactionManager);
}
private UpCheckResource resource = new UpCheckResource();

@Test
public void upcheck() {
when(transactionManager.upcheck()).thenReturn(true);

public void upCheck() {
final Response response = resource.upCheck();
assertThat(response.getStatus()).isEqualTo(200);
assertThat(response.getEntity()).isEqualTo("I'm up!");

verify(transactionManager).upcheck();
}

@Test
public void upcheckWhenDBNotReady() {
when(transactionManager.upcheck()).thenReturn(false);

final Response response = resource.upCheck();
assertThat(response.getStatus()).isEqualTo(200);
assertThat(response.getEntity()).isEqualTo("Database unavailable");

verify(transactionManager).upcheck();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public Set<Object> getSingletons() {
new TransactionResource(
transactionManager, batchResendManager, payloadEncoder, legacyResendManager);

final UpCheckResource upCheckResource = new UpCheckResource(transactionManager);
final UpCheckResource upCheckResource = new UpCheckResource();

final PrivacyGroupResource privacyGroupResource = new PrivacyGroupResource(privacyGroupManager);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public Set<Object> getSingletons() {
new RawTransactionResource(transactionManager);
final PartyInfoResource partyInfoResource = new PartyInfoResource(discovery);
final KeyResource keyResource = new KeyResource();
final UpCheckResource upCheckResource = new UpCheckResource(transactionManager);
final UpCheckResource upCheckResource = new UpCheckResource();
return Set.of(rawTransactionResource, partyInfoResource, keyResource, upCheckResource);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public Set<Object> getSingletons() {
RawTransactionResource rawTransactionResource = new RawTransactionResource(transactionManager);
EncodedPayloadResource encodedPayloadResource =
new EncodedPayloadResource(encodedPayloadManager, transactionManager);
final UpCheckResource upCheckResource = new UpCheckResource(transactionManager);
final UpCheckResource upCheckResource = new UpCheckResource();

final PrivacyGroupResource privacyGroupResource = new PrivacyGroupResource(privacyGroupManager);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ static Config createNode(NodeAlias nodeAlias) {
setUsername("junit");
setPassword("junit");
setUrl("jdbc:h2:mem:thirdpty".concat(nodeAlias.name()));
setAutoCreateTables(true);
}
});

Expand Down