Skip to content

Commit

Permalink
Merge branch 'master' into e2e_laura
Browse files Browse the repository at this point in the history
  • Loading branch information
lauratbg committed Apr 26, 2024
2 parents 4e47c6a + b1289a4 commit c8bf83a
Show file tree
Hide file tree
Showing 62 changed files with 2,134 additions and 348 deletions.
9 changes: 5 additions & 4 deletions gatewayservice/gateway-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,15 @@ app.post('/adduser', async (req, res) => {
try {
// Forward the add user request to the user service
const userResponse = await axios.post(userServiceUrl+'/adduser', req.body);
console.log(userResponse)
res.json(userResponse.data);
} catch (error) {
manageError(res, error);

}
});

app.get('/questions', verifyToken, async (req, res) => {
app.get('/questions', async (req, res) => {
try {

// Forward the question request to the quetion service
Expand Down Expand Up @@ -101,7 +102,7 @@ app.get('/questions/:lang/:amount', verifyToken, async (req, res) => {
}
});

app.get('/questions/:lang', verifyToken, async (req, res) => {
app.get('/questions/:lang', async (req, res) => {
try {
if(!validateLang(req.params.lang.toString()))
res.status(400).json({ error: 'Wrong values given' });
Expand Down Expand Up @@ -220,7 +221,7 @@ function validateAmount(amount) {
}

function validateType(type){
return ['POPULATION', 'CAPITAL', 'LANGUAGE', 'SIZE'].includes(type);
return ['POPULATION', 'CAPITAL', 'LANGUAGE', 'SIZE', 'HEAD_OF_GOVERMENT'].includes(type);
}

function validateUser(user){
Expand All @@ -231,7 +232,7 @@ function manageError(res, error){
if(error.response) //Some microservice responded with an error
res.status(error.response.status).json({ error: error.response.data.error });
else //Some other error
res.status(500).json({error : "Interanl server error"})
res.status(500).json({error : "Internal server error"})
}

module.exports = server
36 changes: 34 additions & 2 deletions gatewayservice/gateway-service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jest.mock('axios');



describe('Gateway Service with token mock', () => {
describe('Gateway Service with mocked micro services', () => {

// Mock responses from external services
axios.post.mockImplementation((url, data) => {
Expand Down Expand Up @@ -156,4 +156,36 @@ function checkRecord(response){
function checkQuestion(response){
expect(response.statusCode).toBe(200);
expect(response.body[0]).toHaveProperty('question', "¿Cuál es la población de Oviedo?");
}
}

describe('Gateway Service without mocked micro services', () => {

it('should not forward login request and give 500', async () => {
try{
await request(app)
.post('/login')
.send({ username: 'testuser', password: 'testpassword' });

} catch(error){
expect(error.response.status).toBe(500);
expect(error.response.data.error).toBe('Internal server error');
}
});

it('should not forward login request and give 500', async () => {
axios.post.mockImplementation((url, data) => {
if (url.endsWith('/login')) {
throw new Error("Important information");
}
});
try{
await request(app)
.post('/login')
.send({ username: 'testuser', password: 'testpassword' });

} catch(error){
expect(error.response.status).toBe(500);
expect(error.response.data.error).toBe('Internal server error');
}
});
});
78 changes: 48 additions & 30 deletions questionGenerator/src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package main.java;

import java.util.ArrayList;
import java.util.List;

import main.java.questionGenerator.QuestionGenerator;
Expand All @@ -8,37 +9,54 @@
import main.java.questionGenerator.repository.QuestionRepository;

public class Main {

private static QuestionGenerator qg = QuestionGenerator.getInstance();

private static final long TIME_SKIP = 18000000; //5 hours
// private static final long TIME_SKIP = 43200000; //12 hours
//private static final long TIME_SKIP = 1000; //1 minute


private static String[] languages = {"en", "es", "tr"};

private static QuestionType[] types = {QuestionType.CAPITAL, QuestionType.LANGUAGE, QuestionType.POPULATION,
QuestionType.SIZE, QuestionType.HEAD_OF_GOVERMENT};
//, QuestionType.VIDEOGAME_DEVELOPER, QuestionType.VIDEOGAME_PUBLISHER, QuestionType.VIDEOGAME_GENRE, QuestionType.VIDEOGAME_COUNTRY};

public static void main(String[] args) {
QuestionGenerator qgEn = new QuestionGenerator("en");

run(qgEn, QuestionType.CAPITAL, 3);
System.out.println();

run(qgEn, QuestionType.LANGUAGE, 3);
System.out.println();

run(qgEn, QuestionType.POPULATION, 3);
System.out.println();

run(qgEn, QuestionType.SIZE, 3);

QuestionGenerator qgEs = new QuestionGenerator("es");

run(qgEs, QuestionType.CAPITAL, 3);
System.out.println();

run(qgEs, QuestionType.LANGUAGE, 3);
System.out.println();

run(qgEs, QuestionType.POPULATION, 3);
System.out.println();

run(qgEs, QuestionType.SIZE, 3);
//private static final int NUMBER_OF_QUESTIONS = 50;
private static final int NUMBER_OF_QUESTIONS = 100;
//private static final int NUMBER_OF_QUESTIONS = 3;
//private static final int NUMBER_OF_QUESTIONS = 1;

public static void main(String[] args) {
while(true) {
List<String> questions = generate().stream().map(q -> q.getJSON().toString()).toList();
QuestionRepository.getInstance().populate(questions);
try {
Thread.sleep(TIME_SKIP);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

private static List<Question> generate() {
List<Question> questions = new ArrayList<Question>();
for(String lang : languages) {
qg.setLanguageCode(lang);
for(QuestionType type: types)
questions.addAll(run(qg, type, NUMBER_OF_QUESTIONS));
}
return questions;
}

private static void run(QuestionGenerator qg, QuestionType type, int numberOfQuestions){
List<Question> questions = qg.generateQuestions(type, numberOfQuestions);
QuestionRepository.getInstance().insert(questions.stream().map(q -> q.getJSON().toString()).toList());
private static List<Question> run(QuestionGenerator qg, QuestionType type, int numberOfQuestions) {
List<Question> questions = qg.generateQuestions(type, numberOfQuestions);
for(int i=0; i<questions.size(); i++) {
Question question = questions.get(i);
question.setNumber(i);
}
return questions;
}
}

}
38 changes: 38 additions & 0 deletions questionGenerator/src/main/java/StaticMain.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package main.java;

import java.util.List;

import main.java.questionGenerator.QuestionGenerator;
import main.java.questionGenerator.question.Question;
import main.java.questionGenerator.question.QuestionType;
import main.java.questionGenerator.repository.QuestionRepository;

public class StaticMain {

public static void main(String[] args) {
QuestionGenerator qg = QuestionGenerator.getInstance();

run(qg, QuestionType.CAPITAL, 3);
run(qg, QuestionType.LANGUAGE, 3);
run(qg, QuestionType.POPULATION, 3);
run(qg, QuestionType.SIZE, 3);
run(qg, QuestionType.HEAD_OF_GOVERMENT, 3);
run(qg, QuestionType.DIRECTOR, 3);
run(qg, QuestionType.VIDEOGAME_DEVELOPER, 3);
run(qg, QuestionType.VIDEOGAME_PUBLISHER, 3);
run(qg, QuestionType.VIDEOGAME_GENRE, 3);
run(qg, QuestionType.VIDEOGAME_COUNTRY, 3);

}

private static void run(QuestionGenerator qg, QuestionType type, int numberOfQuestions){
List<Question> questions = qg.generateQuestions(type, numberOfQuestions);
for(int i=0; i<questions.size(); i++) {
Question question = questions.get(i);
question.setNumber(i);
System.out.println(question.getJSON().toString());
}
System.out.println();
QuestionRepository.getInstance().insert(questions.stream().map(q -> q.getJSON().toString()).toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,46 @@
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.generator.Generator;
import main.java.questionGenerator.generator.answersAreEntites.withSubProperties.CapitalGenerator;
import main.java.questionGenerator.generator.answersAreEntites.withSubProperties.HeadOfGovernmentGenerator;
import main.java.questionGenerator.generator.answersAreEntites.withSubProperties.LanguageGenerator;
import main.java.questionGenerator.generator.answersAreEntites.withSubProperties.videogames.VideogameCountryGenerator;
import main.java.questionGenerator.generator.answersAreEntites.withSubProperties.videogames.VideogameDeveloperGenerator;
import main.java.questionGenerator.generator.answersAreEntites.withSubProperties.videogames.VideogameGenreGenerator;
import main.java.questionGenerator.generator.answersAreEntites.withSubProperties.videogames.VideogamePublisherGenerator;
import main.java.questionGenerator.generator.answersAreEntites.withoutSubProperties.DirectorGenerator;
import main.java.questionGenerator.generator.answersAreNotEntites.PopulationGenerator;
import main.java.questionGenerator.generator.answersAreNotEntites.SizeGenerator;
import main.java.questionGenerator.question.Question;
import main.java.questionGenerator.question.QuestionType;

public class QuestionGenerator {

private AbstractGenerator generator;
private Generator generator;
private String languageCode;

public QuestionGenerator(String languageCode){
this.languageCode = languageCode;
private static QuestionGenerator questionGenerator;

public static QuestionGenerator getInstance() {
if(questionGenerator==null)
questionGenerator = new QuestionGenerator();
return questionGenerator;
}

private QuestionGenerator() {

}

public List<Question> generateQuestions(QuestionType type, int amount){
setGenerator(type);
generator.setLocalization(languageCode);
long sampleSize = getSampleSize(amount);
generator.setSampleSize(sampleSize);
List<Question> questions = new ArrayList<>();
List<String> entites = new ArrayList<>();
try {
entites = EntityGenerator.getEntities(type, 100);
entites = EntityGenerator.getEntities(type, sampleSize, generator.getPropertyId());
} catch (IOException e) {
e.printStackTrace();
}
Expand All @@ -43,6 +59,13 @@ public List<Question> generateQuestions(QuestionType type, int amount){
try {
q = generator.generate(entity);
questions.add(q);
} catch(RuntimeException e) {
/*
* Sometimes not all the parameters for generating can be passed in the query, so this
* acts as failsave to avoid generating undesired questions, while not overloading the
* screen with error messages if not wanted
*/
//System.err.println(e.getMessage());
} catch (Exception e) {
//If there's any problem generating the question we jump to the next one
System.err.println(e.getMessage());
Expand All @@ -55,23 +78,61 @@ public List<Question> generateQuestions(QuestionType type, int amount){
private void setGenerator(QuestionType type) {
switch (type) {
case POPULATION: {
generator = new PopulationGenerator();
generator = PopulationGenerator.getInstance();
break;
}
case CAPITAL: {
generator = new CapitalGenerator();
generator = CapitalGenerator.getInstance();
break;

}
case SIZE: {
generator = new SizeGenerator();
generator = SizeGenerator.getInstance();
break;
}
case LANGUAGE: {
generator = new LanguageGenerator();
generator = LanguageGenerator.getInstance();
break;
}
case HEAD_OF_GOVERMENT: {
generator = HeadOfGovernmentGenerator.getInstance();
break;
}
case DIRECTOR: {
generator = DirectorGenerator.getInstance();
break;
}
case VIDEOGAME_DEVELOPER: {
generator = VideogameDeveloperGenerator.getInstance();
break;
}
case VIDEOGAME_PUBLISHER: {
generator = VideogamePublisherGenerator.getInstance();
break;
}
case VIDEOGAME_GENRE: {
generator = VideogameGenreGenerator.getInstance();
break;
}
case VIDEOGAME_COUNTRY: {
generator = VideogameCountryGenerator.getInstance();
break;
}
}
}

public String getLanguageCode() {
return languageCode;
}

public void setLanguageCode(String languageCode) {
this.languageCode = languageCode;
}

private long getSampleSize(int amount) {
long value = amount * 10;
if(value<1000)
return 1000;
return value;
}

}
Loading

0 comments on commit c8bf83a

Please sign in to comment.