-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Transaction): implement API for getting all Transactions
- Loading branch information
1 parent
aac6424
commit 8703925
Showing
12 changed files
with
262 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
src/main/java/uk/ac/ic/doc/blocc/dashboard/transaction/TransactionRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package uk.ac.ic.doc.blocc.dashboard.transaction; | ||
|
||
import java.util.List; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Query; | ||
import org.springframework.stereotype.Repository; | ||
import uk.ac.ic.doc.blocc.dashboard.transaction.model.ApprovalTransaction; | ||
import uk.ac.ic.doc.blocc.dashboard.transaction.model.CompositeKey; | ||
import uk.ac.ic.doc.blocc.dashboard.transaction.model.Transaction; | ||
|
||
@Repository | ||
public interface TransactionRepository | ||
extends JpaRepository<ApprovalTransaction, CompositeKey> { | ||
|
||
@Query("SELECT tx FROM Transaction tx WHERE tx.key.containerNum = ?1") | ||
List<Transaction> findAllByContainerNum(int containerNum); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
.../uk/ac/ic/doc/blocc/dashboard/transaction/model/MinimalApprovalTransactionSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package uk.ac.ic.doc.blocc.dashboard.transaction.model; | ||
|
||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import com.fasterxml.jackson.databind.JsonSerializer; | ||
import com.fasterxml.jackson.databind.SerializerProvider; | ||
import java.io.IOException; | ||
|
||
public class MinimalApprovalTransactionSerializer extends JsonSerializer<ApprovalTransaction> { | ||
|
||
@Override | ||
public void serialize(ApprovalTransaction value, JsonGenerator gen, | ||
SerializerProvider serializers) throws IOException { | ||
gen.writeStartObject(); | ||
gen.writeStringField("txId", value.getTxId()); | ||
gen.writeStringField("creator", value.getCreator()); | ||
gen.writeNumberField("createdTimestamp", value.getCreatedTimestamp()); | ||
gen.writeEndObject(); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...ic/doc/blocc/dashboard/transaction/model/MinimalSensorChaincodeTransactionSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package uk.ac.ic.doc.blocc.dashboard.transaction.model; | ||
|
||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import com.fasterxml.jackson.databind.JsonSerializer; | ||
import com.fasterxml.jackson.databind.SerializerProvider; | ||
import java.io.IOException; | ||
|
||
public class MinimalSensorChaincodeTransactionSerializer extends | ||
JsonSerializer<SensorChaincodeTransaction> { | ||
|
||
@Override | ||
public void serialize(SensorChaincodeTransaction transaction, JsonGenerator gen, | ||
SerializerProvider serializers) throws IOException { | ||
gen.writeStartObject(); | ||
gen.writeStringField("txId", transaction.getTxId()); | ||
gen.writeStringField("creator", transaction.getCreator()); | ||
gen.writeNumberField("createdTimestamp", transaction.getCreatedTimestamp()); | ||
gen.writeObjectField("reading", transaction.getReading()); | ||
gen.writeEndObject(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
src/test/java/uk/ac/ic/doc/blocc/dashboard/transaction/TransactionRepositoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package uk.ac.ic.doc.blocc.dashboard.transaction; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.util.List; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; | ||
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; | ||
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager; | ||
import org.springframework.test.context.DynamicPropertyRegistry; | ||
import org.springframework.test.context.DynamicPropertySource; | ||
import org.testcontainers.containers.PostgreSQLContainer; | ||
import uk.ac.ic.doc.blocc.dashboard.fabric.model.TemperatureHumidityReading; | ||
import uk.ac.ic.doc.blocc.dashboard.transaction.model.ApprovalTransaction; | ||
import uk.ac.ic.doc.blocc.dashboard.transaction.model.SensorChaincodeTransaction; | ||
import uk.ac.ic.doc.blocc.dashboard.transaction.model.Transaction; | ||
|
||
@DataJpaTest | ||
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) | ||
class TransactionRepositoryTest { | ||
|
||
static final PostgreSQLContainer<?> postgres = | ||
new PostgreSQLContainer<>("postgres:latest"); | ||
|
||
static { | ||
postgres.start(); | ||
} | ||
|
||
@DynamicPropertySource | ||
static void properties(DynamicPropertyRegistry registry) { | ||
registry.add("spring.datasource.url", postgres::getJdbcUrl); | ||
registry.add("spring.datasource.username", postgres::getUsername); | ||
registry.add("spring.datasource.password", postgres::getPassword); | ||
} | ||
|
||
@Autowired | ||
private TestEntityManager entityManager; | ||
|
||
@Autowired | ||
private TransactionRepository repository; | ||
|
||
@Test | ||
void findAllByContainerNum() { | ||
// Given | ||
SensorChaincodeTransaction tx1 = new SensorChaincodeTransaction( | ||
"tx123", | ||
1, | ||
"Container5MSP", | ||
100L, | ||
new TemperatureHumidityReading(25, 0.3F, 100L)); | ||
|
||
SensorChaincodeTransaction tx2 = new SensorChaincodeTransaction( | ||
"tx456", | ||
3, | ||
"Container5MSP", | ||
103L, | ||
new TemperatureHumidityReading(30, 0.2F, 103L)); | ||
|
||
SensorChaincodeTransaction tx3 = new SensorChaincodeTransaction( | ||
"tx1299", | ||
1, | ||
"Container5MSP", | ||
100L, | ||
new TemperatureHumidityReading(22, 0.3F, 100L)); | ||
|
||
ApprovalTransaction approval1 = new ApprovalTransaction( | ||
"app1", 1, "Container6MSP", 101L, tx1); | ||
ApprovalTransaction approval2 = new ApprovalTransaction( | ||
"app2", 1, "Container7MSP", 102L, tx1); | ||
ApprovalTransaction approval3 = new ApprovalTransaction( | ||
"app3", 3, "Container9MSP", 102L, tx2); | ||
ApprovalTransaction approval4 = new ApprovalTransaction( | ||
"app4", 1, "Container7MSP", 102L, tx3); | ||
|
||
entityManager.persist(tx1); | ||
entityManager.persist(tx2); | ||
entityManager.persist(tx3); | ||
entityManager.persist(approval1); | ||
entityManager.persist(approval2); | ||
entityManager.persist(approval3); | ||
entityManager.persist(approval4); | ||
entityManager.flush(); | ||
|
||
// When | ||
List<Transaction> found = repository.findAllByContainerNum(1); | ||
|
||
// Then | ||
assertThat(found).hasSize(5).contains(tx1, tx3, approval1, approval2, approval4); | ||
assertThat(found).doesNotContain(tx2, approval3); | ||
} | ||
} |
Oops, something went wrong.