Skip to content

Commit

Permalink
Merge pull request #80 from eparovyshnaya/558321-tests
Browse files Browse the repository at this point in the history
558321 design general purpose interfaces to empower reporting
- test ref implementation is provided to illustrate efforts on
implementing export to several output formats
- `ExportTest` 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 2c59524 + a404ed0 commit a0717a6
Show file tree
Hide file tree
Showing 14 changed files with 547 additions and 0 deletions.
1 change: 1 addition & 0 deletions releng/org.eclipse.passage.loc.aggregator/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
<module>../../products/org.eclipse.passage.loc.operator.product</module>

<module>../../bundles/org.eclipse.passage.loc.yars.api</module>
<module>../../tests/org.eclipse.passage.loc.yars.api.tests</module>

</modules>

Expand Down
11 changes: 11 additions & 0 deletions tests/org.eclipse.passage.loc.yars.api.tests/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Manifest-Version: 1.0
Automatic-Module-Name: org.eclipse.passage.loc.yars.api.tests
Bundle-ManifestVersion: 2
Bundle-SymbolicName: org.eclipse.passage.loc.yars.api.tests
Bundle-Version: 0.1.0.qualifier
Bundle-Name: %Bundle-Name
Bundle-Vendor: %Bundle-Vendor
Bundle-Copyright: %Bundle-Copyright
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Require-Bundle: org.junit;bundle-version="4.12.0",
org.eclipse.passage.loc.yars.api;bundle-version="0.1.0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#Properties file for org.eclipse.passage.loc.yars.api.tests
###############################################################################
# Copyright (c) 2019 ArSysOp and others
#
# 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
###############################################################################

Bundle-Name = Passage LOC YARS API Tests
Bundle-Vendor = Eclipse Passage
Bundle-Copyright = Copyright (c) 2019 ArSysOp and others.\n\
\n\
This program and the accompanying materials are made\n\
available under the terms of the Eclipse Public License 2.0\n\
which is available at https://www.eclipse.org/legal/epl-2.0/\n\
\n\
SPDX-License-Identifier: EPL-2.0\n\
18 changes: 18 additions & 0 deletions tests/org.eclipse.passage.loc.yars.api.tests/build.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
###############################################################################
# Copyright (c) 2019 ArSysOp and others
#
# 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
###############################################################################

source.. = src/
output.. = target/classes/
bin.includes = META-INF/,\
.,\
OSGI-INF/
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.export;

import org.eclipse.passage.loc.yars.internal.api.FetchParams;
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;

@SuppressWarnings("restriction")
public class All implements Query<InMemoryStorage, ExportedEntry, FetchParams> {

@Override
public String id() {
return "ALL"; //$NON-NLS-1$
}

@Override
public String description() {
return "Fetch all entries from an im-memory storage in one fell swoop"; //$NON-NLS-1$
}

@Override
public FetchedData<InMemoryStorage, ExportedEntry> data(InMemoryStorage base, FetchParams params) {
return new Fetch(base);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*******************************************************************************
* 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.export;

import java.util.Arrays;
import java.util.List;

import org.eclipse.passage.loc.yars.internal.api.ListMedia;

@SuppressWarnings("restriction")
class Csv implements ListMedia<ExportedEntry, String> {

private final StringBuilder builder;
private final List<String> header;

private Csv(StringBuilder builder, List<String> header) {
this.builder = builder;
this.header = header;
}

Csv(String... header) {
this(new StringBuilder(), Arrays.asList(header));
}

@Override
public Csv start() {
builder.delete(0, builder.length());
header.forEach(facet -> builder.append(facet).append(";")); //$NON-NLS-1$
return this;
}

@Override
public Csv startNode(ExportedEntry node) {
builder.append("\n"); //$NON-NLS-1$
return this;
}

@Override
public Csv inner(String data, String name) {
builder.append(data).append(";"); //$NON-NLS-1$
return this;
}

@Override
public String content() {
return builder.toString();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*******************************************************************************
* 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.export;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.passage.loc.yars.internal.api.ListMedia;

@SuppressWarnings("restriction")
public class Enlistment<T> implements ListMedia<T, List<T>> {

private final List<T> content = new ArrayList<>();

@Override
public Enlistment<T> start() {
content.clear();
return this;
}

@Override
public Enlistment<T> startNode(T root) {
content.add(root);
return this;
}

@Override
public List<T> content() {
return content;
}

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

import org.eclipse.passage.loc.yars.internal.api.ExportData;
import org.eclipse.passage.loc.yars.internal.api.FetchedData;
import org.eclipse.passage.loc.yars.internal.api.ListMedia;
import org.eclipse.passage.loc.yars.internal.api.model.InMemoryStorage;

@SuppressWarnings("restriction")
class Export implements ExportData<ExportedEntry> {

private final FetchedData<InMemoryStorage, ExportedEntry> query;

Export(FetchedData<InMemoryStorage, ExportedEntry> query) {
this.query = query;
}

@Override
public <C> void write(ListMedia<ExportedEntry, C> media) {
media.start();
query.get().forEach(d -> d.write(media));
media.finish();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*******************************************************************************
* 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.export;

import static org.junit.Assert.assertEquals;

import java.util.Arrays;

import org.eclipse.passage.loc.yars.internal.api.FetchParams;
import org.eclipse.passage.loc.yars.internal.api.ListMedia;
import org.eclipse.passage.loc.yars.internal.api.Storage;
import org.eclipse.passage.loc.yars.internal.api.model.InMemoryStorage;
import org.eclipse.passage.loc.yars.internal.api.model.StoredEntry;
import org.junit.Test;

/**
* <p>
* Let's say, you have a domain class {@linkplain StoredEntry} and a
* {@linkplain Storage} - some coverer, that can somehow provide access to all
* existing instances.
* </p>
* <p>
* You desire to
* </p>
* <ul>
* <li>have a look at all entries</li>
* <li>get some information from each</li>
* <li>and {@code write} this information to some output format.</li>
* </ul>
*
* <p>
* Here what you should do to make this happen
* </p>
* <ul>
* <li>implement a {@linkplain Query}</li>
* <li>Form a new class to keep information of interest(here it is called
* {@linkplain ExportedEntry}</li>
* <li></li>
* <li></li>
* </ul>
*
*/
@SuppressWarnings("restriction")
public class ExportTest {

@Test
public void testCsv() {
assertEquals("name;\nGammy;\nQuami;\nTsunami;", //$NON-NLS-1$
queryResult(new Csv("name"))); //$NON-NLS-1$
}

@Test
public void testEnlistment() {
assertEquals(Arrays.asList( //
new ExportedEntry("Gammy"), //$NON-NLS-1$
new ExportedEntry("Quami"), //$NON-NLS-1$
new ExportedEntry("Tsunami")), //$NON-NLS-1$
queryResult(new Enlistment<>()));
}

@Test
public void testJson() {
assertEquals("{\n" + //$NON-NLS-1$
"\t\"node\" : {\n" + //$NON-NLS-1$
"\t\t\"name\" : \"Gammy\"\n" + //$NON-NLS-1$
"\t}\n" + //$NON-NLS-1$
"\t\"node\" : {\n" + //$NON-NLS-1$
"\t\t\"name\" : \"Quami\"\n" + //$NON-NLS-1$
"\t}\n" + //$NON-NLS-1$
"\t\"node\" : {\n" + //$NON-NLS-1$
"\t\t\"name\" : \"Tsunami\"\n" + //$NON-NLS-1$
"\t}\n" + //$NON-NLS-1$
"}\n", //$NON-NLS-1$
queryResult(new Json()));
}

private <T> T queryResult(ListMedia<ExportedEntry, T> media) {
new Export(new All().data(//
new InMemoryStorage( //
new StoredEntry("Gammy", "US"), //$NON-NLS-1$ //$NON-NLS-2$
new StoredEntry("Quami", "France"), //$NON-NLS-1$ //$NON-NLS-2$
new StoredEntry("Tsunami", "Japan")//$NON-NLS-1$ //$NON-NLS-2$
), //
new FetchParams.Empty()))//
.write(media);
return media.content();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*******************************************************************************
* 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.export;

import java.util.Objects;

import org.eclipse.passage.loc.yars.internal.api.ListMedia;

@SuppressWarnings("restriction")
public class ExportedEntry {

private String name;

public ExportedEntry(String name) {
this.name = name;
}

public <T> void write(ListMedia<ExportedEntry, T> media) {
media.startNode(this)//
.inner(name, "name") //$NON-NLS-1$
.finishNode(this);
}

@Override // generated
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
ExportedEntry exportedEntry = (ExportedEntry) o;
return Objects.equals(name, exportedEntry.name);
}

@Override // generated
public int hashCode() {
return name != null ? name.hashCode() : 0;
}

}
Loading

0 comments on commit a0717a6

Please sign in to comment.