Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
refactored the verification check into a new ZinggOptions function #81
  • Loading branch information
navinrathore committed Dec 22, 2021
commit eb53368ee7d8ffafbd9ba742f232c6335248e820
8 changes: 1 addition & 7 deletions client/src/main/java/zingg/client/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import zingg.client.util.Email;
import zingg.client.util.EmailBody;
import zingg.client.util.Util;

/**
* This is the main point of interface with the Zingg matching product.
Expand All @@ -22,7 +21,6 @@ public class Client implements Serializable {
private ClientOptions options;

public static final Log LOG = LogFactory.getLog(Client.class);
public static final int EXIT_STATUS_ILLEGAL_CMD = 127;


/**
Expand Down Expand Up @@ -133,11 +131,7 @@ public static void main(String... args) {
System.exit(0);
}
String phase = options.get(ClientOptions.PHASE).value.trim();
if (ZinggOptions.getByValue(phase) == null) {
LOG.error("'" + phase + "' is not a valid phase");
LOG.error("Valid phases are: " + Util.join(ZinggOptions.getAllZinggOptions(), "|"));
System.exit(EXIT_STATUS_ILLEGAL_CMD);
}
ZinggOptions.verifyPhase(phase);
Arguments arguments = null;
if (options.get(ClientOptions.CONF).value.endsWith("json")) {
arguments = Arguments.createArgumentsFromJSON(options.get(ClientOptions.CONF).value, phase);
Expand Down
10 changes: 9 additions & 1 deletion client/src/main/java/zingg/client/ZinggOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.util.Arrays;

import zingg.client.util.Util;

public enum ZinggOptions {

TRAIN("train"),
Expand Down Expand Up @@ -38,5 +40,11 @@ public static final ZinggOptions getByValue(String value){
return null;
}


public static void verifyPhase(String phase) throws ZinggClientException {
if (getByValue(phase) == null) {
String message = "'" + phase + "' is not a valid phase. "
+ "Valid phases are: " + Util.join(getAllZinggOptions(), "|");
throw new ZinggClientException(message);
}
}
}
61 changes: 12 additions & 49 deletions client/src/test/java/zingg/client/TestClient.java
Original file line number Diff line number Diff line change
@@ -1,66 +1,29 @@
package zingg.client;

import static org.junit.Assert.assertTrue;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import static org.junit.Assert.fail;

import org.junit.Test;

public class TestClient {

@Test
public void testInvalidPhaseArg() {
String s = null;
int exitStatusExpected = 127; // error code for an illegal command e.g. typo
public void testValidPhase() {
String phase = "train";
try {
String zinggInvalidPhaseCmd = "scripts/zingg.sh --phase findTrainingDat --conf examples/febrl/config.json";
Process p = Runtime.getRuntime().exec(zinggInvalidPhaseCmd, null, new File(System.getenv("ZINGG_HOME")));
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
// read the output from the command
System.out.println("Here is the standard output of the command:\n");
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
// read any errors from the attempted command
System.out.println("Here is the standard error of the command (if any):\n");
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
p.waitFor();
assertTrue(exitStatusExpected == p.exitValue());
} catch (IOException | InterruptedException e) {
e.printStackTrace();
ZinggOptions.verifyPhase(phase);
} catch (ZinggClientException e1) {
fail("No exception was expected as it is a valid phase: " + phase);
}
}

@Test
public void testValidPhaseArg() {
String s = null;
int exitStatusExpected = 0; //default SUCCESS code
public void testInvalidPhase() {
String phase = "tain";
try {
String zinggHome = System.getenv("ZINGG_HOME");
String zinggCmd = "scripts/zingg.sh --phase findTrainingData --conf examples/febrl/config.json";
Process p = Runtime.getRuntime().exec(zinggCmd, null, new File(zinggHome));
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
// read the output from the command
System.out.println("Here is the standard output of the command:\n");
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
// read any errors from the attempted command
System.out.println("Here is the standard error of the command (if any):\n");
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
p.waitFor();
assertTrue(exitStatusExpected == p.exitValue());
} catch (IOException | InterruptedException e) {
e.printStackTrace();
ZinggOptions.verifyPhase(phase);
fail("An exception should have been thrown for an invalid phase");
} catch (ZinggClientException e1) {
System.out.println("Expected exception as it is an invalid phase: " + phase);
}
}
}