Skip to content

Commit

Permalink
Merge pull request #105 from 12urenloop/fix-tests
Browse files Browse the repository at this point in the history
fix(tests): lazy load baton statuses
  • Loading branch information
NuttyShrimp authored Mar 25, 2024
2 parents ac75872 + 57e998a commit c6894ee
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
4 changes: 4 additions & 0 deletions src/main/java/telraam/database/daos/BatonDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ public interface BatonDAO extends DAO<Baton> {
@RegisterBeanMapper(Baton.class)
Optional<Baton> getById(@Bind("id") int id);

@SqlQuery("SELECT * FROM baton WHERE mac = :mac")
@RegisterBeanMapper(Baton.class)
Optional<Baton> getByMac(@Bind("mac") String mac);

@Override
@SqlUpdate("DELETE FROM baton WHERE id = :id")
@RegisterBeanMapper(Baton.class)
Expand Down
21 changes: 14 additions & 7 deletions src/main/java/telraam/monitoring/BatonStatusHolder.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;

public class BatonStatusHolder {
// Map from batonMac to batonStatus
Expand All @@ -23,13 +24,18 @@ public class BatonStatusHolder {
public BatonStatusHolder(BatonDAO BDAO, DetectionDAO DDAO) {
batonDAO = BDAO;
detectionDAO = DDAO;
this.initStatus();
}

private void initStatus() {
var batons = batonDAO.getAll();
for (Baton baton : batons) {
BatonStatus batonStatus = new BatonStatus(
private BatonStatus getStatusForBaton(String batonMac) {
BatonStatus batonStatus = batonStatusMap.get(batonMac);
if (batonStatus == null) {
Optional<Baton> optionalBaton = batonDAO.getByMac(batonMac);
if (optionalBaton.isEmpty()) {
return null;
}
Baton baton = optionalBaton.get();

batonStatus = new BatonStatus(
baton.getMac().toLowerCase(),
baton.getId(),
baton.getName(),
Expand All @@ -41,6 +47,7 @@ private void initStatus() {
);
batonStatusMap.put(baton.getMac().toLowerCase(), batonStatus);
}
return batonStatus;

}

Expand Down Expand Up @@ -83,13 +90,13 @@ public BatonStatus GetBatonStatus(Integer batonId) {
baton.ifPresent(value -> batonIdToMac.put(batonId, value.getMac().toLowerCase()));
}
String batonMac = batonIdToMac.get(batonId);
return batonStatusMap.get(batonMac);
return getStatusForBaton(batonMac);
}

public BatonStatus createBatonStatus(Integer batonId) {
String batonMac = batonIdToMac.get(batonId);
if (batonMac != null) {
return batonStatusMap.get(batonMac);
return getStatusForBaton(batonMac);
}
var baton = batonDAO.getById(batonId);
if (baton.isEmpty()) {
Expand Down

0 comments on commit c6894ee

Please sign in to comment.