Added tests to conbfiguration creator

android-scanner
VladimirEliTokarev 2016-10-04 19:52:08 +03:00
parent 55279e2e3d
commit f535847ee5
2 changed files with 90 additions and 6 deletions

View File

@ -22,19 +22,23 @@ import java.util.Scanner;
* Created by Vladimir Eliezer Tokarev on 10/4/2016.
* This class creates QuestionBallotUIElement from given configuration file
*/
class VotingBoothConfigurationCreator {
public class VotingBoothConfigurationCreator {
private int CURRENT_NUMBER_OF_QUESTIONS_REPRESENTED_TO_VOTER = 2 ;
public static String WRONG_QUESTIONS_AMOUNT_ERROR_MESSAGE = "The configuration file had too many ballot question objects.";
public static String UNSUPPORTED_VALUE_TYPE_ERROR_MESSAGE = "The given question type doesnt supported in current version";
private String IMAGES_TYPE = "png";
/**
* Creates VotingBoothConfiguarion object form file that given as paramter
* @param configPath the path to the json configuration
* @return configuraiton object
*/
VotingBoothConfiguration CreateConfiguration(String configPath) throws IOException, JSONException {
public VotingBoothConfiguration CreateConfiguration(String configPath) throws IOException, JSONException {
VotingBoothConfiguration config = new VotingBoothConfiguration();
JSONArray array = this.getJsonFromPath(configPath);
if (array.length() > CURRENT_NUMBER_OF_QUESTIONS_REPRESENTED_TO_VOTER) {
throw new IOException("The configration has to many questions datas");
if (array.length() != CURRENT_NUMBER_OF_QUESTIONS_REPRESENTED_TO_VOTER) {
throw new IOException(WRONG_QUESTIONS_AMOUNT_ERROR_MESSAGE);
} else {
try {
BallotQuestionUIElementOuterClass.BallotQuestionUIElement ballotQuestionUIElement = this.convertJSONObjToBallotQuestion(array.getJSONObject(0));
@ -124,7 +128,7 @@ class VotingBoothConfigurationCreator {
return ByteString.copyFrom(getFileBytes(questionValue));
}
else {
throw new IOException("The given questio type doesnt supported in current version");
throw new IOException(UNSUPPORTED_VALUE_TYPE_ERROR_MESSAGE);
}
}
@ -139,7 +143,7 @@ class VotingBoothConfigurationCreator {
BufferedImage originalImage = ImageIO.read(new File(filepath));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(originalImage, "png", baos);
ImageIO.write(originalImage, IMAGES_TYPE, baos);
baos.flush();
imageInByte = baos.toByteArray();
baos.close();

View File

@ -0,0 +1,80 @@
package meerkat.voting;
import meerkat.voting.gui.VotingBoothConfigurationCreator;
import org.factcenter.qilin.util.Pair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Before;
import org.junit.Test;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Scanner;
/**
* Created by Vladimir Eliezer Tokarev on 10/4/2016.
* this class tests that the VotingBoothVonfigurationCreator does really creates the right configurations
*/
public class VotingBoothConfigurationCreatorTest {
private VotingBoothConfigurationCreator configCreator;
private String currentConfigurationPath = "/F:/vova/meerkat/meerkat-java/voting-booth-gui/src/main/resources/configuration/VotingBoothConfiguration.json";
@Before
public void setup() {
this.configCreator = new VotingBoothConfigurationCreator();
}
@Test
public void testConfigurationCreation() throws IOException, JSONException {
List<Pair<JSONObject, Exception>> configs = new ArrayList<>();
String content = new Scanner(new File(this.currentConfigurationPath)).useDelimiter("\\Z").next();
JSONObject jsonObject = new JSONObject(content);
JSONArray currentBallotQuestions = jsonObject.getJSONArray("questions");
// simple test case try create current configurations suppose to run without exceptions
JSONObject testCase1 = new JSONObject();
testCase1.put("questions", new JSONArray(currentBallotQuestions.toString()));
configs.add(new Pair<>(testCase1, new Exception("what")));
// The creation should fail when the amount of questions is different from what have been expected
JSONObject testCase2 = new JSONObject();
JSONArray array = new JSONArray();
array.put(new JSONArray(currentBallotQuestions.toString()).getJSONObject(0));
testCase2.put("questions", array);
configs.add(new Pair<>(testCase2,new IOException(VotingBoothConfigurationCreator.WRONG_QUESTIONS_AMOUNT_ERROR_MESSAGE)));
// We support only TEXT and IMAGE types for files
JSONObject testCase3 = new JSONObject();
array = new JSONArray(currentBallotQuestions.toString());
JSONObject wrongType = array.getJSONObject(0);
JSONObject uiAnswers = wrongType.getJSONObject("answers");
uiAnswers.put("type", 1337);
wrongType.put("answers", uiAnswers);
array.put(0, wrongType);
testCase3.put("questions", array);
configs.add(new Pair<>(testCase3, new IOException(VotingBoothConfigurationCreator.UNSUPPORTED_VALUE_TYPE_ERROR_MESSAGE)));
// Check that the exceptions that have been trowhed are what we have expected
for (int i = 0 ; i < configs.size() ; i++) {
Pair<JSONObject, Exception> pair = configs.get(i);
FileWriter fileWriter = new FileWriter("/F:/testing.json");
fileWriter.write(pair.a.toString());
fileWriter.close();
try {
this.configCreator.CreateConfiguration("/F:/testing.json");
}catch (Exception e) {
assert Objects.equals(e.getMessage(), pair.b.getMessage());
}
(new File("/F:/testing.json")).delete();
}
}
}