From a30e33466af0473e98d7432023f6d0c9392a2621 Mon Sep 17 00:00:00 2001 From: jorge Date: Sun, 7 Apr 2024 21:48:07 +0200 Subject: [PATCH] Fixed the flaky tests --- questionGenerator/src/main/java/MainMock.java | 52 ------- .../questionGenerator/QuestionGenerator.java | 22 +-- .../QuestionGeneratorMock.java | 127 ------------------ .../generator/AbstractGenerator.java | 14 +- .../generator/AnswersAreEntities.java | 20 +-- 5 files changed, 30 insertions(+), 205 deletions(-) delete mode 100644 questionGenerator/src/main/java/MainMock.java delete mode 100644 questionGenerator/src/main/java/questionGenerator/QuestionGeneratorMock.java diff --git a/questionGenerator/src/main/java/MainMock.java b/questionGenerator/src/main/java/MainMock.java deleted file mode 100644 index 208458da..00000000 --- a/questionGenerator/src/main/java/MainMock.java +++ /dev/null @@ -1,52 +0,0 @@ -package main.java; - -import java.util.ArrayList; -import java.util.List; - -import main.java.questionGenerator.QuestionGeneratorMock; -import main.java.questionGenerator.question.Question; -import main.java.questionGenerator.question.QuestionType; -import main.java.questionGenerator.repository.QuestionRepository; - -public class MainMock { - - private static QuestionType[] types = {QuestionType.POPULATION, QuestionType.CAPITAL, QuestionType.SIZE, - QuestionType.LANGUAGE}; - - public static void main(String[] args) { - System.out.println("Ahora en Espa�ol"); - QuestionGeneratorMock qg = new QuestionGeneratorMock("es"); - for(QuestionType t : types) { - run(qg, t); - System.out.println(); - } - System.out.println("Now English"); - qg = new QuestionGeneratorMock("en"); - for(QuestionType t : types) { - run(qg, t); - System.out.println(); - } - System.out.println("Now in english but with bad language code"); - qg = new QuestionGeneratorMock("ep"); - for(QuestionType t : types) { - run(qg, t); - System.out.println(); - } - - } - - private static void run(QuestionGeneratorMock qg, QuestionType type) { - List questionJSONList = new ArrayList<>(); - - //Populate JSON list here - for(int i=0; i<3; i++) { - Question question = qg.generateQuestion(type); - question.setNumber(i); - questionJSONList.add(question.getJSON().toString()); - System.out.println(question.getJSON().toString()); - } - - QuestionRepository.getInstance().insert(questionJSONList); - } - -} diff --git a/questionGenerator/src/main/java/questionGenerator/QuestionGenerator.java b/questionGenerator/src/main/java/questionGenerator/QuestionGenerator.java index 92879cbe..2c5f81b2 100644 --- a/questionGenerator/src/main/java/questionGenerator/QuestionGenerator.java +++ b/questionGenerator/src/main/java/questionGenerator/QuestionGenerator.java @@ -15,8 +15,8 @@ import main.java.questionGenerator.question.QuestionType; public class QuestionGenerator { - - private AbstractGenerator generator; + + private AbstractGenerator generator; private String languageCode; public QuestionGenerator(String languageCode){ @@ -33,19 +33,21 @@ public List generateQuestions(QuestionType type, int amount){ } catch (IOException e) { e.printStackTrace(); } - Random rnd = new Random(); - List chosen = new ArrayList<>(); + Random rnd = new Random(); int size = entites.size(); - int number = 0; - while(number0) { int index = rnd.nextInt(size); String entity = entites.get(index); - if(!chosen.contains(entity)) { - chosen.add(entity); - Question q = generator.generate(entity); + entites.remove(index); + Question q = null; + try { + q = generator.generate(entity); questions.add(q); - number++; + } catch (Exception e) { + //If there's any problem generating the question we jump to the next one + System.err.println(e.getMessage()); } + size = entites.size(); } return questions; } diff --git a/questionGenerator/src/main/java/questionGenerator/QuestionGeneratorMock.java b/questionGenerator/src/main/java/questionGenerator/QuestionGeneratorMock.java deleted file mode 100644 index d40b6fc6..00000000 --- a/questionGenerator/src/main/java/questionGenerator/QuestionGeneratorMock.java +++ /dev/null @@ -1,127 +0,0 @@ -package main.java.questionGenerator; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; -import java.util.Random; - -import main.java.questionGenerator.entityGenerator.EntityGenerator; -import main.java.questionGenerator.generator.AbstractGenerator; -import main.java.questionGenerator.generator.specificGenerators.CapitalGenerator; -import main.java.questionGenerator.generator.specificGenerators.LanguageGenerator; -import main.java.questionGenerator.generator.specificGenerators.PopulationGenerator; -import main.java.questionGenerator.generator.specificGenerators.SizeGenerator; -import main.java.questionGenerator.question.Question; -import main.java.questionGenerator.question.QuestionType; - -public class QuestionGeneratorMock { - - private AbstractGenerator generator; - private String id; - private String languageCode; - - private static String[] POP_ENTITIES = {"Q14317", "Q12273", "Q14649"}; - private static String[] CAP_ENTITIES = {"Q3934", "Q29", "Q43"}; - private static String[] SIZE_ENTITIES = {"Q29", "Q12273", "Q3934"}; - private static String[] LANG_ENTITIES = {"Q29", "Q43", "Q3934"}; - - public QuestionGeneratorMock(String languageCode){ - this.languageCode = languageCode; - } - - public Question generateQuestion(QuestionType type) { - generatorFactory(type); - generator.setLocalization(languageCode); - return generator.generate(id); - } - - public List generateQuestions(QuestionType type, int amount){ - setGenerator(type); - generator.setLocalization(languageCode); - List questions = new ArrayList<>(); - List entites = new ArrayList<>(); - try { - entites = EntityGenerator.getEntities(type, 100); - } catch (IOException e) { - e.printStackTrace(); - } - Random rnd = new Random(); - List chosen = new ArrayList<>(); - int size = entites.size(); - int number = 0; - while(number names) { return mtv.getText(); } +// protected abstract String getQuestion(String name); + protected abstract String getRightAnswer(Map> claims) throws Exception; + protected abstract List getWrongAnswers(String rightAnswer) throws Exception; protected String getQuestion(String name) { String q = getMessages().getString(message); return String.format(q, name); } - protected abstract String getRightAnswer(Map> claims); - protected abstract List getWrongAnswers(String rightAnswer); - - public String getPropertyId() { return propertyId; } - + public static ItemDocumentImpl getAlreadyProcessedEntity(String id) { return alreadyProcessedEntities.get(id); } diff --git a/questionGenerator/src/main/java/questionGenerator/generator/AnswersAreEntities.java b/questionGenerator/src/main/java/questionGenerator/generator/AnswersAreEntities.java index 171b22cc..f7bd6713 100644 --- a/questionGenerator/src/main/java/questionGenerator/generator/AnswersAreEntities.java +++ b/questionGenerator/src/main/java/questionGenerator/generator/AnswersAreEntities.java @@ -16,9 +16,9 @@ import main.java.questionGenerator.question.QuestionType; public abstract class AnswersAreEntities extends AbstractGenerator { - + private final String PROPERTY_TO_CHECK; - private final QuestionType type; + private QuestionType type; public AnswersAreEntities(String propertyId, QuestionType type, String propertyToCheck, String message) { super(propertyId, type, message); @@ -27,7 +27,11 @@ public AnswersAreEntities(String propertyId, QuestionType type, String propertyT } @Override - protected String getRightAnswer(Map> claims) { + protected String getRightAnswer(Map> claims) throws Exception { + if(claims.get(super.getPropertyId())==null) { + throw new Exception("Claims does not have the property " + super.getPropertyId()); + } + for(Statement st : claims.get(super.getPropertyId())) { boolean valid = true; for(SnakGroup sg : st.getQualifiers()) { @@ -48,7 +52,7 @@ protected String getRightAnswer(Map> claims) { return null; } - protected String processRightAnswer(Statement st) { + private String processRightAnswer(Statement st) { String entity = getIdFromLink(st.getValue().toString()); String answer = ""; try { @@ -66,7 +70,7 @@ protected String processRightAnswer(Statement st) { return answer; } - protected String getAnswer(String id){ + private String getAnswer(String id) throws Exception{ ItemDocumentImpl idi = getAlreadyProcessedEntity(id); if(idi==null) { @@ -93,7 +97,7 @@ protected String getIdFromLink(String url) { } @Override - protected List getWrongAnswers(String rightAnswer) { + protected List getWrongAnswers(String rightAnswer) throws Exception { List entites = new ArrayList<>(); try { entites = EntityGenerator.getEntities(type, 100); @@ -102,15 +106,13 @@ protected List getWrongAnswers(String rightAnswer) { } Random rnd = new Random(); List result = new ArrayList<>(); - List used = new ArrayList<>(); for(int i = 0; i < 3; i++){ int rndnum = rnd.nextInt(entites.size()); String wrong = getAnswer(entites.get(rndnum)); - if(wrong == null || wrong.equals(rightAnswer) || used.contains(rndnum)) + if(wrong == null || wrong.equals(rightAnswer) || result.contains(wrong)) i--; else{ result.add(wrong); - used.add(rndnum); } } return result;