Skip to content

Commit

Permalink
StationDatabaseManager tests and debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
xspanger3770 committed Aug 22, 2023
1 parent c5b1290 commit 3f4b146
Show file tree
Hide file tree
Showing 5 changed files with 221 additions and 11 deletions.
17 changes: 15 additions & 2 deletions src/main/java/globalquake/database/Network.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class Network implements Serializable {

private final String networkCode;
private final String description;
private final Collection<Station> stations;
private final List<Station> stations;

public Network(String networkCode, String description) {
this.networkCode = networkCode;
Expand All @@ -27,7 +27,20 @@ public String getDescription() {
return description;
}

public Collection<Station> getStations() {
public List<Station> getStations() {
return stations;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Network network = (Network) o;
return Objects.equals(networkCode, network.networkCode) && Objects.equals(description, network.description) && Objects.equals(stations, network.stations);
}

@Override
public int hashCode() {
return Objects.hash(networkCode, description, stations);
}
}
13 changes: 9 additions & 4 deletions src/main/java/globalquake/database/Station.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

public class Station implements Serializable {
Expand All @@ -20,7 +21,7 @@ public class Station implements Serializable {

private final String stationCode;
private final String stationSite;
private final Collection<Channel> channels;
private final List<Channel> channels;
private final Network network;
private Channel selectedChannel = null;

Expand All @@ -38,7 +39,7 @@ public Network getNetwork() {
return network;
}

public Collection<Channel> getChannels() {
public List<Channel> getChannels() {
return channels;
}

Expand Down Expand Up @@ -80,12 +81,12 @@ public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Station station = (Station) o;
return Double.compare(lat, station.lat) == 0 && Double.compare(lon, station.lon) == 0 && Double.compare(alt, station.alt) == 0 && Objects.equals(stationCode, station.stationCode) && Objects.equals(stationSite, station.stationSite);
return getChannels().equals(station.getChannels()) && Double.compare(lat, station.lat) == 0 && Double.compare(lon, station.lon) == 0 && Double.compare(alt, station.alt) == 0 && Objects.equals(stationCode, station.stationCode) && Objects.equals(stationSite, station.stationSite);
}

@Override
public int hashCode() {
return Objects.hash(lat, lon, alt, stationCode, stationSite);
return Objects.hash(lat, lon, alt, stationCode, stationSite, channels);
}

public boolean hasAvailableChannel() {
Expand All @@ -104,6 +105,10 @@ public void selectBestChannel() {
}

public void selectBestAvailableChannel() {
if (!channels.contains(selectedChannel)) {
selectedChannel = null;
}

if(selectedChannel != null){
return;
}
Expand Down
32 changes: 27 additions & 5 deletions src/main/java/globalquake/database/StationDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -168,15 +168,15 @@ public static Channel getChannel(List<Network> networks, String networkCode, Str
return null;
}

Station station = getStation(network, stationCode);
Station station = findStation(network, stationCode);
if(station == null){
return null;
}

return getChannel(station, channelName, locationCode);
}

private static Station getStation(Network network, String stationCode) {
private static Station findStation(Network network, String stationCode) {
for(Station station: network.getStations()){
if(station.getStationCode().equals(stationCode)){
return station;
Expand All @@ -187,7 +187,7 @@ private static Station getStation(Network network, String stationCode) {


public static Station getOrCreateStation(Network network, String stationCode, String stationSite, double lat, double lon, double alt) {
Station station = getStation(network, stationCode);
Station station = findStation(network, stationCode);
if(station != null){
return station;
}
Expand All @@ -199,6 +199,17 @@ public static Station getOrCreateStation(Network network, String stationCode, St
return station;
}

public static Station getOrInsertStation(Network network, Station stationNew) {
Station station = findStation(network, stationNew.getStationCode());
if(station != null){
return station;
}

network.getStations().add(stationNew);

return stationNew;
}

public static Network getNetwork(List<Network> networks, String networkCode) {
for(Network network: networks){
if(network.getNetworkCode().equals(networkCode)){
Expand All @@ -221,11 +232,22 @@ public static Network getOrCreateNetwork(List<Network> networks, String networkC
return resultNetwork;
}

public static Network getOrInsertNetwork(List<Network> networks, Network network) {
Network resultNetwork = getNetwork(networks, network.getNetworkCode());
if(resultNetwork != null) {
return resultNetwork;
}

networks.add(network);

return network;
}


@SuppressWarnings("UnusedReturnValue")
public Channel acceptChannel(Network network, Station station, Channel channel) {
Network networkFound = getOrCreateNetwork(networks, network.getNetworkCode(), network.getDescription());
Station stationFound = getOrCreateStation(networkFound, station.getStationCode(), station.getStationSite(), station.getLatitude(), station.getLongitude(), station.getAlt());
Network networkFound = getOrInsertNetwork(networks, network);
Station stationFound = getOrInsertStation(networkFound, station);
Channel channelFound = getChannel(stationFound, channel.getCode(), channel.getLocationCode());
if(channelFound != null){
stationFound.getChannels().remove(channelFound);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ public class StationDatabaseManager {
private final List<Runnable> statusListeners = new CopyOnWriteArrayList<>();
private boolean updating = false;

public StationDatabaseManager(){};

public StationDatabaseManager(StationDatabase stationDatabase){
this.stationDatabase = stationDatabase;
}

public void load() throws FatalIOException {
File file = getDatabaseFile();
if (!file.getParentFile().exists()) {
Expand Down
164 changes: 164 additions & 0 deletions src/test/java/globalquake/database/StationDatabaseManagerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
package globalquake.database;

import org.junit.Test;

import java.util.List;
import java.util.Optional;

import static org.junit.Assert.*;

public class StationDatabaseManagerTest {

@Test
public void testConstructor(){
StationDatabase stationDatabase = new StationDatabase();
StationDatabaseManager databaseManager = new StationDatabaseManager(stationDatabase);

assertEquals(stationDatabase, databaseManager.getStationDatabase());
}

@Test
public void testAcceptChannel(){
Network dummyNetwork = new Network("coolNetwork", "");
Station dummyStation = new Station(dummyNetwork, "coolStation", "", 0, 0, 0);
Channel dummyChannel = new Channel("coolChannel", "00", 50, 0, 0, 0, null);

StationDatabase stationDatabase = new StationDatabase();
stationDatabase.acceptChannel(dummyNetwork, dummyStation, dummyChannel);

assertEquals(1, stationDatabase.getNetworks().size());
assertEquals(1, stationDatabase.getNetworks().get(0).getStations().size());
var opt = stationDatabase.getNetworks().get(0).getStations().stream().findAny();
assertTrue(opt.isPresent());
assertEquals(1, stationDatabase.getNetworks().get(0).getStations().stream().findAny().get().getChannels().size());
}

@Test
public void testAcceptAnotherChannel(){
Network dummyNetwork = new Network("coolNetwork", "");
Station dummyStation = new Station(dummyNetwork, "coolStation", "", 0, 0, 0);
Channel dummyChannel = new Channel("coolChannel", "00", 50, 0, 0, 0, null);

StationDatabase stationDatabase = new StationDatabase();
stationDatabase.acceptChannel(dummyNetwork, dummyStation, dummyChannel);

assertEquals(1, stationDatabase.getNetworks().size());
assertEquals(1, stationDatabase.getNetworks().get(0).getStations().size());
var opt = stationDatabase.getNetworks().get(0).getStations().stream().findAny();
assertTrue(opt.isPresent());
assertEquals(1, stationDatabase.getNetworks().get(0).getStations().stream().findAny().get().getChannels().size());

Channel dummyChannel2 = new Channel("coolChannel", "00", 50, 50, 0, 0, null);
stationDatabase.acceptChannel(dummyNetwork, dummyStation, dummyChannel2);

assertEquals(1, stationDatabase.getNetworks().size());
assertEquals(1, stationDatabase.getNetworks().get(0).getStations().size());
opt = stationDatabase.getNetworks().get(0).getStations().stream().findAny();
assertTrue(opt.isPresent());
assertEquals(1, stationDatabase.getNetworks().get(0).getStations().stream().findAny().get().getChannels().size());

Channel channel = stationDatabase.getNetworks().get(0).getStations().stream().findAny().get().getChannels().stream().findAny().get();
assertEquals(50, channel.getLatitude(), 1e-6);
}

@Test
public void testRemoveStationSource(){
StationSource stationSource1 = new StationSource("1", "");
StationSource stationSource2 = new StationSource("2", "");
Network dummyNetwork = new Network("coolNetwork", "");
Station dummyStation = new Station(dummyNetwork, "coolStation", "", 0, 0, 0);
Channel dummyChannel = new Channel("coolChannel", "00", 50, 0, 0, 0, stationSource1);
Channel dummyChannelDup = new Channel("coolChannel", "00", 50, 0, 0, 0, stationSource2);
Channel dummyChannel2 = new Channel("coolChannel2", "00", 50, 0, 0, 0, stationSource2);

StationDatabase stationDatabase = new StationDatabase();
StationDatabaseManager stationDatabaseManager = new StationDatabaseManager(stationDatabase);

stationDatabase.getStationSources().add(stationSource1);
stationDatabase.acceptChannel(dummyNetwork, dummyStation, dummyChannel);
stationDatabase.acceptChannel(dummyNetwork, dummyStation, dummyChannelDup);

assertEquals(1, stationDatabase.getNetworks().size());
assertEquals(1, stationDatabase.getNetworks().get(0).getStations().size());
Optional<Station> opt = stationDatabase.getNetworks().get(0).getStations().stream().findAny();
assertTrue(opt.isPresent());
assertEquals(1, stationDatabase.getNetworks().get(0).getStations().get(0).getChannels().size());

stationDatabase.acceptChannel(dummyNetwork, dummyStation, dummyChannel2);
assertEquals(2, stationDatabase.getNetworks().get(0).getStations().get(0).getChannels().size());

assertEquals(2, stationDatabase.getNetworks().get(0).getStations().get(0).getChannels().get(0).getStationSources().size());
assertEquals(1, stationDatabase.getNetworks().get(0).getStations().get(0).getChannels().get(1).getStationSources().size());

// REMOVE STATION SOURCE 1
stationDatabaseManager.removeAllStationSources(List.of(stationSource1));

assertEquals(1, stationDatabase.getNetworks().size());
assertEquals(1, stationDatabase.getNetworks().get(0).getStations().size());
opt = stationDatabase.getNetworks().get(0).getStations().stream().findAny();
assertTrue(opt.isPresent());

assertEquals(2, stationDatabase.getNetworks().get(0).getStations().get(0).getChannels().size());
assertEquals(1, stationDatabase.getNetworks().get(0).getStations().get(0).getChannels().get(0).getStationSources().size());
assertEquals(1, stationDatabase.getNetworks().get(0).getStations().get(0).getChannels().get(1).getStationSources().size());

stationDatabaseManager.removeAllStationSources(List.of(stationSource2));

assertEquals(0, stationDatabase.getNetworks().size());
}

@Test
public void testSelectedChannel(){
SeedlinkNetwork dummySeedlinkNetwork = new SeedlinkNetwork("dummy", "", 5);
StationSource stationSource1 = new StationSource("1", "");
StationSource stationSource2 = new StationSource("2", "");
Network dummyNetwork = new Network("coolNetwork", "");
Station dummyStation = new Station(dummyNetwork, "coolStation", "", 0, 0, 0);
Channel dummyChannel = new Channel("coolChannel", "00", 50, 0, 0, 0, stationSource1);
Channel dummyChannelDup = new Channel("coolChannel", "00", 50, 0, 0, 0, stationSource2);
Channel dummyChannel2 = new Channel("coolChannel2", "00", 50, 0, 0, 0, stationSource2);
dummyChannel2.getSeedlinkNetworks().add(dummySeedlinkNetwork);
dummyChannel.getSeedlinkNetworks().add(dummySeedlinkNetwork);

StationDatabase stationDatabase = new StationDatabase();
StationDatabaseManager stationDatabaseManager = new StationDatabaseManager(stationDatabase);

stationDatabase.getStationSources().add(stationSource1);
stationDatabase.acceptChannel(dummyNetwork, dummyStation, dummyChannel);
stationDatabase.acceptChannel(dummyNetwork, dummyStation, dummyChannelDup);

assertEquals(1, stationDatabase.getNetworks().size());
assertEquals(1, stationDatabase.getNetworks().get(0).getStations().size());
Optional<Station> opt = stationDatabase.getNetworks().get(0).getStations().stream().findAny();
assertTrue(opt.isPresent());
assertEquals(1, stationDatabase.getNetworks().get(0).getStations().get(0).getChannels().size());

stationDatabase.acceptChannel(dummyNetwork, dummyStation, dummyChannel2);
assertEquals(1, stationDatabase.getNetworks().size());
assertEquals(1, stationDatabase.getNetworks().get(0).getStations().size());

dummyStation.setSelectedChannel(dummyChannel2);
assertEquals(dummyChannel2, dummyStation.getSelectedChannel());
assertEquals(dummyStation, dummyStation);
assertEquals(dummyStation, stationDatabase.getNetworks().get(0).getStations().get(0));
assertEquals(dummyChannel2, stationDatabase.getNetworks().get(0).getStations().get(0).getSelectedChannel());

assertEquals(2, stationDatabase.getNetworks().get(0).getStations().get(0).getChannels().size());

assertEquals(2, stationDatabase.getNetworks().get(0).getStations().get(0).getChannels().get(0).getStationSources().size());
assertEquals(1, stationDatabase.getNetworks().get(0).getStations().get(0).getChannels().get(1).getStationSources().size());

// REMOVE STATION SOURCE 2
stationDatabaseManager.removeAllStationSources(List.of(stationSource2));

assertEquals(1, stationDatabase.getNetworks().size());
assertEquals(1, stationDatabase.getNetworks().get(0).getStations().size());
opt = stationDatabase.getNetworks().get(0).getStations().stream().findAny();
assertTrue(opt.isPresent());

assertEquals(1, stationDatabase.getNetworks().get(0).getStations().get(0).getChannels().size());
assertEquals(dummyChannel, stationDatabase.getNetworks().get(0).getStations().get(0).getSelectedChannel());
}


}

0 comments on commit 3f4b146

Please sign in to comment.