-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #81 from eparovyshnaya/558321-tests-2
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 <[email protected]>
- Loading branch information
Showing
4 changed files
with
183 additions
and
0 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
...ge.loc.yars.api.tests/src/org/eclipse/passage/loc/yars/internal/api/pagination/Fetch.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<InMemoryStorage, StoredEntry> { | ||
|
||
private final PaginationSettings settings; | ||
private final InMemoryStorage storage; | ||
|
||
Fetch(PaginationSettings settings, InMemoryStorage storage) { | ||
this.settings = settings; | ||
this.storage = storage; | ||
} | ||
|
||
@Override | ||
public List<StoredEntry> get() { | ||
List<StoredEntry> 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()); | ||
} | ||
|
||
} |
38 changes: 38 additions & 0 deletions
38
...age.loc.yars.api.tests/src/org/eclipse/passage/loc/yars/internal/api/pagination/Page.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<InMemoryStorage, StoredEntry, PaginationSettings> { | ||
|
||
@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<InMemoryStorage, StoredEntry> data(InMemoryStorage storage, PaginationSettings params) { | ||
return new Fetch(params, storage); | ||
} | ||
|
||
} |
36 changes: 36 additions & 0 deletions
36
...pi.tests/src/org/eclipse/passage/loc/yars/internal/api/pagination/PaginationSettings.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
} |
64 changes: 64 additions & 0 deletions
64
...rs.api.tests/src/org/eclipse/passage/loc/yars/internal/api/pagination/PaginationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<StoredEntry> 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<StoredEntry> 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<StoredEntry> result = pageContent(2); | ||
assertEquals(1, result.size()); | ||
assertEquals("page 3", result.get(0).origin()); //$NON-NLS-1$ | ||
} | ||
|
||
@SuppressWarnings("restriction") | ||
private List<StoredEntry> 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$ | ||
); | ||
} | ||
|
||
} |