From 1bca884f20961bb332bb6ca4fb634c944fc4579c Mon Sep 17 00:00:00 2001 From: "elena.parovyshnaya" Date: Tue, 17 Dec 2019 11:43:27 +0300 Subject: [PATCH] 558321 design general purpose interfaces to empower reporting - reference implementation is provided for paginated fetching - `PaginationTest` is going to be exhaustively documented later in a separate commit together with the api package Signed-off-by: elena.parovyshnaya --- .../yars/internal/api/pagination/Fetch.java | 45 +++++++++++++ .../yars/internal/api/pagination/Page.java | 38 +++++++++++ .../api/pagination/PaginationSettings.java | 36 +++++++++++ .../api/pagination/PaginationTest.java | 64 +++++++++++++++++++ 4 files changed, 183 insertions(+) create mode 100644 tests/org.eclipse.passage.loc.yars.api.tests/src/org/eclipse/passage/loc/yars/internal/api/pagination/Fetch.java create mode 100644 tests/org.eclipse.passage.loc.yars.api.tests/src/org/eclipse/passage/loc/yars/internal/api/pagination/Page.java create mode 100644 tests/org.eclipse.passage.loc.yars.api.tests/src/org/eclipse/passage/loc/yars/internal/api/pagination/PaginationSettings.java create mode 100644 tests/org.eclipse.passage.loc.yars.api.tests/src/org/eclipse/passage/loc/yars/internal/api/pagination/PaginationTest.java diff --git a/tests/org.eclipse.passage.loc.yars.api.tests/src/org/eclipse/passage/loc/yars/internal/api/pagination/Fetch.java b/tests/org.eclipse.passage.loc.yars.api.tests/src/org/eclipse/passage/loc/yars/internal/api/pagination/Fetch.java new file mode 100644 index 000000000..43de21bc9 --- /dev/null +++ b/tests/org.eclipse.passage.loc.yars.api.tests/src/org/eclipse/passage/loc/yars/internal/api/pagination/Fetch.java @@ -0,0 +1,45 @@ +/******************************************************************************* + * Copyright (c) 2019 ArSysOp + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * ArSysOp - initial API and implementation + *******************************************************************************/ +package org.eclipse.passage.loc.yars.internal.api.pagination; + +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +import org.eclipse.passage.loc.yars.internal.api.FetchedData; +import org.eclipse.passage.loc.yars.internal.api.model.InMemoryStorage; +import org.eclipse.passage.loc.yars.internal.api.model.StoredEntry; + +@SuppressWarnings("restriction") + +class Fetch implements FetchedData { + + private final PaginationSettings settings; + private final InMemoryStorage storage; + + Fetch(PaginationSettings settings, InMemoryStorage storage) { + this.settings = settings; + this.storage = storage; + } + + @Override + public List get() { + List all = storage.entries(); + return IntStream.range(settings.from(), settings.to()) // + .filter(i -> i >= 0) // + .filter(i -> i < all.size()) // + .mapToObj(all::get) // + .collect(Collectors.toList()); + } + +} diff --git a/tests/org.eclipse.passage.loc.yars.api.tests/src/org/eclipse/passage/loc/yars/internal/api/pagination/Page.java b/tests/org.eclipse.passage.loc.yars.api.tests/src/org/eclipse/passage/loc/yars/internal/api/pagination/Page.java new file mode 100644 index 000000000..5859ddec7 --- /dev/null +++ b/tests/org.eclipse.passage.loc.yars.api.tests/src/org/eclipse/passage/loc/yars/internal/api/pagination/Page.java @@ -0,0 +1,38 @@ +/******************************************************************************* + * Copyright (c) 2019 ArSysOp + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * ArSysOp - initial API and implementation + *******************************************************************************/ +package org.eclipse.passage.loc.yars.internal.api.pagination; + +import org.eclipse.passage.loc.yars.internal.api.FetchedData; +import org.eclipse.passage.loc.yars.internal.api.Query; +import org.eclipse.passage.loc.yars.internal.api.model.InMemoryStorage; +import org.eclipse.passage.loc.yars.internal.api.model.StoredEntry; + +@SuppressWarnings("restriction") +public class Page implements Query { + + @Override + public String id() { + return "PAGE"; //$NON-NLS-1$ + } + + @Override + public String description() { + return "Fetch paginated original entries from in-memory storage"; //$NON-NLS-1$ + } + + @Override + public FetchedData data(InMemoryStorage storage, PaginationSettings params) { + return new Fetch(params, storage); + } + +} diff --git a/tests/org.eclipse.passage.loc.yars.api.tests/src/org/eclipse/passage/loc/yars/internal/api/pagination/PaginationSettings.java b/tests/org.eclipse.passage.loc.yars.api.tests/src/org/eclipse/passage/loc/yars/internal/api/pagination/PaginationSettings.java new file mode 100644 index 000000000..bd9eb380d --- /dev/null +++ b/tests/org.eclipse.passage.loc.yars.api.tests/src/org/eclipse/passage/loc/yars/internal/api/pagination/PaginationSettings.java @@ -0,0 +1,36 @@ +/******************************************************************************* + * Copyright (c) 2019 ArSysOp + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * ArSysOp - initial API and implementation + *******************************************************************************/ +package org.eclipse.passage.loc.yars.internal.api.pagination; + +import org.eclipse.passage.loc.yars.internal.api.FetchParams; + +@SuppressWarnings("restriction") +class PaginationSettings implements FetchParams { + + private final int no; + private final int length; + + PaginationSettings(int no, int length) { + this.no = no; + this.length = length; + } + + int from() { + return no * length; + } + + int to() { + return (no + 1) * length; + } + +} diff --git a/tests/org.eclipse.passage.loc.yars.api.tests/src/org/eclipse/passage/loc/yars/internal/api/pagination/PaginationTest.java b/tests/org.eclipse.passage.loc.yars.api.tests/src/org/eclipse/passage/loc/yars/internal/api/pagination/PaginationTest.java new file mode 100644 index 000000000..0b8a03c7c --- /dev/null +++ b/tests/org.eclipse.passage.loc.yars.api.tests/src/org/eclipse/passage/loc/yars/internal/api/pagination/PaginationTest.java @@ -0,0 +1,64 @@ +/******************************************************************************* + * Copyright (c) 2019 ArSysOp + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * ArSysOp - initial API and implementation + *******************************************************************************/ +package org.eclipse.passage.loc.yars.internal.api.pagination; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.util.List; + +import org.eclipse.passage.loc.yars.internal.api.model.InMemoryStorage; +import org.eclipse.passage.loc.yars.internal.api.model.StoredEntry; +import org.junit.Test; + +public class PaginationTest { + + @Test + public void testFirstPage() { + List result = pageContent(0); + assertEquals(3, result.size()); + assertTrue(result.stream().allMatch(e -> e.origin().equals("page 1"))); //$NON-NLS-1$ + } + + @Test + public void testMidPage() { + List result = pageContent(1); + assertEquals(3, result.size()); + assertTrue(result.stream().allMatch(e -> e.origin().equals("page 2"))); //$NON-NLS-1$ + } + + @Test + public void testNotFullPage() { + List result = pageContent(2); + assertEquals(1, result.size()); + assertEquals("page 3", result.get(0).origin()); //$NON-NLS-1$ + } + + @SuppressWarnings("restriction") + private List pageContent(int no) { + return new Page().data(base(), new PaginationSettings(no, 3)).get(); + } + + private InMemoryStorage base() { + return new InMemoryStorage( // + new StoredEntry("1.0", "page 1"), //$NON-NLS-1$ //$NON-NLS-2$ + new StoredEntry("1.1", "page 1"), //$NON-NLS-1$ //$NON-NLS-2$ + new StoredEntry("1.2", "page 1"), //$NON-NLS-1$ //$NON-NLS-2$ + new StoredEntry("2.0", "page 2"), //$NON-NLS-1$ //$NON-NLS-2$ + new StoredEntry("2.1", "page 2"), //$NON-NLS-1$ //$NON-NLS-2$ + new StoredEntry("2.2", "page 2"), //$NON-NLS-1$ //$NON-NLS-2$ + new StoredEntry("3.1", "page 3")//$NON-NLS-1$ //$NON-NLS-2$ + ); + } + +}