Add RestClient and DataFactory for HTTP requests and test data generation
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
package com.humanbooster;
|
||||
|
||||
import com.humanbooster.client.RestClient;
|
||||
import com.humanbooster.config.HibernateConfig;
|
||||
import com.humanbooster.config.ServerConfig;
|
||||
import com.humanbooster.dao.AdDao;
|
||||
import com.humanbooster.dao.ArticleDao;
|
||||
import com.humanbooster.dao.UserDao;
|
||||
import com.humanbooster.factory.DataFactory;
|
||||
import com.humanbooster.model.Ad;
|
||||
import com.humanbooster.model.Article;
|
||||
import com.humanbooster.model.User;
|
||||
@@ -17,14 +19,14 @@ import org.hibernate.boot.MetadataSources;
|
||||
import org.hibernate.boot.registry.StandardServiceRegistry;
|
||||
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDate;
|
||||
import java.util.List;
|
||||
|
||||
public class App {
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Démarrage de l'application");
|
||||
|
||||
RestClient client = new RestClient();
|
||||
|
||||
try {
|
||||
ServerConfig serverConfig = new ServerConfig();
|
||||
serverConfig.startServer();
|
||||
@@ -45,45 +47,16 @@ public class App {
|
||||
sessionFactory = metadata.buildSessionFactory();
|
||||
}
|
||||
|
||||
User user = new User("Bob", "bob@example.com", null);
|
||||
|
||||
user.setArticles(List.of(
|
||||
new Article("Article 1", "Contenu de l'article 1", LocalDate.now(), user, 0),
|
||||
new Article("Article 2", "Contenu de l'article 2", LocalDate.now(), user, 0)
|
||||
));
|
||||
|
||||
List<Ad> ads = List.of(
|
||||
(new Ad(
|
||||
"Ad 1",
|
||||
"Contenu de l'annonce 1",
|
||||
LocalDate.now(),
|
||||
LocalDate.now().plusDays(7),
|
||||
"contact@example.com",
|
||||
BigDecimal.valueOf(12))),
|
||||
(new Ad(
|
||||
"Ad 2",
|
||||
"Contenu de l'annonce 2",
|
||||
LocalDate.now(),
|
||||
LocalDate.now().plusDays(10),
|
||||
"contact@example.com",
|
||||
BigDecimal.valueOf(6.7)))
|
||||
|
||||
);
|
||||
|
||||
UserService userService = new UserService(new UserDao(sessionFactory));
|
||||
ArticleService articleService = new ArticleService(new ArticleDao(sessionFactory));
|
||||
AdService adService = new AdService(new AdDao(sessionFactory));
|
||||
|
||||
DataFactory dataFactory = new DataFactory();
|
||||
|
||||
cleanDatabase(userService, articleService, adService);
|
||||
|
||||
userService.createUser(user);
|
||||
|
||||
articleService.findArticlesByCriteria("test", 3L, 1, 1).forEach(article -> {
|
||||
System.out.println("\nArticle trouvé :" + article.toString());
|
||||
}
|
||||
);
|
||||
|
||||
ads.forEach(adService::createAd);
|
||||
userService.createUser(dataFactory.createTestUser("Michel", "michel@test.fr"));
|
||||
dataFactory.createTestAds().forEach(adService::createAd);
|
||||
|
||||
sessionFactory.close();
|
||||
System.out.print("Fin du programme");
|
||||
|
||||
@@ -1,6 +0,0 @@
|
||||
package com.humanbooster.annotation;
|
||||
|
||||
public @interface Pair {
|
||||
|
||||
String message() default "Le nombre doit être pair";
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
package com.humanbooster.annotation;
|
||||
|
||||
import jakarta.validation.ConstraintValidator;
|
||||
import jakarta.validation.ConstraintValidatorContext;
|
||||
|
||||
public class PairValidator implements ConstraintValidator<Pair, Integer> {
|
||||
|
||||
@Override
|
||||
public boolean isValid(Integer integer, ConstraintValidatorContext constraintValidatorContext) {
|
||||
return (integer != null) && (integer % 2 == 0);
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
package com.humanbooster.api;
|
||||
|
||||
import org.glassfish.jersey.jackson.JacksonFeature;
|
||||
import org.glassfish.jersey.server.ResourceConfig;
|
||||
|
||||
public class ApiApplication extends ResourceConfig {
|
||||
public ApiApplication() {
|
||||
packages("com.humanbooster");
|
||||
register(JacksonFeature.class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package com.humanbooster.client;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
|
||||
public class RestClient {
|
||||
|
||||
private final HttpClient httpClient = HttpClient.newHttpClient();
|
||||
|
||||
public String sendGetRequest(String url, String method, String body) {
|
||||
|
||||
HttpRequest.Builder builder = HttpRequest.newBuilder()
|
||||
.uri(URI.create(url))
|
||||
.header("Content-Type", "application/json");
|
||||
|
||||
switch (method.toUpperCase()) {
|
||||
case "GET":
|
||||
builder.GET();
|
||||
break;
|
||||
case "POST":
|
||||
builder.POST(HttpRequest.BodyPublishers.ofString(body));
|
||||
break;
|
||||
case "PUT":
|
||||
builder.PUT(HttpRequest.BodyPublishers.ofString(body));
|
||||
break;
|
||||
case "DELETE":
|
||||
builder.DELETE();
|
||||
break;
|
||||
default:
|
||||
throw new IllegalArgumentException("Invalid HTTP method: " + method);
|
||||
}
|
||||
|
||||
HttpRequest request = builder.build();
|
||||
|
||||
HttpResponse<String> response;
|
||||
|
||||
try {
|
||||
response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
|
||||
return parseResponse(response.body());
|
||||
} catch (Exception e) {
|
||||
System.err.println("Error occurred while sending the request: " + e.getMessage());
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private String parseResponse(String response) {
|
||||
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
try {
|
||||
JsonNode jsonNode = mapper.readTree(response);
|
||||
return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonNode);
|
||||
} catch (Exception e) {
|
||||
System.err.println("Error occurred while parsing the response: " + e.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -1,18 +1,23 @@
|
||||
package com.humanbooster.config;
|
||||
|
||||
import com.humanbooster.api.ApiApplication;
|
||||
import org.eclipse.jetty.server.Server;
|
||||
import org.eclipse.jetty.servlet.ServletContextHandler;
|
||||
import org.eclipse.jetty.servlet.ServletHolder;
|
||||
import org.glassfish.jersey.jackson.JacksonFeature;
|
||||
import org.glassfish.jersey.server.ResourceConfig;
|
||||
import org.glassfish.jersey.servlet.ServletContainer;
|
||||
|
||||
public class ServerConfig {
|
||||
public class ServerConfig extends ResourceConfig {
|
||||
|
||||
public ServerConfig() {
|
||||
packages("com.humanbooster");
|
||||
register(JacksonFeature.class);
|
||||
}
|
||||
|
||||
public void startServer() throws Exception {
|
||||
System.out.println("Starting server...");
|
||||
|
||||
ResourceConfig config = new ApiApplication();
|
||||
ResourceConfig config = this;
|
||||
|
||||
ServletHolder servlet = new ServletHolder(new ServletContainer(config));
|
||||
Server server = new Server(80);
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.humanbooster.factory;
|
||||
|
||||
import com.humanbooster.model.Ad;
|
||||
import com.humanbooster.model.Article;
|
||||
import com.humanbooster.model.User;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDate;
|
||||
import java.util.List;
|
||||
|
||||
public class DataFactory {
|
||||
|
||||
public User createTestUser(String name, String email) {
|
||||
User user = new User(name, email, null);
|
||||
|
||||
user.setArticles(List.of(
|
||||
new Article("Article 1", "Contenu de l'article 1", LocalDate.now(), user, 0),
|
||||
new Article("Article 2", "Contenu de l'article 2", LocalDate.now(), user, 0)
|
||||
));
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
public List<Ad> createTestAds() {
|
||||
return List.of(
|
||||
(new Ad(
|
||||
"Ad 1",
|
||||
"Contenu de l'annonce 1",
|
||||
LocalDate.now(),
|
||||
LocalDate.now().plusDays(7),
|
||||
"contact@example.com",
|
||||
BigDecimal.valueOf(12))),
|
||||
(new Ad(
|
||||
"Ad 2",
|
||||
"Contenu de l'annonce 2",
|
||||
LocalDate.now(),
|
||||
LocalDate.now().plusDays(10),
|
||||
"contact@example.com",
|
||||
BigDecimal.valueOf(6.7)))
|
||||
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user