blob: fcc655f92849f6f0714862410888b77e216018af [file] [log] [blame]
margaretha1960ea52023-02-28 11:20:15 +01001package de.ids_mannheim.korap.scenario;
2
Marc Kupietzd43a98d2023-09-22 17:11:46 +02003import static org.junit.jupiter.api.Assertions.assertEquals;
4import static org.junit.jupiter.api.Assertions.assertNotNull;
5import static org.junit.jupiter.api.Assertions.assertTrue;
margaretha1960ea52023-02-28 11:20:15 +01006
Marc Kupietzd43a98d2023-09-22 17:11:46 +02007import org.junit.jupiter.api.Test;
margaretha1960ea52023-02-28 11:20:15 +01008import org.springframework.test.context.ContextConfiguration;
margaretha1960ea52023-02-28 11:20:15 +01009import com.fasterxml.jackson.databind.JsonNode;
margaretha1960ea52023-02-28 11:20:15 +010010import de.ids_mannheim.korap.authentication.http.HttpAuthorizationHandler;
11import de.ids_mannheim.korap.config.Attributes;
12import de.ids_mannheim.korap.config.SpringJerseyTest;
13import de.ids_mannheim.korap.exceptions.KustvaktException;
14import de.ids_mannheim.korap.exceptions.StatusCodes;
15import de.ids_mannheim.korap.utils.JsonUtils;
margaretha96c309d2023-08-16 12:24:12 +020016import jakarta.ws.rs.core.Response;
17import jakarta.ws.rs.core.Response.Status;
margaretha1960ea52023-02-28 11:20:15 +010018
19/**
20 * <p>Test scenario for ICC (International Comparable Corpus)
21 * instance</p>
Marc Kupietzd43a98d2023-09-22 17:11:46 +020022 * <p>
margaretha1960ea52023-02-28 11:20:15 +010023 * The instance requires user authentication and access to data is
24 * restricted to only logged-in users.
Marc Kupietzd43a98d2023-09-22 17:11:46 +020025 * <p>
margaretha1960ea52023-02-28 11:20:15 +010026 * This class uses <em>test-config-icc.xml</em> spring XML config
27 * defining the location of a specific kustvakt configuration file for
Marc Kupietzd43a98d2023-09-22 17:11:46 +020028 * this instance:<em>kustvakt-icc.conf</em>.
29 *
margaretha3af22a42023-04-21 15:31:58 +020030 * <p>
margaretha35e1ca22023-11-16 22:00:01 +010031 * To run a Kustvakt jar with ICC setup, the following files are
32 * needed:
margaretha3af22a42023-04-21 15:31:58 +020033 * </p>
34 * <ul>
35 * <li>a Spring configuration file</li>
margaretha35e1ca22023-11-16 22:00:01 +010036 * <li>a Kustvakt configuration file that must be placed at the jar
37 * folder</li>
38 * <li>a JDBC properties file that must be placed at the jar
39 * folder</li>
margaretha3af22a42023-04-21 15:31:58 +020040 * </ul>
Marc Kupietzd43a98d2023-09-22 17:11:46 +020041 * <p>
margaretha3af22a42023-04-21 15:31:58 +020042 * Example:
Marc Kupietzd43a98d2023-09-22 17:11:46 +020043 *
margaretha1960ea52023-02-28 11:20:15 +010044 * <p>
45 * <code>
margaretha3af22a42023-04-21 15:31:58 +020046 * java -jar Kustvakt-full-0.69.3.jar --spring-config
margaretha1960ea52023-02-28 11:20:15 +010047 * test-config-icc.xml
48 * </code>
49 * </p>
Marc Kupietzd43a98d2023-09-22 17:11:46 +020050 *
margaretha1960ea52023-02-28 11:20:15 +010051 * <h1>Spring configuration file</h1>
52 * <p>
53 * For ICC, collectionRewrite in the Spring XML configuration must
54 * be disabled. This has been done in <em>test-config-icc.xml</em>.
55 * </p>
Marc Kupietzd43a98d2023-09-22 17:11:46 +020056 *
margaretha1960ea52023-02-28 11:20:15 +010057 * <p>For testing, the ICC configuration uses HTTP Basic
58 * Authentication and doesn't use LDAP.</p>
Marc Kupietzd43a98d2023-09-22 17:11:46 +020059 *
margaretha1960ea52023-02-28 11:20:15 +010060 * <p>For production, Basic Authentication must be
61 * disabled/commented.</p>
Marc Kupietzd43a98d2023-09-22 17:11:46 +020062 *
margaretha1960ea52023-02-28 11:20:15 +010063 * <pre><code>
64 * &lt;bean id="basic_auth"
margaretha3af22a42023-04-21 15:31:58 +020065 * class="de.ids_mannheim.korap.authentication.BasicAuthentication"/&gt;
Marc Kupietzd43a98d2023-09-22 17:11:46 +020066 *
margaretha1960ea52023-02-28 11:20:15 +010067 * &lt;util:list id="kustvakt_authproviders"
68 * value-type="de.ids_mannheim.korap.interfaces.AuthenticationIface"&gt;
69 * &lt;!-- &lt;ref bean="basic_auth" /&gt; --&gt;
70 * </code>
71 * </pre>
Marc Kupietzd43a98d2023-09-22 17:11:46 +020072 *
margaretha35e1ca22023-11-16 22:00:01 +010073 * <p>For production, the init-method of Initializator should be
74 * changed to init.</p>
Marc Kupietzd43a98d2023-09-22 17:11:46 +020075 *
margaretha3af22a42023-04-21 15:31:58 +020076 * <pre>
77 * <code>
margaretha35e1ca22023-11-16 22:00:01 +010078 * &lt;bean id="initializator"
79 * class="de.ids_mannheim.de.init.Initializator"
80 * init-method="init"&gt;&lt;/bean&gt;
margaretha3af22a42023-04-21 15:31:58 +020081 * </code>
82 * </pre>
Marc Kupietzd43a98d2023-09-22 17:11:46 +020083 *
margaretha1960ea52023-02-28 11:20:15 +010084 * <h1>Kustvakt configuration file</h1>
Marc Kupietzd43a98d2023-09-22 17:11:46 +020085 *
margaretha3af22a42023-04-21 15:31:58 +020086 * <p>
margaretha1960ea52023-02-28 11:20:15 +010087 * The configuration file: <em>kustvakt-icc.conf</em> includes the
88 * following setup:
margaretha3af22a42023-04-21 15:31:58 +020089 * </p>
Marc Kupietzd43a98d2023-09-22 17:11:46 +020090 *
margaretha1960ea52023-02-28 11:20:15 +010091 * <ul>
92 * <li>
93 * <em>krill.indexDir</em> should indicate the location of the index.
94 * It is set to the wiki-index for the test.
95 * </li>
Marc Kupietzd43a98d2023-09-22 17:11:46 +020096 *
margaretha1960ea52023-02-28 11:20:15 +010097 * <p>
98 * <code>krill.indexDir=../wiki-index</code>
99 * </p>
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200100 *
margaretha1960ea52023-02-28 11:20:15 +0100101 * <li>
102 * <em>availability.regex</em>
103 * properties should be removed or commented since the data doesn't
104 * contain availability and access to data is not determined by this
105 * field.
106 * </li>
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200107 *
margaretha1960ea52023-02-28 11:20:15 +0100108 * <li>
109 * Resource filter class names for the search and match info services
110 * should be defined by <em>search.resource.filters property</em>. For
111 * example, to restricts access with only authentication filter:</li>
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200112 *
margaretha1960ea52023-02-28 11:20:15 +0100113 * <p>
114 * <code>search.resource.filters=AuthenticationFilter </code>
115 * </p>
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200116 *
margaretha1960ea52023-02-28 11:20:15 +0100117 * <li><em>oauth2.password.authentication</em> indicating the
118 * authentication method to match usernames and password.
119 * <code>TEST</code> is a dummy authentication that doesn't do any
120 * matching. For production, it must be changed to
121 * <code>LDAP</code>.</li>
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200122 *
margaretha1960ea52023-02-28 11:20:15 +0100123 * <p><code>oauth2.password.authentication=LDAP</code></p>
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200124 *
margaretha1960ea52023-02-28 11:20:15 +0100125 * </ul>
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200126 *
margaretha1960ea52023-02-28 11:20:15 +0100127 * @author elma
128 * @see /src/main/resources/properties/jdbc.properties
129 */
130@ContextConfiguration("classpath:test-config-icc.xml")
131public class ICCTest extends SpringJerseyTest {
132
133 public final static String API_VERSION = "v1.0";
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200134
margaretha1960ea52023-02-28 11:20:15 +0100135 public String basicAuth;
136
margaretha35e1ca22023-11-16 22:00:01 +0100137 public ICCTest () throws KustvaktException {
138 basicAuth = HttpAuthorizationHandler
139 .createBasicAuthorizationHeaderValue("user", "password");
margaretha1960ea52023-02-28 11:20:15 +0100140 }
141
142 @Test
margaretha35e1ca22023-11-16 22:00:01 +0100143 public void searchWithoutLogin () throws KustvaktException {
144 Response r = target().path(API_VERSION).path("search")
145 .queryParam("q", "[orth=das]").queryParam("ql", "poliqarp")
146 .request().get();
margaretha1960ea52023-02-28 11:20:15 +0100147 assertEquals(Status.UNAUTHORIZED.getStatusCode(), r.getStatus());
margaretha1960ea52023-02-28 11:20:15 +0100148 String entity = r.readEntity(String.class);
149 JsonNode node = JsonUtils.readTree(entity);
margaretha35e1ca22023-11-16 22:00:01 +0100150 assertEquals(StatusCodes.AUTHORIZATION_FAILED,
151 node.at("/errors/0/0").asInt());
margaretha1960ea52023-02-28 11:20:15 +0100152 }
153
154 @Test
margaretha35e1ca22023-11-16 22:00:01 +0100155 public void searchWithLogin () throws KustvaktException {
156 Response r = target().path(API_VERSION).path("search")
157 .queryParam("q", "[orth=das]").queryParam("ql", "poliqarp")
158 .request().header(Attributes.AUTHORIZATION, basicAuth).get();
margaretha1960ea52023-02-28 11:20:15 +0100159 assertEquals(Status.OK.getStatusCode(), r.getStatus());
margaretha1960ea52023-02-28 11:20:15 +0100160 String entity = r.readEntity(String.class);
161 JsonNode node = JsonUtils.readTree(entity);
162 assertTrue(node.at("/matches").size() > 0);
163 }
164
165 @Test
margaretha35e1ca22023-11-16 22:00:01 +0100166 public void matchInfoWithoutLogin () throws KustvaktException {
167 Response response = target().path(API_VERSION).path("corpus")
168 .path("WDD17").path("982").path("72848").path("p2815-2816")
169 .queryParam("foundry", "*").request().get();
margaretha1960ea52023-02-28 11:20:15 +0100170 assertEquals(Status.UNAUTHORIZED.getStatusCode(), response.getStatus());
margaretha1960ea52023-02-28 11:20:15 +0100171 String entity = response.readEntity(String.class);
172 JsonNode node = JsonUtils.readTree(entity);
margaretha35e1ca22023-11-16 22:00:01 +0100173 assertEquals(StatusCodes.AUTHORIZATION_FAILED,
174 node.at("/errors/0/0").asInt());
margaretha1960ea52023-02-28 11:20:15 +0100175 }
176
177 @Test
margaretha35e1ca22023-11-16 22:00:01 +0100178 public void matchInfoWithLogin () throws KustvaktException {
179 Response response = target().path(API_VERSION).path("corpus")
180 .path("WDD17").path("982").path("72848").path("p2815-2816")
181 .queryParam("foundry", "*").request()
182 .header(Attributes.AUTHORIZATION, basicAuth).get();
margaretha1960ea52023-02-28 11:20:15 +0100183 assertEquals(Status.OK.getStatusCode(), response.getStatus());
margaretha1960ea52023-02-28 11:20:15 +0100184 String entity = response.readEntity(String.class);
185 JsonNode node = JsonUtils.readTree(entity);
margaretha1960ea52023-02-28 11:20:15 +0100186 assertTrue(node.at("/hasSnippet").asBoolean());
187 assertNotNull(node.at("/matchID").asText());
188 assertNotNull(node.at("/snippet").asText());
189 }
190}