Skip to content

Commit

Permalink
Bug 564420 conditions | move HC condition miner to new interfaces
Browse files Browse the repository at this point in the history
 - compose a test for RemoteConditionsRequest url composition

Signed-off-by: elena.parovyshnaya <elena.parovyshnaya@gmail.com>
  • Loading branch information
eparovyshnaya committed Jul 1, 2020
1 parent c270ed5 commit 8487459
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 9 deletions.
Expand Up @@ -17,7 +17,10 @@
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.HashMap;
import java.util.function.Supplier;

import org.eclipse.passage.lic.internal.api.LicensedProduct;
import org.eclipse.passage.lic.internal.api.LicensingException;
Expand All @@ -37,23 +40,26 @@
import org.eclipse.passage.lic.internal.net.LicensingServerCoordinates.HostPort;

/**
* Basing on
*
* @author user
*/
@SuppressWarnings("restriction")
final class RemoteConditionsRequest implements Request<HttpURLConnection> {
public final class RemoteConditionsRequest implements Request<HttpURLConnection> {

private final LicensedProduct product;
private final Supplier<Path> settings;

public RemoteConditionsRequest(LicensedProduct product) {
public RemoteConditionsRequest(LicensedProduct product, Supplier<Path> settings) {
this.product = product;
this.settings = settings;
}

public RemoteConditionsRequest(LicensedProduct product) {
this(product, new InstallationPath());
}

@Override
public URL url() throws ConditionMiningException {
try {
HostPort corrdinates = new LicensingServerCoordinates(new InstallationPath()).get();
HostPort corrdinates = new LicensingServerCoordinates(settings).get();
return new URL("http", //$NON-NLS-1$
corrdinates.host(), //
Integer.parseInt(corrdinates.port()), //
Expand Down Expand Up @@ -86,7 +92,7 @@ private String encode(String value) throws UnsupportedEncodingException {

@Override
public Configuration<HttpURLConnection> config() {
throw new UnsupportedOperationException();
return new HttpUrlConnectionConfiguration(1000, new HashMap<>());
}

}
Expand Up @@ -27,6 +27,10 @@ public LicensingServerPort(String value) {
super(value);
}

public LicensingServerPort(int value) {
super(Integer.toString(value));
}

@Override
public String entrySeparator() {
return "\n"; //$NON-NLS-1$
Expand Down
5 changes: 4 additions & 1 deletion tests/org.eclipse.passage.lic.hc.tests/META-INF/MANIFEST.MF
Expand Up @@ -7,4 +7,7 @@ Bundle-Name: %Bundle-Name
Bundle-Vendor: %Bundle-Vendor
Bundle-Copyright: %Bundle-Copyright
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Require-Bundle: org.junit
Require-Bundle: org.junit,
org.eclipse.passage.lic.base,
org.eclipse.passage.lic.net,
org.eclipse.passage.lic.hc
Expand Up @@ -12,12 +12,78 @@
*******************************************************************************/
package org.eclipse.passage.lic.internal.hc.tests;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.FileWriter;
import java.io.IOException;
import java.net.URL;

import org.eclipse.passage.lic.internal.api.LicensedProduct;
import org.eclipse.passage.lic.internal.api.conditions.ConditionAction;
import org.eclipse.passage.lic.internal.api.conditions.UserRole;
import org.eclipse.passage.lic.internal.api.conditions.mining.ConditionMiningException;
import org.eclipse.passage.lic.internal.base.BaseLicensedProduct;
import org.eclipse.passage.lic.internal.base.NamedData;
import org.eclipse.passage.lic.internal.base.ProductIdentifier;
import org.eclipse.passage.lic.internal.base.ProductVersion;
import org.eclipse.passage.lic.internal.base.io.PassageFileExtension;
import org.eclipse.passage.lic.internal.hc.remote.impl.RemoteConditionsRequest;
import org.eclipse.passage.lic.internal.net.LicensingAction;
import org.eclipse.passage.lic.internal.net.LicensingRole;
import org.eclipse.passage.lic.internal.net.LicensingServerHost;
import org.eclipse.passage.lic.internal.net.LicensingServerPort;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

@SuppressWarnings("restriction")
public final class RemoteConditionsRequestTest {

private final String host = "fake.licensing.server"; //$NON-NLS-1$
private final int port = 8080;

@Rule
public TemporaryFolder folder = new TemporaryFolder();

@Test
public void f() {
public void urlComposed() throws IOException {
writeSettings();
URL url = url();
assertEquals(host, url.getHost());
assertEquals(port, url.getPort());
assertNotNull(url.getQuery());
assertTrue(url.getQuery().contains(new ProductIdentifier("any").key())); //$NON-NLS-1$
assertTrue(url.getQuery().contains(new ProductVersion("any").key())); //$NON-NLS-1$
assertTrue(url.getQuery().contains(new LicensingAction(new ConditionAction.Of("any")).key())); //$NON-NLS-1$
assertTrue(url.getQuery().contains(new LicensingRole(new UserRole.Of("any")).key())); //$NON-NLS-1$
}

private URL url() {
try {
return new RemoteConditionsRequest(product(), folder.getRoot()::toPath).url();
} catch (ConditionMiningException e) {
fail("Url composition on valid parameters must succssed"); //$NON-NLS-1$
throw new RuntimeException(e); // unreachable
}
}

private void writeSettings() throws IOException {
StringBuilder output = new StringBuilder();
new NamedData.Writable<String>(new LicensingServerHost(host)).write(output, "=", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
new NamedData.Writable<String>(new LicensingServerPort(port)).write(output, "=", "\n");//$NON-NLS-1$ //$NON-NLS-2$
try (FileWriter writer = new FileWriter(folder.newFile(settingsFileName()))) {
writer.append(output.toString());
}
}

private LicensedProduct product() {
return new BaseLicensedProduct("fake-product", "0.1.27"); //$NON-NLS-1$//$NON-NLS-2$
}

private String settingsFileName() {
return Long.toHexString(System.nanoTime()) + new PassageFileExtension.Settings().get();
}
}

0 comments on commit 8487459

Please sign in to comment.