Skip to content

Commit

Permalink
Merge pull request #291 from eclipse-passage/564986
Browse files Browse the repository at this point in the history
564986 API revision | conditions | transport | xml
  • Loading branch information
eparovyshnaya authored Jul 7, 2020
2 parents a873827 + 671e46f commit 9c72909
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Collection;

import org.eclipse.passage.lic.internal.api.conditions.Condition;
import org.eclipse.passage.lic.internal.api.registry.Service;

/**
* Persistence interface for {@link Condition}(s).
* Encapsulates knowledge how to retrieve {@link Condition}(s) from an input
* source of the configured {@linkplain ContentType}.
*/
public interface ConditionTransport extends Service<ContentType> {

Expand All @@ -31,10 +31,4 @@ public interface ConditionTransport extends Service<ContentType> {
*/
Collection<Condition> read(InputStream input) throws IOException;

/**
* Writes {@link Condition}(s) to the given {@link OutputStream}. Stream remains
* open.
*/
void write(Collection<Condition> conditions, OutputStream output) throws IOException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
* Intentionally ugly.
*/
@SuppressWarnings("restriction")
final class ConditionPack {
public final class ConditionPack {

Collection<Condition> conditions;

ConditionPack(Collection<Condition> conditions) {
public ConditionPack(Collection<Condition> conditions) {
this.conditions = conditions;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Collection;
import java.util.Objects;

import org.eclipse.passage.lic.internal.api.conditions.Condition;
import org.eclipse.passage.lic.internal.api.conditions.mining.ConditionTransport;
Expand All @@ -37,12 +35,4 @@ public Collection<Condition> read(InputStream input) throws IOException {
return new JsonObjectMapper().get().readValue(input, ConditionPack.class).conditions;
}

@SuppressWarnings("resource")
@Override
public void write(Collection<Condition> conditions, OutputStream output) throws IOException {
Objects.requireNonNull(conditions, "JsonConditionTransport::write::conditions"); //$NON-NLS-1$
Objects.requireNonNull(output, "JsonConditionTransport::write::output"); //$NON-NLS-1$
output.write(new JsonObjectMapper().get().writeValueAsBytes(new ConditionPack(conditions)));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import com.fasterxml.jackson.databind.module.SimpleModule;

@SuppressWarnings("restriction")
class JsonObjectMapper implements Supplier<ObjectMapper> {
public final class JsonObjectMapper implements Supplier<ObjectMapper> {

@Override
public ObjectMapper get() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,13 @@ public Iterable<LicensingCondition> readConditions(InputStream input) throws IOE
return extracted;
}

/**
* FIXME: beware: on reading we expect LicensePackDescriptor to be a root for
* conditions but on reading we put conditions directly into contents. Will not
* work both sides. Totally deprecated.
*/
@Override
public void writeConditions(Iterable<LicensingCondition> conditions, OutputStream output)
throws IOException {
public void writeConditions(Iterable<LicensingCondition> conditions, OutputStream output) throws IOException {
Resource resource = new XMIResourceImpl();
EList<EObject> contents = resource.getContents();
for (LicensingCondition conditionDescriptor : conditions) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,15 @@

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Collection;
import java.util.HashMap;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.xmi.impl.XMIResourceImpl;
import org.eclipse.passage.lic.api.conditions.LicensingCondition;
import org.eclipse.passage.lic.base.conditions.LicensingConditions;
import org.eclipse.passage.lic.internal.api.conditions.Condition;
import org.eclipse.passage.lic.internal.api.conditions.EvaluationType;
import org.eclipse.passage.lic.internal.api.conditions.ValidityPeriodClosed;
import org.eclipse.passage.lic.internal.api.conditions.mining.ConditionTransport;
import org.eclipse.passage.lic.internal.api.conditions.mining.ContentType;
import org.eclipse.passage.lic.internal.base.conditions.BaseCondition;
Expand Down Expand Up @@ -75,32 +69,4 @@ private Condition condition(LicenseGrantDescriptor descriptor) {
descriptor.getConditionExpression()));
}

@Override
public void write(Collection<Condition> conditions, OutputStream output) throws IOException {
Resource resource = new XMIResourceImpl();
EList<EObject> contents = resource.getContents();
for (Condition condition : conditions) {
LicensingCondition conditionDescriptor = condition(condition);
if (conditionDescriptor instanceof EObject) {
EObject eObject = (EObject) conditionDescriptor;
contents.add(eObject);
}
}
resource.save(output, new HashMap<>());
}

// FIXME: severe error here: none of'em are EObject! Find a way to construct
// true-emf LicenseGrantDescriptors
private LicensingCondition condition(Condition condition) {
LicensingCondition lic = LicensingConditions.create(//
condition.feature(), //
condition.versionMatch().version(), //
condition.versionMatch().rule().identifier(), //
((ValidityPeriodClosed) condition.validityPeriod()).from(), //
((ValidityPeriodClosed) condition.validityPeriod()).to(), //
condition.evaluationInstructions().type().identifier(), //
condition.evaluationInstructions().expression());
throw new UnsupportedOperationException();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
import static org.junit.Assume.assumeTrue;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;

import org.eclipse.passage.lic.internal.api.conditions.Condition;
import org.eclipse.passage.lic.internal.api.conditions.ValidityPeriodClosed;
import org.eclipse.passage.lic.internal.api.conditions.mining.ConditionTransport;
import org.junit.Test;

Expand All @@ -36,45 +36,45 @@ public final void identifiedByContentType() {
assertNotNull(transport().id());
}

@Test(expected = NullPointerException.class)
public final void conditionsAreMandatory() throws IOException {
transport().write(null, new ByteArrayOutputStream());
}

@Test
public final void readYourOwnWritings() throws IOException {
// given
Collection<Condition> sedentaries = conditions();
assumeTrue("Too few: supply at least two test conditions for transportation", sedentaries.size() > 1); //$NON-NLS-1$
ConditionTransport transport = transport();
// when
String serialized = write(transport, sedentaries);
Collection<Condition> nomads = read(transport, serialized);
Collection<Condition> nomads = read(transport, serialized(sedentaries));
// then
assertEquals(new HashSet<>(textual(sedentaries)), new HashSet<>(textual(nomads)));
}

private String write(ConditionTransport transport, Collection<Condition> conditions) throws IOException {
ByteArrayOutputStream output = new ByteArrayOutputStream();
transport.write(conditions, output);
return new String(output.toByteArray());
}

private Collection<Condition> read(ConditionTransport transport, String source) throws IOException {
return transport.read(new ByteArrayInputStream(source.getBytes()));
}

private Set<String> textual(Collection<Condition> conditions) {
return conditions.stream()//
private Set<String> textual(Collection<Condition> condition) {
return condition.stream()//
.map(this::textual) //
.collect(Collectors.toSet());
}

private String textual(Condition condition) {
return new StringBuilder()//
.append(condition.feature())//
.append(condition.versionMatch().version())//
.append(condition.versionMatch().rule().identifier())//
.append(((ValidityPeriodClosed) condition.validityPeriod()).from())//
.append(((ValidityPeriodClosed) condition.validityPeriod()).to())//
.append(condition.evaluationInstructions().type().identifier())//
.append(condition.evaluationInstructions().expression()) //
.toString();
}

/**
* Provide any textual representation of a condition that distinguishes two
* instances with differing content.
* Supply source for reading which is expected to be parsed to
* {@code conditions()}
*/
protected abstract String textual(Condition condition);
protected abstract String serialized(Collection<Condition> condition);

/**
* Produce new instance of a transport under test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Collection;

import org.eclipse.passage.lic.internal.api.conditions.Condition;
Expand All @@ -34,9 +33,4 @@ public Collection<Condition> read(InputStream input) throws IOException {
throw new UnsupportedOperationException();
}

@Override
public void write(Collection<Condition> conditions, OutputStream output) throws IOException {
throw new UnsupportedOperationException();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
Expand Down Expand Up @@ -62,11 +61,6 @@ public Collection<Condition> read(InputStream input) throws IOException {
return conditions;
}

@Override
public void write(Collection<Condition> conditions, OutputStream output) throws IOException {
throw new UnsupportedOperationException();
}

private VersionMatch commonVersion() {
return new BaseVersionMatch("1.0.0", new MatchingRuleDefault()); //$NON-NLS-1$
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,12 @@

import org.eclipse.passage.lic.api.tests.conditions.mining.ConditionTransportContractTest;
import org.eclipse.passage.lic.internal.api.conditions.Condition;
import org.eclipse.passage.lic.internal.api.conditions.ValidityPeriodClosed;
import org.eclipse.passage.lic.internal.api.conditions.mining.ConditionTransport;
import org.eclipse.passage.lic.internal.json.tobemoved.JsonConditionTransport;

@SuppressWarnings("restriction")
public final class JsonConditionTransportTest extends ConditionTransportContractTest {

@Override
protected String textual(Condition condition) {
return new StringBuffer() //
.append(condition.feature()).append('|') //
.append(condition.versionMatch().version()).append('|') //
.append(condition.versionMatch().rule().identifier()).append('|') //
.append(textual(((ValidityPeriodClosed) condition.validityPeriod()))).append('|') //
.append(condition.evaluationInstructions().type().identifier()).append('|') //
.append(condition.evaluationInstructions().expression()) //
.toString();

}

private String textual(ValidityPeriodClosed period) {
return String.format("%d-%d", period.from().getTime(), period.to().getTime()); //$NON-NLS-1$
}

@Override
protected ConditionTransport transport() {
return new JsonConditionTransport();
Expand All @@ -50,4 +32,9 @@ protected Collection<Condition> conditions() {
return new TestConditions().conditions();
}

@Override
protected String serialized(Collection<Condition> condition) {
return new TestConditions().textual();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
*******************************************************************************/
package org.eclipse.passage.lic.json.tests.tobemoved;

import static org.junit.Assert.fail;
import static org.junit.Assume.assumeNoException;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
Expand All @@ -26,6 +31,10 @@
import org.eclipse.passage.lic.internal.base.conditions.MatchingRuleCompatible;
import org.eclipse.passage.lic.internal.base.conditions.MatchingRuleEquivalent;
import org.eclipse.passage.lic.internal.base.conditions.MatchingRulePerfect;
import org.eclipse.passage.lic.internal.json.tobemoved.ConditionPack;
import org.eclipse.passage.lic.internal.json.tobemoved.JsonObjectMapper;

import com.fasterxml.jackson.core.JsonProcessingException;

@SuppressWarnings("restriction")
public final class TestConditions {
Expand All @@ -50,4 +59,17 @@ Collection<Condition> conditions() {
return conditions;
}

String textual() {
ByteArrayOutputStream output = new ByteArrayOutputStream();
try {
output.write(new JsonObjectMapper().get().writeValueAsBytes(new ConditionPack(conditions)));
return new String(output.toByteArray(), "UTF-8"); //$NON-NLS-1$
} catch (JsonProcessingException e) {
fail("Is not intendd to fail on valid data"); //$NON-NLS-1$
} catch (IOException e) {
assumeNoException(e); // file system misbehavior must not fail test
}
throw new RuntimeException("unreachable"); //$NON-NLS-1$
}

}

0 comments on commit 9c72909

Please sign in to comment.