Skip to content

Commit

Permalink
Adding paging settings in OpenSearch for EO admin page
Browse files Browse the repository at this point in the history
  • Loading branch information
aaime committed Mar 9, 2017
1 parent b4dc2d4 commit be00e9d
Show file tree
Hide file tree
Showing 13 changed files with 217 additions and 29 deletions.
Expand Up @@ -7,6 +7,11 @@
import org.geoserver.config.ServiceInfo;
import org.geotools.util.Version;

/**
* OpenSearch for EO service descriptor
*
* @author Andrea Aime - GeoSolutions
*/
public interface OSEOInfo extends ServiceInfo {

public static int DEFAULT_MAXIMUM_RECORDS = 100;
Expand All @@ -31,14 +36,14 @@ public interface OSEOInfo extends ServiceInfo {
*
* @return
*/
int getMaximumRecords();
int getMaximumRecordsPerPage();

/**
* Sets the maximum amount of records returned in a search
*
* @param maximumRecords
*/
void setMaximumRecords(int maximumRecords);
void setMaximumRecordsPerPage(int maximumRecords);

/**
* Returns the default records per page when no "count" parameter is provided
Expand Down
Expand Up @@ -24,11 +24,11 @@ public void setRecordsPerPage(int defaultRecords) {
this.recordsPerPage = defaultRecords;
}

public int getMaximumRecords() {
public int getMaximumRecordsPerPage() {
return maximumRecords;
}

public void setMaximumRecords(int maximumRecords) {
public void setMaximumRecordsPerPage(int maximumRecords) {
this.maximumRecords = maximumRecords;
}

Expand Down
Expand Up @@ -36,7 +36,7 @@ protected OSEOInfo createServiceFromScratch(GeoServer gs) {
oseo.setName("OSEO");
oseo.setAbstract("Provides interoperable access, following ISO/OGC interface guidelines, to Earth Observation metadata.");
oseo.setTitle("OpenSearch for Earth Observation");
oseo.setMaximumRecords(OSEOInfo.DEFAULT_MAXIMUM_RECORDS);
oseo.setMaximumRecordsPerPage(OSEOInfo.DEFAULT_MAXIMUM_RECORDS);
oseo.setRecordsPerPage(OSEOInfo.DEFAULT_RECORDS_PER_PAGE);
final List<KeywordInfo> keywords = oseo.getKeywords();
keywords.add(new Keyword("EarthObservation"));
Expand Down
Expand Up @@ -92,8 +92,8 @@ public static List<Parameter<?>> getBasicOpensearch(OSEOInfo info) {

ParameterBuilder count = new ParameterBuilder("count", Integer.class).prefix(OS_PREFIX);
count.minimumInclusive(0);
if (info.getMaximumRecords() > 0) {
count.maximumInclusive(info.getMaximumRecords());
if (info.getMaximumRecordsPerPage() > 0) {
count.maximumInclusive(info.getMaximumRecordsPerPage());
}
result.add(count.build());

Expand Down
Expand Up @@ -122,7 +122,7 @@ private int getConfiguredMaxFeatures() {
if (info == null) {
return OSEOInfo.DEFAULT_MAXIMUM_RECORDS;
} else {
return info.getMaximumRecords();
return info.getMaximumRecordsPerPage();
}
}

Expand Down
Expand Up @@ -11,6 +11,7 @@

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.nio.file.Files;
import java.nio.file.Paths;
Expand All @@ -24,6 +25,8 @@
import java.util.stream.Collectors;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.geoserver.opensearch.eo.store.JDBCOpenSearchAccess;
import org.geoserver.opensearch.eo.store.OpenSearchAccess;
import org.geotools.data.DataAccessFinder;
import org.geotools.data.DataStoreFinder;
Expand Down Expand Up @@ -79,30 +82,33 @@ public static void tearDownStore() {

private static List<String> loadScriptCommands(String scriptLocation) throws IOException {
// grab all non comment, non empty lines
List<String> lines = Files.lines(Paths.get(scriptLocation)).map(l -> l.trim())
.filter(l -> !l.startsWith("--") && !l.isEmpty()).collect(Collectors.toList());
// regroup them into statements
List<String> statements = new ArrayList<String>();
String buffer = null;
for (String line : lines) {
if (buffer == null) {
buffer = line;
} else {
buffer = buffer + "\n" + line;
}
if (line.contains(";")) {
statements.add(buffer);
buffer = null;
try(InputStream is = JDBCOpenSearchAccess.class.getResourceAsStream(scriptLocation)) {
List<String> lines = IOUtils.readLines(is).stream().map(l -> l.trim())
.filter(l -> !l.startsWith("--") && !l.isEmpty()).collect(Collectors.toList());
// regroup them into statements
List<String> statements = new ArrayList<String>();
String buffer = null;
for (String line : lines) {
if (buffer == null) {
buffer = line;
} else {
buffer = buffer + "\n" + line;
}
if (line.contains(";")) {
statements.add(buffer);
buffer = null;
}
}
return statements;

}
return statements;
}

/**
* Takes the postgis.sql creation script, adapts it and runs it on H2
*/
static void createTables(JDBCDataStore h2) throws SQLException, IOException {
List<String> statements = loadScriptCommands("src/test/resources/postgis.sql");
List<String> statements = loadScriptCommands("/postgis.sql");
try (Connection conn = h2.getConnection(Transaction.AUTO_COMMIT);
Statement st = conn.createStatement();) {
for (String statement : statements) {
Expand All @@ -129,7 +135,7 @@ static void createTables(JDBCDataStore h2) throws SQLException, IOException {
* Takes the postgis.sql creation script, adapts it and runs it on H2
*/
static void populateCollections(JDBCDataStore h2) throws SQLException, IOException {
List<String> statements = loadScriptCommands("src/test/resources/collection_h2_data.sql");
List<String> statements = loadScriptCommands("/collection_h2_data.sql");
try (Connection conn = h2.getConnection(Transaction.AUTO_COMMIT);
Statement st = conn.createStatement();) {
for (String statement : statements) {
Expand Down
Expand Up @@ -6,6 +6,7 @@

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Map;

import javax.xml.XMLConstants;
Expand Down Expand Up @@ -64,8 +65,19 @@ protected void setUpTestData(SystemTestData testData) throws Exception {
protected void onSetUp(SystemTestData testData) throws Exception {
super.onSetUp(testData);

setupBasicOpenSearch(testData, getCatalog(), getGeoServer());
}

/**
* Sets up a H2 based OpenSearchAccess and configures OpenSearch for EO to use it
* @param testData
* @param cat
* @param gs
* @throws IOException
* @throws SQLException
*/
public static void setupBasicOpenSearch(SystemTestData testData, Catalog cat, GeoServer gs) throws IOException, SQLException {
// create the plain database
Catalog cat = getCatalog();
DataStoreInfo jdbcDs = cat.getFactory().createDataStore();
jdbcDs.setName("oseo_jdbc");
WorkspaceInfo ws = cat.getDefaultWorkspace();
Expand Down Expand Up @@ -99,7 +111,6 @@ protected void onSetUp(SystemTestData testData) throws Exception {
cat.add(osDs);

// configure opensearch for EO to use it
final GeoServer gs = getGeoServer();
OSEOInfo service = gs.getService(OSEOInfo.class);
service.setOpenSearchAccessStoreId(osDs.getId());
gs.save(service);
Expand Down
13 changes: 13 additions & 0 deletions src/community/oseo/web-oseo/pom.xml
Expand Up @@ -48,6 +48,13 @@
<artifactId>easymockclassextension</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.geoserver.community</groupId>
<artifactId>gs-oseo-core</artifactId>
<classifier>tests</classifier>
<scope>test</scope>
<version>${gs.version}</version>
</dependency>
<dependency>
<groupId>org.geoserver</groupId>
<artifactId>gs-main</artifactId>
Expand All @@ -72,5 +79,11 @@
<artifactId>hamcrest-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.geotools.jdbc</groupId>
<artifactId>gt-jdbc-h2</artifactId>
<version>${gt.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Expand Up @@ -17,6 +17,24 @@
</li>
</ul>
</fieldset>
<fieldset>
<legend>
<span>
<wicket:message key="paging">paging</wicket:message>
</span>
</legend>
<ul>
<li>
<label><wicket:message key="recordsPerPage">recordsPerPage</wicket:message></label>
<input type="text" wicket:id="recordsPerPage" class="field text"/>
</li>
<li>
<label><wicket:message key="maximumRecordsPerPage">maxRecordsPerPage</wicket:message></label>
<input type="text" wicket:id="maximumRecordsPerPage" class="field text"/>
</li>
</ul>
</fieldset>

</wicket:extend>
</body>
</html>
Expand Down
Expand Up @@ -6,13 +6,18 @@

import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.FormComponent;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.form.validation.AbstractFormValidator;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.validation.validator.RangeValidator;
import org.geoserver.catalog.DataStoreInfo;
import org.geoserver.opensearch.eo.OSEOInfo;
import org.geoserver.web.data.store.StoreListChoiceRenderer;
import org.geoserver.web.services.BaseServiceAdminPage;
import org.geoserver.web.wicket.ParamResourceModel;

public class OSEOAdminPage extends BaseServiceAdminPage<OSEOInfo> {

Expand All @@ -36,7 +41,7 @@ protected Class<OSEOInfo> getServiceClass() {
return OSEOInfo.class;
}

@SuppressWarnings({ "rawtypes", "unchecked" })
@SuppressWarnings({ "rawtypes", "unchecked", "serial" })
protected void build(final IModel info, Form form) {
OSEOInfo model = (OSEOInfo) info.getObject();

Expand All @@ -48,6 +53,32 @@ protected void build(final IModel info, Form form) {
"openSearchAccessId", new PropertyModel<DataStoreInfo>(this, "backend"),
new OpenSearchAccessListModel(), new StoreListChoiceRenderer());
form.add(openSearchAccessReference);

final TextField<Integer> recordsPerPage = new TextField<>("recordsPerPage", Integer.class);
recordsPerPage.add(RangeValidator.minimum(0));
recordsPerPage.setRequired(true);
form.add(recordsPerPage);
final TextField<Integer> maximumRecordsPerPage = new TextField<>("maximumRecordsPerPage", Integer.class);
maximumRecordsPerPage.add(RangeValidator.minimum(0));
maximumRecordsPerPage.setRequired(true);
form.add(maximumRecordsPerPage);
// check that records is lower or equal than maximum
form.add(new AbstractFormValidator() {

@Override
public void validate(Form<?> form) {
Integer records = recordsPerPage.getConvertedInput();
Integer maximum = maximumRecordsPerPage.getConvertedInput();
if(recordsPerPage != null && maximum != null && records > maximum) {
form.error(new ParamResourceModel("recordsGreaterThanMaximum", form, records, maximum));
}
}

@Override
public FormComponent<?>[] getDependentFormComponents() {
return new FormComponent<?>[] {recordsPerPage, maximumRecordsPerPage};
}
});
}

protected String getServiceName() {
Expand Down
Expand Up @@ -4,4 +4,8 @@ oseo.title = OS-EO
OSEOAdminPage.title = OpenSearch for Earth Observation
OSEOAdminPage.description = Manage publishing earth observation metadata
OSEOAdminPage.metadataSource = OpenSearch store
OSEOAdminPage.openSearchAccessId = OpenSearch metadata source
OSEOAdminPage.openSearchAccessId = OpenSearch metadata source
OSEOAdminPage.paging = Paging
OSEOAdminPage.recordsPerPage = Default records per page
OSEOAdminPage.maximumRecordsPerPage = Maximum records per page
OSEOAdminPage.recordsGreaterThanMaximum = Records per page {0} should be smaller or equal to maximum records per page {1}
@@ -0,0 +1,65 @@
/* (c) 2017 Open Source Geospatial Foundation - all rights reserved
* This code is licensed under the GPL 2.0 license, available at the root
* application directory.
*/
package org.geoserver.opensearch.eo.web;

import static org.junit.Assert.*;

import org.apache.wicket.feedback.FeedbackMessage;
import org.apache.wicket.util.tester.FormTester;
import org.geoserver.opensearch.eo.OSEOInfo;
import org.junit.Before;
import org.junit.Test;

public class OSEOAdminPageTest extends OSEOWebTestSupport {


@Before
public void startPage() {
login();
tester.startPage(OSEOAdminPage.class);
}

@Test
public void smokeTest() throws Exception {
tester.assertNoErrorMessage();
tester.assertModelValue("form:maximumRecordsPerPage", OSEOInfo.DEFAULT_MAXIMUM_RECORDS);
tester.assertModelValue("form:recordsPerPage", OSEOInfo.DEFAULT_RECORDS_PER_PAGE);
// print(tester.getLastRenderedPage(), true, true);
}

@Test
public void verifyRequiredFields() throws Exception {
checkRequired("maximumRecordsPerPage");
checkRequired("recordsPerPage");
}

@Test
public void testPagingValidation() throws Exception {
setupPagingValues(-10, 100);
assertEquals(1, tester.getMessages(FeedbackMessage.ERROR).size());
setupPagingValues(100, -10);
assertEquals(1, tester.getMessages(FeedbackMessage.ERROR).size());
setupPagingValues(100, 10);
assertEquals(1, tester.getMessages(FeedbackMessage.ERROR).size());
setupPagingValues(2, 1000);
tester.assertNoErrorMessage();
}

private void setupPagingValues(int records, int maxRecords) {
tester.startPage(OSEOAdminPage.class);
FormTester formTester = tester.newFormTester("form");
formTester.setValue("recordsPerPage", "" + records);
formTester.setValue("maximumRecordsPerPage", "" + maxRecords);
formTester.submit();
}

private void checkRequired(String componentName) {
tester.startPage(OSEOAdminPage.class);
FormTester formTester = tester.newFormTester("form");
formTester.setValue(componentName, null);
formTester.submit();
assertEquals(1, tester.getMessages(FeedbackMessage.ERROR).size());
}
}

0 comments on commit be00e9d

Please sign in to comment.