Skip to content

Commit

Permalink
Improve test coverage (#62)
Browse files Browse the repository at this point in the history
* Improve test coverage

* Optimize imports
  • Loading branch information
erikmafo authored Apr 5, 2021
1 parent 35512d2 commit c4108de
Show file tree
Hide file tree
Showing 33 changed files with 335 additions and 70 deletions.
3 changes: 2 additions & 1 deletion src/main/java/com/erikmafo/btviewer/config/AppConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.common.annotations.VisibleForTesting;
import com.google.inject.Inject;
import com.google.inject.name.Named;
import org.jetbrains.annotations.NotNull;

public class AppConfig {

Expand Down Expand Up @@ -32,7 +33,7 @@ public void setUseInMemoryDatabase(boolean useInMemoryDatabase) {
this.useInMemoryDatabase = useInMemoryDatabase;
}

private static String getConfigName(ApplicationEnvironment environment) {
private static String getConfigName(@NotNull ApplicationEnvironment environment) {
return environment.isProduction() ?
"config.properties" :
String.format("config.%s.properties", environment.getName());
Expand Down
20 changes: 11 additions & 9 deletions src/main/java/com/erikmafo/btviewer/model/Aggregation.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public Aggregation(AggregationExpression.Type type, String fieldName) {

public void updateFrom(@NotNull Aggregation other) {
if (other.type != type) {
throw new AssertionError(
throw new IllegalArgumentException(
String.format("Expected aggregation types to be equal but was %s and %s", type, other.type));
}
switch (type) {
Expand All @@ -45,18 +45,12 @@ public String getName() {
return String.format("%s(%s)", type.name(), fieldName);
}

public void updateSum(double addToSum) {
sum += addToSum;
}

public void incrementCount() {
count++;
}

public void setCount(int count) {
this.count = count;
}

public void setSum(double sum) { this.sum = sum; }

public Number getValue() {
switch (type) {
case COUNT: return count;
Expand All @@ -70,4 +64,12 @@ public Number getValue() {
public String toString() {
return getValue().toString();
}

private void updateSum(double addToSum) {
sum += addToSum;
}

private void incrementCount() {
count++;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,13 @@ public Object convert(BigtableCell cell) {
* @param cell a bigtable cell.
* @return true if the cell value converts to a number, false otherwise.
*/
public boolean isNumber(BigtableCell cell) {
public boolean isNumberCellDefinition(BigtableCell cell) {
var cellDefinition = getCellDefinition(cell);

switch (cellDefinition.getValueType().toLowerCase()) {
case "double":
case "integer":
case "float":
switch (cellDefinition.getValueType()) {
case ValueTypeConstants.DOUBLE:
case ValueTypeConstants.INTEGER:
case ValueTypeConstants.FLOAT:
return true;
default:
return false;
Expand All @@ -86,16 +86,16 @@ private CellDefinition getCellDefinition(@NotNull BigtableCell cell) {
.filter(c -> c.getFamily().equals(cell.getFamily())
&& c.getQualifier().equals(cell.getQualifier()))
.findFirst()
.orElse(new CellDefinition("string", cell.getFamily(), cell.getQualifier()));
.orElse(new CellDefinition(ValueTypeConstants.STRING, cell.getFamily(), cell.getQualifier()));
}

private Object convertUsingValueType(BigtableCell cell, @NotNull String valueType) {
switch (valueType.toLowerCase()) {
case "double":
switch (valueType.toUpperCase()) {
case ValueTypeConstants.DOUBLE:
return ByteBuffer.wrap(cell.getBytes()).getDouble();
case "integer":
case ValueTypeConstants.INTEGER:
return ByteBuffer.wrap(cell.getBytes()).getInt();
case "float":
case ValueTypeConstants.FLOAT:
return ByteBuffer.wrap(cell.getBytes()).getFloat();
default:
return cell.getValueAsString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,23 @@ public ByteString toByteString(Field field, Value value) {
.filter(c -> c.getQualifier().equals(field.getQualifier()))
.map(CellDefinition::getValueType)
.findFirst()
.orElse("STRING");
.orElse(ValueTypeConstants.STRING);

ByteString byteString;
switch (valueType.toUpperCase()) {
case "STRING":
case ValueTypeConstants.STRING:
byteString = ByteStringConverterUtil.toByteString(value.asString());
break;
case "DOUBLE":
case ValueTypeConstants.DOUBLE:
byteString = ByteStringConverterUtil.toByteString(value.asDouble());
break;
case "FLOAT":
case ValueTypeConstants.FLOAT:
byteString = ByteStringConverterUtil.toByteString(value.asFloat());
break;
case "INTEGER":
case ValueTypeConstants.INTEGER:
byteString = ByteStringConverterUtil.toByteString(value.asInt());
break;
default: throw new AssertionError("Value type " + valueType.toUpperCase() + " is not supported");
default: throw new IllegalArgumentException(String.format("Value type %s is not supported", valueType.toUpperCase()));
}

return byteString;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.erikmafo.btviewer.model;

public class ValueTypeConstants {
public static final String STRING = "STRING";
public static final String DOUBLE = "DOUBLE";
public static final String FLOAT = "FLOAT";
public static final String INTEGER = "INTEGER";
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@

import com.erikmafo.btviewer.config.AppConfig;
import com.erikmafo.btviewer.config.ApplicationEnvironment;
import com.erikmafo.btviewer.services.internal.*;
import com.erikmafo.btviewer.services.internal.AppDataStorage;
import com.erikmafo.btviewer.services.internal.AppDataStorageImpl;
import com.erikmafo.btviewer.services.internal.BigtableEmulatorSettingsProvider;
import com.erikmafo.btviewer.services.internal.BigtableSettingsProvider;
import com.erikmafo.btviewer.services.internal.BigtableSettingsProviderImpl;
import com.erikmafo.btviewer.services.internal.DynamicCredentialsProvider;
import com.erikmafo.btviewer.services.internal.TestDataUtil;
import com.google.api.gax.core.CredentialsProvider;
import com.google.inject.AbstractModule;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
import com.google.gson.Gson;
import com.google.inject.Singleton;
import org.jetbrains.annotations.NotNull;
import org.mapdb.*;
import org.mapdb.DB;
import org.mapdb.DBMaker;
import org.mapdb.DataInput2;
import org.mapdb.DataOutput2;
import org.mapdb.Serializer;

import java.io.IOException;
import java.nio.file.Files;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package com.erikmafo.btviewer.services.query;

import com.erikmafo.btviewer.model.*;
import com.erikmafo.btviewer.model.BigtableInstance;
import com.erikmafo.btviewer.model.BigtableTable;
import com.erikmafo.btviewer.model.BigtableValueConverter;
import com.erikmafo.btviewer.model.ByteStringConverterImpl;
import com.erikmafo.btviewer.model.QueryResultRow;
import com.erikmafo.btviewer.services.internal.AppDataStorage;
import com.erikmafo.btviewer.services.internal.BigtableSettingsProvider;
import com.erikmafo.btviewer.sql.QueryConverter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,17 +121,24 @@ private Aggregation createAggregationEntry(AggregationExpression aggregationExpr
var entry = createAggregationEntry(aggregationExpression);
switch (aggregationExpression.getType()) {
case AVG:
entry.setSum(getDouble(BigtableCell.from(cell)));
entry.setCount(1);
break;
case SUM:
entry.updateSum(getDouble(BigtableCell.from(cell)));
entry.setSum(getDouble(BigtableCell.from(cell)));
break;
case COUNT:
entry.setCount(1);
break;
default:
throw new IllegalArgumentException(
String.format("aggregation type: %s is not supported", aggregationExpression.getType()));
}
return entry;
}

private double getDouble(BigtableCell cell) {
if (valueConverter.isNumber(cell)) {
if (valueConverter.isNumberCellDefinition(cell)) {
return ((Number)valueConverter.convert(cell)).doubleValue();
} else {
return 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
import com.erikmafo.btviewer.util.FXMLLoaderUtil;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonBar;
import javafx.scene.control.Dialog;
import javafx.scene.control.DialogPane;
import javafx.scene.control.TextField;
import javafx.stage.FileChooser;
import org.jetbrains.annotations.NotNull;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.fxmisc.richtext.LineNumberFactory;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.reactfx.Subscription;

import javax.inject.Inject;
import java.time.Duration;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,36 @@
package com.erikmafo.btviewer.ui.components;

import com.erikmafo.btviewer.model.*;
import com.erikmafo.btviewer.model.BigtableCell;
import com.erikmafo.btviewer.model.BigtableColumn;
import com.erikmafo.btviewer.model.BigtableTable;
import com.erikmafo.btviewer.model.BigtableTableSettings;
import com.erikmafo.btviewer.model.BigtableValueConverter;
import com.erikmafo.btviewer.model.QueryResultRow;
import com.erikmafo.btviewer.services.table.LoadTableSettingsService;
import com.erikmafo.btviewer.services.table.SaveTableSettingsService;
import com.erikmafo.btviewer.util.AlertUtil;
import com.sun.javafx.PlatformUtil;
import javafx.beans.binding.Bindings;
import javafx.beans.property.*;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.ReadOnlyObjectWrapper;
import javafx.beans.property.ReadOnlyStringWrapper;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.control.SelectionMode;
import javafx.scene.control.TablePositionBase;
import javafx.scene.control.TreeTableCell;
import javafx.scene.control.TreeTableColumn;
import javafx.scene.control.TreeTablePosition;
import javafx.scene.control.TreeTableView;
import javafx.scene.input.Clipboard;
import javafx.scene.input.ClipboardContent;
import javafx.scene.input.KeyCode;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
package com.erikmafo.btviewer.ui.components;

import com.erikmafo.btviewer.model.BigtableColumn;
import com.erikmafo.btviewer.model.BigtableTable;
import com.erikmafo.btviewer.model.BigtableTableSettings;
import com.erikmafo.btviewer.model.CellDefinition;
import com.erikmafo.btviewer.util.FXMLLoaderUtil;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.control.ButtonBar;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Dialog;
import javafx.scene.control.DialogPane;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;

import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@
import javafx.beans.property.SimpleObjectProperty;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.control.Button;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.control.TreeCell;
import javafx.scene.control.TreeView;
import org.jetbrains.annotations.NotNull;

import javax.inject.Inject;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/erikmafo/btviewer/util/Check.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static <T> void notNull(T param, String paramName) {
* @throws IllegalArgumentException if param is null or empty.
*/
public static <T> void notNullOrEmpty(String param, String paramName) {
if (param == null || param.isEmpty()) {
if (StringUtil.isNullOrEmpty(param)) {
throw new IllegalArgumentException(String.format("%s cannot be null or empty", paramName));
}
}
Expand Down
7 changes: 5 additions & 2 deletions src/main/resources/fxml/add_instance_dialog.fxml
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<?import javafx.scene.control.ButtonType?>
<?import javafx.scene.control.DialogPane?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.GridPane?>
<fx:root type="DialogPane"
xmlns:fx="http://javafx.com/fxml"
headerText="Add Bigtable Instance"
Expand Down
1 change: 0 additions & 1 deletion src/main/resources/fxml/bigtable_menu_bar.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>

<MenuBar xmlns:fx="http://javafx.com/fxml"
fx:id="menuBar"
fx:controller="com.erikmafo.btviewer.ui.controllers.MenuBarController">
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/fxml/cell_view.fxml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.*?>

<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.BorderPane?>
<fx:root type="BorderPane"
xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
Expand Down
9 changes: 6 additions & 3 deletions src/main/resources/fxml/credentials_path_dialog.fxml
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>


<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ButtonType?>
<?import javafx.scene.control.DialogPane?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.GridPane?>
<fx:root type="DialogPane"
xmlns:fx="http://javafx.com/fxml"
headerText="Configure Credentials"
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/fxml/main.fxml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.BorderPane?>

<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.VBox?>
<BorderPane xmlns:fx="http://javafx.com/fxml"
fx:controller="com.erikmafo.btviewer.ui.controllers.MainController"
Expand Down
5 changes: 2 additions & 3 deletions src/main/resources/fxml/manage_credentials_dialog_pane.fxml
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.collections.FXCollections?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ButtonType?>
<?import javafx.scene.control.cell.PropertyValueFactory?>
<?import javafx.scene.control.DialogPane?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.VBox?>

<?import javafx.collections.FXCollections?>
<?import javafx.scene.control.cell.PropertyValueFactory?>
<fx:root prefHeight="400.0"
prefWidth="600.0"
type="DialogPane"
Expand Down
Loading

0 comments on commit c4108de

Please sign in to comment.