Skip to content

Commit

Permalink
feat(Transaction): add API for getting all transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
TonyWu3027 committed Aug 22, 2023
1 parent 0cda417 commit 649e843
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ public List<ApprovedTempReading> getApprovedTransactions(@RequestParam int conta
}

@GetMapping("/all")
public List<Transaction> getTransactions(@RequestParam int containerNum) {
public List<Transaction> getTransactions(@RequestParam(required = false) Integer containerNum) {
if (containerNum == null) {
return transactionService.getTransactions();
}
return transactionService.getTransactions(containerNum);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,8 @@ public void addApprovalTransaction(String txId, int containerNum, String approvi
public List<Transaction> getTransactions(int containerNum) {
return transactionRepository.findAllByContainerNum(containerNum);
}

public List<Transaction> getTransactions() {
return transactionRepository.findAll();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,43 @@ public void getAllExistingTransactionsForSpecifiedContainer() throws Exception {
.andExpect(status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("$.size()").value(5));
}

@Test
public void getAllExistingTransactions() throws Exception {
// Given
SensorChaincodeTransaction tx1 = new SensorChaincodeTransaction(
"tx123",
1,
"Container5MSP",
100L,
new TemperatureHumidityReading(25, 0.3F, 100L));

SensorChaincodeTransaction tx2 = new SensorChaincodeTransaction(
"tx456",
2,
"Container5MSP",
103L,
new TemperatureHumidityReading(30, 0.2F, 103L));

SensorChaincodeTransaction tx3 = new SensorChaincodeTransaction(
"tx1299",
3,
"Container5MSP",
100L,
new TemperatureHumidityReading(22, 0.3F, 100L));

ApprovalTransaction approval1 = new ApprovalTransaction(
"app1", 1, "Container6MSP", 101L, tx1);
ApprovalTransaction approval2 = new ApprovalTransaction(
"app2", 2, "Container7MSP", 102L, tx1);

List<Transaction> transactions = List.of(tx1, tx2, tx3, approval1, approval2);

when(transactionService.getTransactions()).thenReturn(transactions);

mockMvc.perform(MockMvcRequestBuilders.get("/api/v1/transaction/all")
.header("Origin", "https://example.com:8080"))
.andExpect(status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("$.size()").value(5));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ public void throwsExceptionWhenSameApprovalTransactionApprovesSensorTransactionA
}

@Test
public void getsTransactions() {
public void getsTransactionsByContainerNum() {
int containerNum = 1;

SensorChaincodeTransaction tx1 = new SensorChaincodeTransaction(
Expand Down Expand Up @@ -228,4 +228,48 @@ public void getsTransactions() {
assertTrue(result.contains(approval2));
}

@Test
public void getsTransactionsReturnsALlExistingTransactions() {

SensorChaincodeTransaction tx1 = new SensorChaincodeTransaction(
"tx123",
1, "creator", 100L,
new TemperatureHumidityReading(25, 0.3F, 100L));
SensorChaincodeTransaction tx2 = new SensorChaincodeTransaction(
"tx1299",
2, "creator", 100L,
new TemperatureHumidityReading(22, 0.3F, 100L));
SensorChaincodeTransaction tx3 = new SensorChaincodeTransaction(
"tx123",
3, "creator", 100L,
new TemperatureHumidityReading(25, 0.3F, 100L));
ApprovalTransaction approval1 = new ApprovalTransaction(
"tx222",
1, "approvingMSP", 101L, tx1);
ApprovalTransaction approval2 = new ApprovalTransaction(
"tx223",
2, "approvingMSP", 101L, tx2);

List<Transaction> mockData = Arrays.asList(
tx1,
tx2,
tx3,
approval1,
approval2
);

when(transactionRepository.findAll()).thenReturn(mockData);

List<Transaction> result = transactionService.getTransactions();

assertEquals(5, result.size());

// Assertions
assertTrue(result.contains(tx1));
assertTrue(result.contains(tx2));
assertTrue(result.contains(tx3));
assertTrue(result.contains(approval1));
assertTrue(result.contains(approval2));
}

}

0 comments on commit 649e843

Please sign in to comment.