2023-12-15 08:59:52 +01:00
|
|
|
package com.commafeed.integration;
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
2024-08-07 08:10:14 +02:00
|
|
|
import java.net.HttpCookie;
|
2023-12-15 08:59:52 +01:00
|
|
|
import java.net.URL;
|
|
|
|
|
import java.nio.charset.StandardCharsets;
|
2023-12-16 09:02:56 +01:00
|
|
|
import java.time.Duration;
|
2024-08-07 08:10:14 +02:00
|
|
|
import java.util.List;
|
2023-12-15 08:59:52 +01:00
|
|
|
import java.util.Objects;
|
|
|
|
|
|
|
|
|
|
import org.apache.commons.io.IOUtils;
|
2024-08-07 08:10:14 +02:00
|
|
|
import org.apache.hc.core5.http.HttpStatus;
|
2023-12-16 09:02:56 +01:00
|
|
|
import org.awaitility.Awaitility;
|
2023-12-15 08:59:52 +01:00
|
|
|
import org.glassfish.jersey.client.JerseyClientBuilder;
|
2024-08-07 08:10:14 +02:00
|
|
|
import org.glassfish.jersey.jackson.internal.jackson.jaxrs.json.JacksonJsonProvider;
|
2024-01-06 08:37:12 +01:00
|
|
|
import org.junit.jupiter.api.AfterEach;
|
2023-12-15 08:59:52 +01:00
|
|
|
import org.junit.jupiter.api.BeforeEach;
|
|
|
|
|
import org.mockserver.client.MockServerClient;
|
2024-08-07 08:10:14 +02:00
|
|
|
import org.mockserver.integration.ClientAndServer;
|
2023-12-15 08:59:52 +01:00
|
|
|
import org.mockserver.model.HttpRequest;
|
|
|
|
|
import org.mockserver.model.HttpResponse;
|
|
|
|
|
|
2024-08-07 08:10:14 +02:00
|
|
|
import com.commafeed.JacksonCustomizer;
|
2023-12-15 08:59:52 +01:00
|
|
|
import com.commafeed.frontend.model.Entries;
|
|
|
|
|
import com.commafeed.frontend.model.Subscription;
|
|
|
|
|
import com.commafeed.frontend.model.request.SubscribeRequest;
|
2024-08-07 08:10:14 +02:00
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
2023-12-15 08:59:52 +01:00
|
|
|
|
2024-08-07 08:10:14 +02:00
|
|
|
import io.restassured.RestAssured;
|
|
|
|
|
import io.restassured.http.Header;
|
2023-12-17 14:11:15 +01:00
|
|
|
import jakarta.ws.rs.client.Client;
|
|
|
|
|
import jakarta.ws.rs.client.Entity;
|
2024-08-07 08:10:14 +02:00
|
|
|
import jakarta.ws.rs.core.Form;
|
|
|
|
|
import jakarta.ws.rs.core.HttpHeaders;
|
2023-12-17 14:11:15 +01:00
|
|
|
import jakarta.ws.rs.core.Response;
|
2023-12-15 08:59:52 +01:00
|
|
|
import lombok.Getter;
|
|
|
|
|
|
|
|
|
|
@Getter
|
2023-12-16 09:02:56 +01:00
|
|
|
public abstract class BaseIT {
|
2023-12-15 08:59:52 +01:00
|
|
|
|
2024-01-24 12:49:20 +01:00
|
|
|
private static final HttpRequest FEED_REQUEST = HttpRequest.request().withMethod("GET").withPath("/");
|
|
|
|
|
|
2024-08-07 08:10:14 +02:00
|
|
|
private MockServerClient mockServerClient;
|
2023-12-15 08:59:52 +01:00
|
|
|
private Client client;
|
|
|
|
|
private String feedUrl;
|
|
|
|
|
private String baseUrl;
|
|
|
|
|
private String apiBaseUrl;
|
|
|
|
|
private String webSocketUrl;
|
|
|
|
|
|
2024-06-19 19:32:25 +02:00
|
|
|
protected JerseyClientBuilder configureClientBuilder(JerseyClientBuilder base) {
|
|
|
|
|
return base;
|
2023-12-16 19:55:34 +01:00
|
|
|
}
|
|
|
|
|
|
2023-12-15 08:59:52 +01:00
|
|
|
@BeforeEach
|
2024-08-07 08:10:14 +02:00
|
|
|
void init() throws IOException {
|
|
|
|
|
this.mockServerClient = ClientAndServer.startClientAndServer(0);
|
2024-01-24 12:49:20 +01:00
|
|
|
|
2024-08-07 08:10:14 +02:00
|
|
|
ObjectMapper mapper = new ObjectMapper();
|
|
|
|
|
new JacksonCustomizer().customize(mapper);
|
|
|
|
|
this.client = configureClientBuilder(new JerseyClientBuilder().register(new JacksonJsonProvider(mapper))).build();
|
2023-12-15 08:59:52 +01:00
|
|
|
this.feedUrl = "http://localhost:" + mockServerClient.getPort() + "/";
|
2024-08-07 08:10:14 +02:00
|
|
|
this.baseUrl = "http://localhost:8085/";
|
2023-12-15 08:59:52 +01:00
|
|
|
this.apiBaseUrl = this.baseUrl + "rest/";
|
2024-08-07 08:10:14 +02:00
|
|
|
this.webSocketUrl = "ws://localhost:8085/ws";
|
|
|
|
|
|
|
|
|
|
URL resource = Objects.requireNonNull(getClass().getResource("/feed/rss.xml"));
|
|
|
|
|
this.mockServerClient.when(FEED_REQUEST)
|
|
|
|
|
.respond(HttpResponse.response().withBody(IOUtils.toString(resource, StandardCharsets.UTF_8)));
|
2023-12-15 08:59:52 +01:00
|
|
|
}
|
|
|
|
|
|
2024-01-06 08:37:12 +01:00
|
|
|
@AfterEach
|
|
|
|
|
void cleanup() {
|
2024-08-07 08:10:14 +02:00
|
|
|
if (this.mockServerClient != null) {
|
|
|
|
|
this.mockServerClient.close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this.client != null) {
|
|
|
|
|
this.client.close();
|
|
|
|
|
}
|
2024-01-06 08:37:12 +01:00
|
|
|
}
|
|
|
|
|
|
2024-01-24 12:49:20 +01:00
|
|
|
protected void feedNowReturnsMoreEntries() throws IOException {
|
|
|
|
|
mockServerClient.clear(FEED_REQUEST);
|
|
|
|
|
|
|
|
|
|
URL resource = Objects.requireNonNull(getClass().getResource("/feed/rss_2.xml"));
|
|
|
|
|
mockServerClient.when(FEED_REQUEST).respond(HttpResponse.response().withBody(IOUtils.toString(resource, StandardCharsets.UTF_8)));
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-07 08:10:14 +02:00
|
|
|
protected List<HttpCookie> login() {
|
|
|
|
|
Form form = new Form();
|
|
|
|
|
form.param("j_username", "admin");
|
|
|
|
|
form.param("j_password", "admin");
|
|
|
|
|
|
|
|
|
|
List<Header> setCookieHeaders = RestAssured.given()
|
|
|
|
|
.formParams("j_username", "admin", "j_password", "admin")
|
|
|
|
|
.post(baseUrl + "j_security_check")
|
|
|
|
|
.then()
|
|
|
|
|
.statusCode(HttpStatus.SC_OK)
|
|
|
|
|
.extract()
|
|
|
|
|
.headers()
|
|
|
|
|
.getList(HttpHeaders.SET_COOKIE);
|
|
|
|
|
return setCookieHeaders.stream().flatMap(h -> HttpCookie.parse(h.getValue()).stream()).toList();
|
2023-12-15 08:59:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected Long subscribe(String feedUrl) {
|
|
|
|
|
SubscribeRequest subscribeRequest = new SubscribeRequest();
|
|
|
|
|
subscribeRequest.setUrl(feedUrl);
|
|
|
|
|
subscribeRequest.setTitle("my title for this feed");
|
2023-12-16 19:55:34 +01:00
|
|
|
return client.target(apiBaseUrl + "feed/subscribe").request().post(Entity.json(subscribeRequest), Long.class);
|
2023-12-15 08:59:52 +01:00
|
|
|
}
|
|
|
|
|
|
2023-12-16 09:02:56 +01:00
|
|
|
protected Long subscribeAndWaitForEntries(String feedUrl) {
|
|
|
|
|
Long subscriptionId = subscribe(feedUrl);
|
|
|
|
|
Awaitility.await().atMost(Duration.ofSeconds(15)).until(() -> getFeedEntries(subscriptionId), e -> e.getEntries().size() == 2);
|
|
|
|
|
return subscriptionId;
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-15 08:59:52 +01:00
|
|
|
protected Subscription getSubscription(Long subscriptionId) {
|
2023-12-16 19:55:34 +01:00
|
|
|
return client.target(apiBaseUrl + "feed/get/{id}").resolveTemplate("id", subscriptionId).request().get(Subscription.class);
|
2023-12-15 08:59:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected Entries getFeedEntries(long subscriptionId) {
|
|
|
|
|
Response response = client.target(apiBaseUrl + "feed/entries")
|
|
|
|
|
.queryParam("id", subscriptionId)
|
2023-12-29 15:17:11 +01:00
|
|
|
.queryParam("readType", "all")
|
2023-12-15 08:59:52 +01:00
|
|
|
.request()
|
|
|
|
|
.get();
|
|
|
|
|
return response.readEntity(Entries.class);
|
|
|
|
|
}
|
2024-01-24 12:49:20 +01:00
|
|
|
|
|
|
|
|
protected void forceRefreshAllFeeds() {
|
|
|
|
|
client.target(apiBaseUrl + "feed/refreshAll").request().get(Void.class);
|
|
|
|
|
}
|
2023-12-15 08:59:52 +01:00
|
|
|
}
|