Compare commits
2
Commits
basic
...
intermediate
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
121dff910d | ||
|
|
ddc421b4b3 |
+2
-5
@@ -22,11 +22,8 @@ CMD ["tail","-f","/dev/null"]
|
||||
#---Building
|
||||
FROM base AS build
|
||||
COPY . .
|
||||
RUN mvn clean package -DskipTests
|
||||
|
||||
#--Testing
|
||||
FROM build AS testing
|
||||
RUN mvn clean test
|
||||
RUN mvn clean package
|
||||
# Add -DskipTests for slightly faster build, but no testing
|
||||
|
||||
#---Production
|
||||
# Pulled again to get rid of maven, to decrease the size
|
||||
|
||||
+9
-4
@@ -32,10 +32,10 @@
|
||||
<caffeine.version>3.1.8</caffeine.version>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<!-- <dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
</dependency> -->
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
@@ -47,11 +47,11 @@
|
||||
<scope>runtime</scope>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<!-- <dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<scope>runtime</scope>
|
||||
</dependency> -->
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
@@ -72,6 +72,11 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-validation</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-text</artifactId>
|
||||
<version>1.10.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.ninjaws.quiz.components;
|
||||
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
|
||||
/**
|
||||
* Sets up the database, and adds the categories
|
||||
*
|
||||
* Fills up the primary database
|
||||
*
|
||||
* Then, at night, creates a new database, adds categories, and then starts filling it up
|
||||
* When full, swaps the databases, and wipes the old one
|
||||
*/
|
||||
public class StartupRunner implements CommandLineRunner {
|
||||
private final JdbcTemplate primaryJdbcTemplate;
|
||||
|
||||
public StartupRunner(JdbcTemplate primaryJdbcTemplate) {
|
||||
this.primaryJdbcTemplate = primaryJdbcTemplate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(String... args) {
|
||||
// Initialization logic here
|
||||
primaryJdbcTemplate.execute("CREATE TABLE IF NOT EXISTS test_table (id INT PRIMARY KEY, name VARCHAR(255))");
|
||||
primaryJdbcTemplate.update("INSERT INTO test_table (id, name) VALUES (?, ?)", 1, "Sample Data");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.ninjaws.quiz.config;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
|
||||
public class DataSourceConfig {
|
||||
@Primary
|
||||
@Bean(name = "primaryDataSourceProperties")
|
||||
@ConfigurationProperties("spring.datasource.primary")
|
||||
public DataSourceProperties primaryDataSourceProperties() {
|
||||
return new DataSourceProperties();
|
||||
}
|
||||
|
||||
@Primary
|
||||
@Bean(name = "primaryDataSource")
|
||||
public DataSource primaryDataSource() {
|
||||
return primaryDataSourceProperties().initializeDataSourceBuilder().build();
|
||||
}
|
||||
|
||||
@Bean(name = "secondaryDataSourceProperties")
|
||||
@ConfigurationProperties("spring.datasource.secondary")
|
||||
public DataSourceProperties secondaryDataSourceProperties() {
|
||||
return new DataSourceProperties();
|
||||
}
|
||||
|
||||
@Bean(name = "secondaryDataSource")
|
||||
public DataSource secondaryDataSource() {
|
||||
return secondaryDataSourceProperties().initializeDataSourceBuilder().build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public JdbcTemplate primaryJdbcTemplate(@Qualifier("primaryDataSource") DataSource ds) {
|
||||
return new JdbcTemplate(ds);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public JdbcTemplate secondaryJdbcTemplate(@Qualifier("secondaryDataSource") DataSource ds) {
|
||||
return new JdbcTemplate(ds);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.ninjaws.quiz.entities;
|
||||
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.Table;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Entity
|
||||
@Table(name = "category")
|
||||
@Getter
|
||||
@Setter
|
||||
public class Category {
|
||||
@Id
|
||||
private Integer id;
|
||||
private String name;
|
||||
|
||||
public Category() {}
|
||||
|
||||
public Category(int id, String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.ninjaws.quiz.entities;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.JoinColumn;
|
||||
import jakarta.persistence.ManyToOne;
|
||||
import jakarta.persistence.Table;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Entity
|
||||
@Table(name = "question")
|
||||
@Getter
|
||||
@Setter
|
||||
public class QuestionEntity {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "category", nullable = false)
|
||||
private Category category;
|
||||
|
||||
private String difficulty;
|
||||
private String type;
|
||||
private String question;
|
||||
|
||||
//* Hidden from the user */
|
||||
private String correctAnswer;
|
||||
private List<String> incorrectAnswers;
|
||||
}
|
||||
@@ -17,5 +17,5 @@ public class ApiResponse {
|
||||
@JsonProperty("response_code")
|
||||
private int responseCode;
|
||||
@JsonProperty("results")
|
||||
private List<Question> results;
|
||||
private List<QuestionDTO> results;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.ninjaws.quiz.models;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.ninjaws.quiz.entities.Category;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
public class CategoryResponse {
|
||||
List<Category> trivia_categories;
|
||||
}
|
||||
+7
-5
@@ -8,14 +8,16 @@ import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
public class Question {
|
||||
private String type;
|
||||
private String difficulty;
|
||||
private String category;
|
||||
private String question;
|
||||
@Setter
|
||||
public class QuestionDTO {
|
||||
|
||||
private String category;
|
||||
private String difficulty;
|
||||
private String type;
|
||||
private String question;
|
||||
|
||||
//* Hidden from the user */
|
||||
private String correctAnswer;
|
||||
@@ -48,21 +48,25 @@ public class QuizSettings {
|
||||
@Max(50)
|
||||
private int amount = 10;
|
||||
|
||||
private Type type = Type.ANY;
|
||||
// private Type type = Type.ANY;
|
||||
|
||||
private Difficulty difficulty = Difficulty.ANY;
|
||||
|
||||
private Category category = Category.ANY;
|
||||
// private Difficulty difficulty = Difficulty.ANY;
|
||||
|
||||
private Integer category;
|
||||
private String difficulty;
|
||||
private String type;
|
||||
|
||||
public QuizSettings(){}
|
||||
|
||||
/** Inverse of toApiQuery */
|
||||
public QuizSettings(int amount, Integer category, String difficulty, String type) {
|
||||
public QuizSettings(int amount, Integer category, String difficulty, String type) {
|
||||
this.amount = amount;
|
||||
this.category = category == null ? Category.ANY : Category.values()[category];
|
||||
this.difficulty = difficulty == null ? Difficulty.ANY : Difficulty.valueOf(difficulty.toUpperCase());
|
||||
this.type = type == null ? Type.ANY : type.equals("boolean") ? Type.BOOLEAN : Type.MULTIPLE;
|
||||
this.category = category;
|
||||
this.difficulty = difficulty;
|
||||
this.type = type;
|
||||
// this.category = category == null ? Category.ANY : Category.values()[category];
|
||||
// this.difficulty = difficulty == null ? Difficulty.ANY : Difficulty.valueOf(difficulty.toUpperCase());
|
||||
// this.type = type == null ? Type.ANY : type.equals("boolean") ? Type.BOOLEAN : Type.MULTIPLE;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -77,19 +81,37 @@ public class QuizSettings {
|
||||
* Converts the class to a format that can be provided as arguments for the api request
|
||||
* @return The resulting request arguments as String
|
||||
*/
|
||||
// public String toApiQuery() {
|
||||
// StringBuilder query = new StringBuilder("?amount=" + amount);
|
||||
|
||||
// if (category != null && category != Category.ANY) {
|
||||
// query.append("&category=").append(category.getApiValue());
|
||||
// }
|
||||
|
||||
// if (difficulty != null && difficulty != Difficulty.ANY) {
|
||||
// query.append("&difficulty=").append(difficulty.getApiValue());
|
||||
// }
|
||||
|
||||
// if (type != null && type != Type.ANY) {
|
||||
// query.append("&type=").append(type.getApiValue());
|
||||
// }
|
||||
|
||||
// return query.toString();
|
||||
// }
|
||||
|
||||
public String toApiQuery() {
|
||||
StringBuilder query = new StringBuilder("?amount=" + amount);
|
||||
|
||||
if (category != null && category != Category.ANY) {
|
||||
query.append("&category=").append(category.getApiValue());
|
||||
if (category != null) {
|
||||
query.append("&category=").append(category);
|
||||
}
|
||||
|
||||
if (difficulty != null && difficulty != Difficulty.ANY) {
|
||||
query.append("&difficulty=").append(difficulty.getApiValue());
|
||||
if (difficulty != null ) {
|
||||
query.append("&difficulty=").append(difficulty);
|
||||
}
|
||||
|
||||
if (type != null && type != Type.ANY) {
|
||||
query.append("&type=").append(type.getApiValue());
|
||||
if (type != null ) {
|
||||
query.append("&type=").append(type);
|
||||
}
|
||||
|
||||
return query.toString();
|
||||
|
||||
@@ -11,7 +11,7 @@ import lombok.Setter;
|
||||
public class Session {
|
||||
private final String id;
|
||||
private int statusCode = 0;
|
||||
private List<Question> questions;
|
||||
private List<QuestionDTO> questions;
|
||||
|
||||
public Session() {
|
||||
this.id = UUID.randomUUID().toString();
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.ninjaws.quiz.repositories;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
import com.ninjaws.quiz.entities.Category;
|
||||
|
||||
public interface CategoryRepository extends JpaRepository<Category, Long>{
|
||||
Optional<Category> findByName(String name);
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.ninjaws.quiz.repositories;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
|
||||
import com.ninjaws.quiz.entities.QuestionEntity;
|
||||
|
||||
public interface QuestionRepository extends JpaRepository<QuestionEntity, Long>{//, JpaSpecificationExecutor<Question> {
|
||||
|
||||
@Query(value = "SELECT * FROM question AS q " +
|
||||
"WHERE (?1 IS NULL OR q.category = ?1) " +
|
||||
"AND (?2 IS NULL OR q.difficulty = ?2) " +
|
||||
"AND (?3 IS NULL OR q.type = ?3) " +
|
||||
"ORDER BY RAND() LIMIT ?4", nativeQuery = true)
|
||||
List<QuestionEntity> lookupQuestions(Integer categoryId, String difficulty, String type, int limit);
|
||||
|
||||
// public List<Question> getQuestionsBy(String type, String category, String difficulty, int limit) {
|
||||
// Specification<Product> spec = Specification.where(QuestionSpecification.hasType(type))
|
||||
// .and(QuestionSpecification.hasCategory(category))
|
||||
// .and(QuestionSpecification.hasDifficulty(difficulty));
|
||||
|
||||
// return productRepository.findAll(spec);
|
||||
// }
|
||||
}
|
||||
@@ -9,6 +9,7 @@ import org.springframework.web.client.RestClientException;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import com.ninjaws.quiz.models.ApiResponse;
|
||||
import com.ninjaws.quiz.models.CategoryResponse;
|
||||
import com.ninjaws.quiz.models.QuizSettings;
|
||||
import com.ninjaws.quiz.models.Request;
|
||||
|
||||
@@ -26,13 +27,13 @@ public class ApiService {
|
||||
|
||||
private static final System.Logger logger = System.getLogger(ApiService.class.getName());
|
||||
|
||||
private final String apiUrl = "https://opentdb.com/api.php";
|
||||
private final String apiUrl = "https://opentdb.com";
|
||||
|
||||
public ApiService() {
|
||||
}
|
||||
|
||||
private String buildRequestString(QuizSettings quizSettings) {
|
||||
return apiUrl + quizSettings.toApiQuery();
|
||||
return apiUrl + "/api.php" + quizSettings.toApiQuery();
|
||||
}
|
||||
|
||||
public void handleRequest(@Valid Request request) {
|
||||
@@ -54,7 +55,9 @@ public class ApiService {
|
||||
* Store them in the DataService
|
||||
*/
|
||||
public void collectBulk() {
|
||||
String url = apiUrl + "?amount=50";
|
||||
// TODO: Make this happen only once, when a new database is generated
|
||||
getCategories();
|
||||
String url = apiUrl + "/api.php?amount=50";
|
||||
try {
|
||||
ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);
|
||||
String json = response.getBody();
|
||||
@@ -64,5 +67,21 @@ public class ApiService {
|
||||
} catch (RestClientException | IOException e) {
|
||||
logger.log(System.Logger.Level.ERROR, "Critical failure", e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public void getCategories() {
|
||||
String url = apiUrl + "/api_category.php";
|
||||
try {
|
||||
ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);
|
||||
String json = response.getBody();
|
||||
CategoryResponse categoryResponse = cleanerService.extractCategoryResponse(json);
|
||||
responseHandler.handleCategoryResponse(categoryResponse);
|
||||
|
||||
} catch (RestClientException | IOException e) {
|
||||
logger.log(System.Logger.Level.ERROR, "Critical failure", e);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,22 +1,87 @@
|
||||
package com.ninjaws.quiz.services;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.lang3.StringEscapeUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.ninjaws.quiz.entities.Category;
|
||||
import com.ninjaws.quiz.entities.QuestionEntity;
|
||||
import com.ninjaws.quiz.models.ApiResponse;
|
||||
import com.ninjaws.quiz.models.CategoryResponse;
|
||||
import com.ninjaws.quiz.models.QuestionDTO;
|
||||
import com.ninjaws.quiz.repositories.CategoryRepository;
|
||||
|
||||
@Service
|
||||
public class CleanerService {
|
||||
|
||||
@Autowired
|
||||
private ObjectMapper objectMapper;
|
||||
@Autowired
|
||||
private CategoryRepository categoryRepository;
|
||||
|
||||
public CleanerService() {}
|
||||
public CleanerService() {
|
||||
}
|
||||
|
||||
public ApiResponse extractApiResponse(String json) throws IOException {
|
||||
return objectMapper.readValue(json, ApiResponse.class);
|
||||
}
|
||||
|
||||
public CategoryResponse extractCategoryResponse(String json) throws IOException {
|
||||
return objectMapper.readValue(json, CategoryResponse.class);
|
||||
}
|
||||
|
||||
public List<QuestionDTO> questionListEntitytoDTO(List<QuestionEntity> entities) {
|
||||
List<QuestionDTO> dtos = new ArrayList<>();
|
||||
for (int i = 0; i < entities.size(); i++) {
|
||||
QuestionEntity entity = entities.get(i);
|
||||
dtos.add(questionEntityToDTO(entity));
|
||||
}
|
||||
return dtos;
|
||||
}
|
||||
|
||||
public QuestionDTO questionEntityToDTO(QuestionEntity entity) {
|
||||
QuestionDTO dto = new QuestionDTO();
|
||||
dto.setQuestion(entity.getQuestion());
|
||||
dto.setCorrectAnswer(entity.getCorrectAnswer());
|
||||
dto.setIncorrectAnswer(entity.getIncorrectAnswers());
|
||||
dto.setCategory(entity.getCategory().getName());
|
||||
dto.setType(entity.getType());
|
||||
dto.setDifficulty(entity.getDifficulty());
|
||||
return dto;
|
||||
}
|
||||
|
||||
public List<QuestionEntity> questionListDTOtoEntity(List<QuestionDTO> dtos) {
|
||||
List<QuestionEntity> entities = new ArrayList<>();
|
||||
for (int i = 0; i < dtos.size(); i++) {
|
||||
QuestionDTO dto = dtos.get(i);
|
||||
entities.add(questionDTOtoEntity(dto));
|
||||
}
|
||||
return entities;
|
||||
}
|
||||
|
||||
public QuestionEntity questionDTOtoEntity(QuestionDTO dto) {
|
||||
QuestionEntity entity = new QuestionEntity();
|
||||
Category category = categoryRepository.findByName(decodeString(dto.getCategory())).orElseThrow(() -> new IllegalStateException("Category not in DB!"));
|
||||
entity.setCategory(category);
|
||||
entity.setDifficulty(dto.getDifficulty());
|
||||
entity.setType(dto.getType());
|
||||
entity.setQuestion(dto.getQuestion());
|
||||
entity.setCorrectAnswer(dto.getCorrectAnswer());
|
||||
entity.setIncorrectAnswers(dto.getIncorrectAnswers());
|
||||
return entity;
|
||||
}
|
||||
|
||||
/**
|
||||
* For some reason, the categories come with special characters,
|
||||
* But the categories from the questions dont
|
||||
* So this has to be manually fixed if they are to be stored in the db (otherwise namematching doesnt work)
|
||||
*/
|
||||
private String decodeString(String text){
|
||||
return StringEscapeUtils.unescapeHtml4(text);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
package com.ninjaws.quiz.services;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.ninjaws.quiz.entities.Category;
|
||||
import com.ninjaws.quiz.entities.QuestionEntity;
|
||||
import com.ninjaws.quiz.models.QuizSettings;
|
||||
import com.ninjaws.quiz.repositories.CategoryRepository;
|
||||
import com.ninjaws.quiz.repositories.QuestionRepository;
|
||||
|
||||
@Service
|
||||
public class DataService {
|
||||
|
||||
@Autowired
|
||||
private QuestionRepository questionRepository;
|
||||
|
||||
@Autowired
|
||||
private CategoryRepository categoryRepository;
|
||||
|
||||
public List<QuestionEntity> getAllQuestions() {
|
||||
return questionRepository.findAll();
|
||||
}
|
||||
|
||||
public QuestionEntity saveQuestion(QuestionEntity question) {
|
||||
return questionRepository.save(question);
|
||||
}
|
||||
|
||||
public List<QuestionEntity> saveQuestions(List<QuestionEntity> questions) {
|
||||
return questionRepository.saveAll(questions);
|
||||
}
|
||||
|
||||
// public List<Question> getRandomQuestions(String type, String category, String difficulty, int limit) {
|
||||
// return questionRepository.findRandomQuestions(type, limit);
|
||||
// }
|
||||
|
||||
public List<QuestionEntity> lookupQuestions(QuizSettings quizSettings) {
|
||||
// Long category = Long.valueOf(quizSettings.getCategory().getApiValue() == 1 ? null : quizSettings.getCategory().getApiValue());
|
||||
// String difficulty = quizSettings.getDifficulty() == QuizSettings.Difficulty.ANY ? null : quizSettings.getDifficulty().getApiValue();
|
||||
// String type = quizSettings.getType() == QuizSettings.Type.ANY ? null : quizSettings.getType().getApiValue();
|
||||
// System.out.println(category);
|
||||
// System.out.println(difficulty);
|
||||
// System.out.println(type);
|
||||
|
||||
return questionRepository.lookupQuestions(quizSettings.getCategory(), quizSettings.getDifficulty(), quizSettings.getType(), quizSettings.getAmount());
|
||||
}
|
||||
|
||||
/**
|
||||
* Tries to get questions that match the specified QuizSettings
|
||||
* @return The questions if the query was possible, and an empty array if not
|
||||
*/
|
||||
// public List<Question> getByQuizSettings(QuizSettings quizSettings) {
|
||||
// Specification<Product> spec = Specification.where(QuestionSpecification.hasType(type))
|
||||
// .and(QuestionSpecification.hasCategory(category))
|
||||
// .and(QuestionSpecification.hasDifficulty(difficulty));
|
||||
|
||||
// return productRepository.findAll(spec);
|
||||
// }
|
||||
|
||||
public List<Category> saveCategories(List<Category> categories) {
|
||||
return categoryRepository.saveAll(categories);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.ninjaws.quiz.services;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class DatabaseScheduler {
|
||||
@Autowired
|
||||
private JdbcTemplate secondaryJdbcTemplate;
|
||||
|
||||
@Scheduled(fixedRate = 60000) // Runs every 60 seconds
|
||||
public void createAndFillSecondaryDatabase() {
|
||||
// Create tables and insert data into the secondary database
|
||||
secondaryJdbcTemplate.
|
||||
.execute("CREATE TABLE IF NOT EXISTS sample_table (id INT PRIMARY KEY, name VARCHAR(255))");
|
||||
secondaryJdbcTemplate.update("INSERT INTO sample_table (id, name) VALUES (?, ?)", 1, "Sample Data");
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,12 @@
|
||||
package com.ninjaws.quiz.services;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.ninjaws.quiz.entities.QuestionEntity;
|
||||
import com.ninjaws.quiz.models.Answers;
|
||||
import com.ninjaws.quiz.models.QuizSettings;
|
||||
import com.ninjaws.quiz.models.Request;
|
||||
@@ -18,6 +20,10 @@ public class QuizService {
|
||||
private CacheService cacheService;
|
||||
@Autowired
|
||||
private QueueService queueService;
|
||||
@Autowired
|
||||
private DataService dataService;
|
||||
@Autowired
|
||||
private CleanerService cleanerService;
|
||||
|
||||
/**
|
||||
* Actions:
|
||||
@@ -29,7 +35,22 @@ public class QuizService {
|
||||
*/
|
||||
public Optional<String> createSession(QuizSettings quizSettings) {
|
||||
Session session = new Session();
|
||||
|
||||
/** First, we check if there are already enough items in our db */
|
||||
List<QuestionEntity> questionsRetrieved = dataService.lookupQuestions(quizSettings);
|
||||
if(!questionsRetrieved.isEmpty()) {
|
||||
session.setQuestions(cleanerService.questionListEntitytoDTO(questionsRetrieved));
|
||||
}
|
||||
|
||||
cacheService.addToCache(session.getId(), session);
|
||||
|
||||
/**
|
||||
* If the questions were retrieved from the DB, then it doesn't need to be added to the queue
|
||||
*/
|
||||
if(!questionsRetrieved.isEmpty()) {
|
||||
return Optional.of(session.getId());
|
||||
}
|
||||
|
||||
boolean addedToQueue = queueService.addToQueue(new Request(session.getId(), quizSettings));
|
||||
/** In this order, because doing the queue before the cache could lead to race conditions (request completion immediately calls cache, but then the item doesn't exist) */
|
||||
if(!addedToQueue) {
|
||||
|
||||
@@ -4,6 +4,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.ninjaws.quiz.models.ApiResponse;
|
||||
import com.ninjaws.quiz.models.CategoryResponse;
|
||||
import com.ninjaws.quiz.models.Session;
|
||||
|
||||
@Service
|
||||
@@ -11,6 +12,10 @@ public class ResponseHandlingService {
|
||||
|
||||
@Autowired
|
||||
private CacheService cacheService;
|
||||
@Autowired
|
||||
private DataService dataService;
|
||||
@Autowired
|
||||
private CleanerService cleanerService;
|
||||
|
||||
/**
|
||||
* If there is no sessionId, then this is a request from the scheduler
|
||||
@@ -28,6 +33,7 @@ public class ResponseHandlingService {
|
||||
case 0:
|
||||
// Success
|
||||
updateSessionInCache(session, response);
|
||||
dataService.saveQuestions(cleanerService.questionListDTOtoEntity(response.getResults()));
|
||||
break;
|
||||
case 1:
|
||||
// Not enough questions to satisfy the request.
|
||||
@@ -61,7 +67,7 @@ public class ResponseHandlingService {
|
||||
public void handleResponse(ApiResponse response) {
|
||||
switch (response.getResponseCode()) {
|
||||
case 0:
|
||||
// TODO: Store in DB
|
||||
dataService.saveQuestions(cleanerService.questionListDTOtoEntity(response.getResults()));
|
||||
break;
|
||||
case 4:
|
||||
// Signal DataService that we have extracted all the data. Stop gathering, switch to caching-mode
|
||||
@@ -80,4 +86,7 @@ public class ResponseHandlingService {
|
||||
cacheService.addToCache(session.getId(), session);
|
||||
}
|
||||
|
||||
public void handleCategoryResponse(CategoryResponse categoryResponse) {
|
||||
dataService.saveCategories(categoryResponse.getTrivia_categories());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,6 +27,8 @@ public class SchedulerService {
|
||||
Request request = queueService.popFromQueue();
|
||||
if (request != null) {
|
||||
apiService.handleRequest(request);
|
||||
} else {
|
||||
apiService.collectBulk();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
package com.ninjaws.quiz.services;
|
||||
|
||||
public class StorageService {
|
||||
|
||||
}
|
||||
@@ -1,2 +1,34 @@
|
||||
spring.application.name=quiz
|
||||
server.port=9090
|
||||
server.port=9090
|
||||
|
||||
# H2 Database configuration
|
||||
spring.h2.console.enabled=true
|
||||
# spring.h2.console.path=/console
|
||||
|
||||
|
||||
# spring.datasource.url=jdbc:h2:mem:testdb
|
||||
# spring.datasource.driverClassName=org.h2.Driver
|
||||
# spring.datasource.username=sa
|
||||
# spring.datasource.password=
|
||||
|
||||
# spring.jpa.hibernate.ddl-auto=create-drop
|
||||
# spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
|
||||
|
||||
|
||||
# Primary database configuration
|
||||
spring.datasource.primary.url=jdbc:h2:mem:testdb
|
||||
# ;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
|
||||
spring.datasource.primary.driver-class-name=org.h2.Driver
|
||||
spring.datasource.primary.username=sa
|
||||
spring.datasource.primary.password=
|
||||
spring.jpa.primary.hibernate.ddl-auto=create-drop
|
||||
spring.jpa.primary.database-platform=org.hibernate.dialect.H2Dialect
|
||||
|
||||
# Secondary database configuration
|
||||
spring.datasource.secondary.url=jdbc:h2:mem:secondarydb
|
||||
# ;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
|
||||
spring.datasource.secondary.driver-class-name=org.h2.Driver
|
||||
spring.datasource.secondary.username=sa
|
||||
spring.datasource.secondary.password=
|
||||
spring.jpa.secondary.hibernate.ddl-auto=create-drop
|
||||
spring.jpa.secondary.database-platform=org.hibernate.dialect.H2Dialect
|
||||
@@ -0,0 +1,59 @@
|
||||
package com.ninjaws.quiz.services;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import com.ninjaws.quiz.models.Session;
|
||||
|
||||
public class CacheServiceTest {
|
||||
|
||||
private CacheService cacheService;
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
cacheService = new CacheService();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddToCache() {
|
||||
String key = "session1";
|
||||
Session session = new Session();
|
||||
|
||||
cacheService.addToCache(key, session);
|
||||
|
||||
assertNotNull(cacheService.getFromCache(key));
|
||||
assertEquals(session, cacheService.getFromCache(key));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetFromCache() {
|
||||
String key = "session1";
|
||||
Session session = new Session();
|
||||
|
||||
cacheService.addToCache(key, session);
|
||||
|
||||
Session retrievedSession = cacheService.getFromCache(key);
|
||||
assertNotNull(retrievedSession);
|
||||
assertEquals(session, retrievedSession);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRemoveFromCache() {
|
||||
String key = "session1";
|
||||
Session session = new Session();
|
||||
|
||||
cacheService.addToCache(key, session);
|
||||
cacheService.removeFromCache(key);
|
||||
|
||||
assertNull(cacheService.getFromCache(key));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetFromCacheWithNonExistentKey() {
|
||||
assertNull(cacheService.getFromCache("nonExistentKey"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
package com.ninjaws.quiz.services;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.ninjaws.quiz.entities.Category;
|
||||
import com.ninjaws.quiz.models.ApiResponse;
|
||||
import com.ninjaws.quiz.models.QuestionDTO;
|
||||
|
||||
// public class CleanerServiceTest {
|
||||
|
||||
// @InjectMocks
|
||||
// private CleanerService cleanerService;
|
||||
|
||||
// @Mock
|
||||
// private ObjectMapper objectMapper;
|
||||
|
||||
// @BeforeEach
|
||||
// public void setUp() {
|
||||
// MockitoAnnotations.openMocks(this);
|
||||
// }
|
||||
|
||||
// @Test
|
||||
// public void testExtractApiResponse() throws IOException {
|
||||
// String json = "{\"response_code\":200,\"results\":[{\"type\":\"multiple choice\",\"difficulty\":\"easy\",\"category\":\"general knowledge\",\"question\":\"What is the capital of France?\",\"correct_answer\":\"Paris\",\"incorrect_answers\":[\"Berlin\",\"Madrid\",\"Rome\"]}]}"; // Example JSON input
|
||||
|
||||
// Question question = new Question();
|
||||
// question.setType("multiple choice");
|
||||
// question.setDifficulty("easy");
|
||||
// question.setCategory("general knowledge");
|
||||
// question.setQuestion("What is the capital of France?");
|
||||
// question.setCorrectAnswer("Paris");
|
||||
// question.setIncorrectAnswers(Arrays.asList("Berlin", "Madrid", "Rome"));
|
||||
|
||||
// ApiResponse expectedResponse = new ApiResponse();
|
||||
// expectedResponse.setResponseCode(200);
|
||||
// expectedResponse.setResults(Arrays.asList(question));
|
||||
// when(objectMapper.readValue(json, ApiResponse.class)).thenReturn(expectedResponse);
|
||||
|
||||
// ApiResponse actualResponse = cleanerService.extractApiResponse(json);
|
||||
// assertThat(actualResponse).isNotNull();
|
||||
// assertThat(actualResponse.getResponseCode()).isEqualTo(expectedResponse.getResponseCode());
|
||||
// assertThat(actualResponse.getResults()).isNotEmpty();
|
||||
// assertThat(actualResponse.getResults().get(0).getQuestion()).isEqualTo(question.getQuestion());
|
||||
// verify(objectMapper, times(1)).readValue(json, ApiResponse.class);
|
||||
// }
|
||||
// }
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
public class CleanerServiceTest {
|
||||
|
||||
@InjectMocks
|
||||
private CleanerService cleanerService;
|
||||
|
||||
@Mock
|
||||
private ObjectMapper objectMapper;
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
MockitoAnnotations.openMocks(this);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExtractApiResponse() throws IOException {
|
||||
String json = "{\"response_code\":200,\"results\":[{\"type\":\"multiple choice\",\"difficulty\":\"easy\",\"category\":\"general knowledge\",\"question\":\"What is the capital of France?\",\"correct_answer\":\"Paris\",\"incorrect_answers\":[\"Berlin\",\"Madrid\",\"Rome\"]}]}"; // Example JSON input
|
||||
|
||||
// Create a Category object for the question
|
||||
Category category = new Category();
|
||||
category.setId(1L);
|
||||
category.setName("General Knowledge");
|
||||
|
||||
// Create the Question object with the category
|
||||
QuestionDTO question = new QuestionDTO();
|
||||
question.setType("multiple choice");
|
||||
question.setDifficulty("easy");
|
||||
question.setCategory(category); // Set the Category object here
|
||||
question.setQuestion("What is the capital of France?");
|
||||
question.setCorrectAnswer("Paris");
|
||||
question.setIncorrectAnswers(Arrays.asList("Berlin", "Madrid", "Rome"));
|
||||
|
||||
ApiResponse expectedResponse = new ApiResponse();
|
||||
expectedResponse.setResponseCode(200);
|
||||
expectedResponse.setResults(Arrays.asList(question));
|
||||
when(objectMapper.readValue(json, ApiResponse.class)).thenReturn(expectedResponse);
|
||||
|
||||
ApiResponse actualResponse = cleanerService.extractApiResponse(json);
|
||||
assertThat(actualResponse).isNotNull();
|
||||
assertThat(actualResponse.getResponseCode()).isEqualTo(expectedResponse.getResponseCode());
|
||||
assertThat(actualResponse.getResults()).isNotEmpty();
|
||||
assertThat(actualResponse.getResults().get(0).getQuestion()).isEqualTo(question.getQuestion());
|
||||
assertThat(actualResponse.getResults().get(0).getCategory().getName()).isEqualTo(category.getName()); // Check category name
|
||||
verify(objectMapper, times(1)).readValue(json, ApiResponse.class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,202 @@
|
||||
package com.ninjaws.quiz.services;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyList;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import com.ninjaws.quiz.entities.Category;
|
||||
import com.ninjaws.quiz.models.QuestionDTO;
|
||||
import com.ninjaws.quiz.repositories.QuestionRepository;
|
||||
|
||||
// public class DataServiceTest {
|
||||
|
||||
// @InjectMocks
|
||||
// private DataService dataService;
|
||||
|
||||
// @Mock
|
||||
// private QuestionRepository questionRepository;
|
||||
|
||||
// private Question question;
|
||||
|
||||
|
||||
// @BeforeEach
|
||||
// public void setUp() {
|
||||
// MockitoAnnotations.openMocks(this);
|
||||
|
||||
// question = new Question();
|
||||
// question.setType("multiple choice");
|
||||
// question.setDifficulty("easy");
|
||||
// question.setCategory("general knowledge");
|
||||
// question.setQuestion("What is the capital of France?");
|
||||
// question.setCorrectAnswer("Paris");
|
||||
// question.setIncorrectAnswers(Arrays.asList("Berlin", "Madrid", "Rome"));
|
||||
// }
|
||||
|
||||
// @Test
|
||||
// public void testSaveQuestion() {
|
||||
// when(questionRepository.save(any(Question.class))).thenReturn(question);
|
||||
|
||||
// Question savedQuestion = dataService.saveQuestion(question);
|
||||
// assertThat(savedQuestion).isNotNull();
|
||||
// assertThat(savedQuestion.getQuestion()).isEqualTo("What is the capital of France?");
|
||||
// verify(questionRepository, times(1)).save(question);
|
||||
// }
|
||||
|
||||
// @Test
|
||||
// public void testGetAllQuestions() {
|
||||
// when(questionRepository.findAll()).thenReturn(Arrays.asList(question));
|
||||
|
||||
// List<Question> questions = dataService.getAllQuestions();
|
||||
// assertThat(questions).isNotEmpty();
|
||||
// assertThat(questions.size()).isEqualTo(1);
|
||||
// assertThat(questions.get(0).getQuestion()).isEqualTo("What is the capital of France?");
|
||||
// verify(questionRepository, times(1)).findAll();
|
||||
// }
|
||||
|
||||
// @Test
|
||||
// public void testFindQuestionById() {
|
||||
// when(questionRepository.findById(any(Long.class))).thenReturn(Optional.of(question));
|
||||
|
||||
// Optional<Question> foundQuestion = questionRepository.findById(1L);
|
||||
// assertThat(foundQuestion).isPresent();
|
||||
// assertThat(foundQuestion.get().getQuestion()).isEqualTo("What is the capital of France?");
|
||||
// verify(questionRepository, times(1)).findById(1L);
|
||||
// }
|
||||
|
||||
// @Test
|
||||
// public void testSaveQuestions() {
|
||||
// Question question1 = new Question();
|
||||
// question1.setType("multiple choice");
|
||||
// question1.setDifficulty("easy");
|
||||
// question1.setCategory("general knowledge");
|
||||
// question1.setQuestion("What is the capital of France?");
|
||||
// question1.setCorrectAnswer("Paris");
|
||||
// question1.setIncorrectAnswers(Arrays.asList("Berlin", "Madrid", "Rome"));
|
||||
|
||||
// Question question2 = new Question();
|
||||
// question2.setType("multiple choice");
|
||||
// question2.setDifficulty("medium");
|
||||
// question2.setCategory("science");
|
||||
// question2.setQuestion("What is the chemical symbol for water?");
|
||||
// question2.setCorrectAnswer("H2O");
|
||||
// question2.setIncorrectAnswers(Arrays.asList("CO2", "O2", "NaCl"));
|
||||
|
||||
// when(questionRepository.saveAll(anyList())).thenReturn(Arrays.asList(question1, question2));
|
||||
|
||||
// List<Question> questions = dataService.saveQuestions(Arrays.asList(question1, question2));
|
||||
|
||||
// assertThat(questions).hasSize(2);
|
||||
// assertThat(questions.get(0).getQuestion()).isEqualTo("What is the capital of France?");
|
||||
// assertThat(questions.get(1).getQuestion()).isEqualTo("What is the chemical symbol for water?");
|
||||
// verify(questionRepository, times(1)).saveAll(anyList());
|
||||
// }
|
||||
// }
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
public class DataServiceTest {
|
||||
|
||||
@InjectMocks
|
||||
private DataService dataService;
|
||||
|
||||
@Mock
|
||||
private QuestionRepository questionRepository;
|
||||
|
||||
private QuestionDTO question;
|
||||
private Category category;
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
MockitoAnnotations.openMocks(this);
|
||||
|
||||
category = new Category();
|
||||
category.setId(1L);
|
||||
category.setName("General Knowledge");
|
||||
|
||||
question = new QuestionDTO();
|
||||
question.setType("multiple choice");
|
||||
question.setDifficulty("easy");
|
||||
question.setCategory(category); // Set the Category object here
|
||||
question.setQuestion("What is the capital of France?");
|
||||
question.setCorrectAnswer("Paris");
|
||||
question.setIncorrectAnswers(Arrays.asList("Berlin", "Madrid", "Rome"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSaveQuestion() {
|
||||
when(questionRepository.save(any(QuestionDTO.class))).thenReturn(question);
|
||||
|
||||
QuestionDTO savedQuestion = dataService.saveQuestion(question);
|
||||
assertThat(savedQuestion).isNotNull();
|
||||
assertThat(savedQuestion.getQuestion()).isEqualTo("What is the capital of France?");
|
||||
assertThat(savedQuestion.getCategory().getName()).isEqualTo("General Knowledge"); // Check category name
|
||||
verify(questionRepository, times(1)).save(question);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetAllQuestions() {
|
||||
when(questionRepository.findAll()).thenReturn(Arrays.asList(question));
|
||||
|
||||
List<QuestionDTO> questions = dataService.getAllQuestions();
|
||||
assertThat(questions).isNotEmpty();
|
||||
assertThat(questions.size()).isEqualTo(1);
|
||||
assertThat(questions.get(0).getQuestion()).isEqualTo("What is the capital of France?");
|
||||
assertThat(questions.get(0).getCategory().getName()).isEqualTo("General Knowledge"); // Check category name
|
||||
verify(questionRepository, times(1)).findAll();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindQuestionById() {
|
||||
when(questionRepository.findById(any(Long.class))).thenReturn(Optional.of(question));
|
||||
|
||||
Optional<QuestionDTO> foundQuestion = questionRepository.findById(1L);
|
||||
assertThat(foundQuestion).isPresent();
|
||||
assertThat(foundQuestion.get().getQuestion()).isEqualTo("What is the capital of France?");
|
||||
assertThat(foundQuestion.get().getCategory().getName()).isEqualTo("General Knowledge"); // Check category name
|
||||
verify(questionRepository, times(1)).findById(1L);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSaveQuestions() {
|
||||
QuestionDTO question1 = new QuestionDTO();
|
||||
question1.setType("multiple choice");
|
||||
question1.setDifficulty("easy");
|
||||
question1.setCategory(category); // Set the Category object here
|
||||
question1.setQuestion("What is the capital of France?");
|
||||
question1.setCorrectAnswer("Paris");
|
||||
question1.setIncorrectAnswers(Arrays.asList("Berlin", "Madrid", "Rome"));
|
||||
|
||||
QuestionDTO question2 = new QuestionDTO();
|
||||
Category category2 = new Category();
|
||||
category2.setId(2L);
|
||||
category2.setName("Science");
|
||||
question2.setType("multiple choice");
|
||||
question2.setDifficulty("medium");
|
||||
question2.setCategory(category2); // Set the Category object here
|
||||
question2.setQuestion("What is the chemical symbol for water?");
|
||||
question2.setCorrectAnswer("H2O");
|
||||
question2.setIncorrectAnswers(Arrays.asList("CO2", "O2", "NaCl"));
|
||||
|
||||
when(questionRepository.saveAll(anyList())).thenReturn(Arrays.asList(question1, question2));
|
||||
|
||||
List<QuestionDTO> questions = dataService.saveQuestions(Arrays.asList(question1, question2));
|
||||
|
||||
assertThat(questions).hasSize(2);
|
||||
assertThat(questions.get(0).getQuestion()).isEqualTo("What is the capital of France?");
|
||||
assertThat(questions.get(1).getQuestion()).isEqualTo("What is the chemical symbol for water?");
|
||||
verify(questionRepository, times(1)).saveAll(anyList());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package com.ninjaws.quiz.services;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import com.ninjaws.quiz.models.QuizSettings;
|
||||
import com.ninjaws.quiz.models.Request;
|
||||
|
||||
public class QueueServiceTest {
|
||||
|
||||
private QueueService queueService;
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
queueService = new QueueService();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddToQueue() {
|
||||
QuizSettings quizSettings = new QuizSettings();
|
||||
Request request = new Request("session1", quizSettings);
|
||||
|
||||
boolean added = queueService.addToQueue(request);
|
||||
assertTrue(added);
|
||||
|
||||
for (int i = 0; i < 4; i++) {
|
||||
added = queueService.addToQueue(new Request("session" + (i + 2), new QuizSettings()));
|
||||
assertTrue(added);
|
||||
}
|
||||
|
||||
added = queueService.addToQueue(new Request("session6", new QuizSettings()));
|
||||
assertFalse(added);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPopFromQueue() {
|
||||
QuizSettings quizSettings1 = new QuizSettings();
|
||||
QuizSettings quizSettings2 = new QuizSettings();
|
||||
|
||||
Request request1 = new Request("session1", quizSettings1);
|
||||
Request request2 = new Request("session2", quizSettings2);
|
||||
|
||||
queueService.addToQueue(request1);
|
||||
queueService.addToQueue(request2);
|
||||
|
||||
Request poppedRequest = queueService.popFromQueue();
|
||||
assertEquals(request1, poppedRequest);
|
||||
|
||||
poppedRequest = queueService.popFromQueue();
|
||||
assertEquals(request2, poppedRequest);
|
||||
|
||||
assertNull(queueService.popFromQueue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPopFromEmptyQueue() {
|
||||
assertNull(queueService.popFromQueue());
|
||||
}
|
||||
}
|
||||
@@ -71,7 +71,7 @@ export class QuizService {
|
||||
url += catIndex !== 0 ? `&category=${catIndex}` : '';
|
||||
|
||||
const diff = quizParams.difficulty.toLowerCase();
|
||||
url += diff !== 'any difficulty' ? `&category=${diff}` : '';
|
||||
url += diff !== 'any difficulty' ? `&difficulty=${diff}` : '';
|
||||
|
||||
const typeIndex = this.getTypes().indexOf(quizParams.type);
|
||||
url +=
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
start_time=$(date +%s)
|
||||
|
||||
for i in {1..6}; do
|
||||
./LoadTestOne.sh &
|
||||
./LoadTestOne-WithParams.sh &
|
||||
sleep 0.1
|
||||
done
|
||||
|
||||
|
||||
Executable
+46
@@ -0,0 +1,46 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Step 1: Get the sessionId from the questions endpoint
|
||||
sessionIdJson=$(curl -s "http://localhost:9090/quiz/questions?amount=25&category=27&difficulty=medium&type=boolean")
|
||||
|
||||
statusCode=$(echo "$sessionIdJson" | grep -o '"statusCode":[0-9]*' | sed 's/"statusCode"://')
|
||||
|
||||
if [ "$statusCode" -ne 0 ]; then
|
||||
echo "Exiting: statusCode is not 0."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
sessionId=$(echo "$sessionIdJson" | grep -o '"sessionId":"[^"]*"' | sed 's/"sessionId":"//;s/"//')
|
||||
|
||||
if [ -z "$sessionId" ]; then
|
||||
echo "Failed to get sessionId"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "Received sessionId: $sessionId"
|
||||
|
||||
# Step 2: Poll the status endpoint every 2 seconds
|
||||
while true; do
|
||||
status_response=$(curl -s http://localhost:9090/quiz/status/"$sessionId")
|
||||
|
||||
if [ $? -eq 0 ]; then
|
||||
|
||||
statusCode=$(echo "$status_response" | grep -o '"statusCode":[0-9]*' | sed 's/"statusCode"://')
|
||||
|
||||
if [ "$statusCode" -ne 0 ]; then
|
||||
echo "Exiting: statusCode is $statusCode"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Check if the response is JSON (starts with '{')
|
||||
if [[ $status_response == \{* ]]; then
|
||||
# echo "$statusCode"
|
||||
echo "$sessionId: done! status: $statusCode"
|
||||
break
|
||||
fi
|
||||
else
|
||||
echo "Status check failed, retrying..."
|
||||
fi
|
||||
|
||||
sleep 2
|
||||
done
|
||||
@@ -30,7 +30,8 @@ while true; do
|
||||
break
|
||||
fi
|
||||
else
|
||||
echo "Status check failed, retrying..."
|
||||
echo "Status check failed, exiting..."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
sleep 2
|
||||
|
||||
|
Before Width: | Height: | Size: 62 KiB After Width: | Height: | Size: 62 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 68 KiB |
Reference in New Issue
Block a user