Skip to content

Commit

Permalink
Merge pull request #81 from eparovyshnaya/558321-tests-2
Browse files Browse the repository at this point in the history
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
eparovyshnaya authored Dec 17, 2019
2 parents a0717a6 + 1bca884 commit afe86c9
Show file tree
Hide file tree
Showing 4 changed files with 183 additions and 0 deletions.
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());
}

}
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);
}

}
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;
}

}
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$
);
}

}

0 comments on commit afe86c9

Please sign in to comment.