Skip to content

Commit

Permalink
cleaned up transaction controller and unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hunteryavitz committed Nov 8, 2023
1 parent 0a6752a commit f4c0244
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.hunteryavitz.blockchainapi.entities.Transaction;
import com.hunteryavitz.blockchainapi.services.HealthMetricService;
import com.hunteryavitz.blockchainapi.services.TransactionService;
import jakarta.websocket.server.PathParam;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

Expand Down Expand Up @@ -50,11 +51,16 @@ public TransactionController(TransactionService transactionService, HealthMetric
/**
* The getTransactionPool method is responsible for returning the transaction pool.
* @param transaction The transaction to be added to the transaction pool.
* @param test test query param.
* @return The transaction pool.
*/
@PostMapping(value = "/submitTransaction", consumes = "application/json", produces = "application/json")
public ResponseEntity<Boolean> submitTransaction(@RequestBody Transaction transaction) {
public ResponseEntity<Boolean> submitTransaction(@RequestBody Transaction transaction,
@PathParam("test") boolean test) {
try {
if (test) {
throw new Exception("Test exception");
}
transactionService.submitTransaction(transaction);
return ResponseEntity.ok(true);
} catch (Exception exception) {
Expand All @@ -66,11 +72,15 @@ public ResponseEntity<Boolean> submitTransaction(@RequestBody Transaction transa

/**
* The getTransactionPool method is responsible for returning the transaction pool.
* @param test test query param.
* @return The transaction pool.
*/
@GetMapping(value = "/getTransactionPool", produces = "application/json")
public ResponseEntity<Transaction[]> getTransactionPool() {
public ResponseEntity<Transaction[]> getTransactionPool(@PathParam("test") boolean test) {
try {
if (test) {
throw new Exception("Test exception");
}
return ResponseEntity.ok(TransactionService.getTransactionPool());
} catch (Exception exception) {
healthMetricService.updateHealth(ContaminationLevel.WARNING, exception);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package com.hunteryavitz.blockchainapi.controllers;

import com.hunteryavitz.blockchainapi.entities.Block;
import com.hunteryavitz.blockchainapi.entities.Transaction;
import com.hunteryavitz.blockchainapi.services.TransactionService;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
Expand All @@ -18,75 +15,89 @@
@TestPropertySource(locations = "classpath:application-test.properties")
public class TransactionControllerTests {

/**
* The TransactionService used to make requests to the API.
*/
@Mock
private TransactionService transactionService;

/**
* The RestTemplate used to make requests to the API.
*/
@Autowired
private TestRestTemplate restTemplate;

/**
* The API version and endpoints.
* The API version and controller.
*/
private static final String API_VERSION = "/api/v1";
private static final String API_VERSION = "/api/v1/transaction";

/**
* The submit transaction endpoint.
*/
private static final String SUBMIT_TRANSACTION_ENDPOINT = "/transaction/submitTransaction";
private static final String SUBMIT_TRANSACTION_ENDPOINT = "/submitTransaction";

/**
* The get transaction pool endpoint.
*/
private static final String GET_BLOCK_BY_INDEX_ENDPOINT = "/blockchain/getBlockById?id=1";
private static final String GET_TRANSACTION_POOL_ENDPOINT = "/getTransactionPool";

/**
* Tests the getBlockchain endpoint.
* The query param for testing.
*/
@Test
void testSubmitTransaction() {
private static final String QUERY_PARAM_TEST = "?test=true";

transactionService = new TransactionService();
transactionService.createInitialTransactionPool();
/**
* Tests the getBlockchain endpoint succeeds.
*/
@Test
void testSubmitTransaction_onSuccess_returns200AndTrue() {

Transaction transaction = new Transaction(999, "right_now", "your mom", "CREATED");

ResponseEntity<Boolean> response = restTemplate.postForEntity(
API_VERSION + SUBMIT_TRANSACTION_ENDPOINT, transaction, Boolean.class);
ResponseEntity<Boolean> response = restTemplate.postForEntity(API_VERSION
+ SUBMIT_TRANSACTION_ENDPOINT, transaction, Boolean.class);

System.out.println(response.getBody());
assert response.getStatusCode().is2xxSuccessful();
assert (Boolean.TRUE.equals(response.getBody()));
}

/**
* Tests the Transaction Service to add Block to Blockchain when full.
* Tests the getBlockchain endpoint fails.
*/
@Test
void testAddsBlockOnFullTransactionPool() {
void testSubmitTransaction_onFail_returns200AndFalse() {

transactionService = new TransactionService();
transactionService.createInitialTransactionPool();
Transaction transaction = new Transaction(999, "right_now", "your mom", "CREATED");
Transaction transaction = new Transaction(
999, "right_now", "your mom", "CREATED");

int transactionPoolLength = TransactionService.getTransactionPool().length;
ResponseEntity<Boolean> response = restTemplate.postForEntity(API_VERSION
+ SUBMIT_TRANSACTION_ENDPOINT
+ QUERY_PARAM_TEST, transaction, Boolean.class);

for (int i = 0; i < transactionPoolLength; i++) {
transactionService.submitTransaction(transaction);
}
System.out.println(response.getBody());
assert response.getStatusCode().is2xxSuccessful();
assert (Boolean.FALSE.equals(response.getBody()));
}

ResponseEntity<Block> response = restTemplate.getForEntity(
API_VERSION + GET_BLOCK_BY_INDEX_ENDPOINT, Block.class);
/**
* Tests the get transaction pool endpoint succeeds.
*/
@Test
void testGetTransactionPool_onSuccess_returns200AndTransactionPool() {

ResponseEntity<Transaction[]> response = restTemplate.getForEntity(API_VERSION
+ GET_TRANSACTION_POOL_ENDPOINT, Transaction[].class);

assert response.getStatusCode().is2xxSuccessful();
assert response.getBody() != null;
}

Block block = response.getBody();
System.out.println(block);
assert block != null;
/**
* Tests the get transaction pool endpoint fails.
*/
@Test
void testGetTransactionPool_onFail_returns200AndEmptyTransactionPool() {

ResponseEntity<Transaction[]> response = restTemplate.getForEntity(API_VERSION
+ GET_TRANSACTION_POOL_ENDPOINT
+ QUERY_PARAM_TEST, Transaction[].class);

assert response.getStatusCode().is2xxSuccessful();
assert response.getBody() != null;
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.hunteryavitz.blockchainapi.services;

import com.hunteryavitz.blockchainapi.entities.Block;
import com.hunteryavitz.blockchainapi.entities.Transaction;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.TestPropertySource;

/**
Expand All @@ -19,6 +22,8 @@ public class TransactionServiceTests {
@Mock
private TransactionService transactionService;

TestRestTemplate restTemplate = new TestRestTemplate();

/**
* Tests the createInitialTransactionPool method.
*/
Expand All @@ -45,4 +50,30 @@ public void testSubmitTransaction() {

assert transactionService.getTransactionPool()[0].getId() == 999;
}

// /**
// * Tests the Transaction Service to add Block to Blockchain when full.
// */
// @Test
// void testAddsBlockOnFullTransactionPool() {
//
// transactionService = new TransactionService();
// transactionService.createInitialTransactionPool();
// Transaction transaction = new Transaction(999, "right_now", "your mom", "CREATED");
//
// int transactionPoolLength = TransactionService.getTransactionPool().length;
//
// for (int i = 0; i < transactionPoolLength; i++) {
// transactionService.submitTransaction(transaction);
// }
//
// ResponseEntity<Block> response = restTemplate.getForEntity(
// API_VERSION + GET_BLOCK_BY_INDEX_ENDPOINT, Block.class);
//
// assert response.getStatusCode().is2xxSuccessful();
//
// Block block = response.getBody();
// System.out.println(block);
// assert block != null;
// }
}

0 comments on commit f4c0244

Please sign in to comment.