Skip to content

Commit

Permalink
Closes #17
Browse files Browse the repository at this point in the history
  • Loading branch information
artemlos committed May 6, 2019
1 parent c57ee5b commit 32e127c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
23 changes: 19 additions & 4 deletions src/main/java/io/cryptolens/methods/Helpers.java
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,8 @@ public class MyClass<T>
}

/**
* <p>Uses the notes field to determine if a certain feature exists (instead of the 8 feature flags).</p>
* <p><strong>Formatting: </strong> The notes field needs to be formatted as a JSON array of strings or of JSON arrays
* where the first element specifies the feature name and the second element is a list of features.</p>
* <p>Uses a special data object associated with the license key to determine if a certain feature exists (instead of the 8 feature flags).</p>
* <p><strong>Formatting: </strong> The name of the data object should be 'cryptolens_features' and it should be structured as a JSON array.</p>
* <p>For example,</p> <pre>["f1", "f2"]</pre><p>means f1 and f2 are true. You can also have feature bundling, eg. </p> <pre>["f1", ["f2",["voice","image"]]]</pre>
* <p>which means that f1 and f2 are true, as well as f2.limited and f2.image. You can set any depth, eg. you can have</p>
* <pre>["f1", ["f2",[["voice",["all"]], "image"]]]</pre> <p>means f2.voice.all is true as well as f2.voice and f2.
Expand All @@ -218,8 +217,24 @@ public class MyClass<T>
*/
public static boolean HasFeature(LicenseKey licenseKey, String featureName) {

if(licenseKey.DataObjects == null) {
return false;
}

String features = null;
for(int i = 0; i < licenseKey.DataObjects.size(); i++) {
if(licenseKey.DataObjects.get(i).Name.equals("cryptolens_features")) {
features = licenseKey.DataObjects.get(i).StringValue;
}
}

if(features == null) {
// data object not found.
return false;
}

JsonParser parser = new JsonParser();
JsonArray array = parser.parse(licenseKey.Notes).getAsJsonArray();
JsonArray array = parser.parse(features).getAsJsonArray();
String[] featurePath = featureName.split("\\.");

boolean found = false;
Expand Down
14 changes: 12 additions & 2 deletions src/test/java/io/cryptolens/HelpersTest.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package io.cryptolens;

import io.cryptolens.methods.Helpers;
import io.cryptolens.models.DataObject;
import io.cryptolens.models.LicenseKey;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

import java.util.ArrayList;

/**
* Unit test for simple App.
*/
Expand Down Expand Up @@ -36,14 +39,21 @@ public static Test suite()
public void testApp()
{
LicenseKey license = new LicenseKey();
license.Notes = "[\"test\", [\"module\",[\"A\"]]]";

license.DataObjects = new ArrayList<>();

DataObject dobj = new DataObject();

dobj.Name = "cryptolens_features";
dobj.StringValue = "[\"test\", [\"module\",[\"A\"]]]";
license.DataObjects.add(dobj);

assertTrue(Helpers.HasFeature(license, "test"));
assertTrue(Helpers.HasFeature(license, "module"));
assertTrue(Helpers.HasFeature(license, "module.A"));
assertFalse(Helpers.HasFeature(license, "module.B"));

license.Notes = "[\"f1\", [\"f2\",[[\"voice\",[\"all\"]]]]]";
license.DataObjects.get(0).StringValue = "[\"f1\", [\"f2\",[[\"voice\",[\"all\"]]]]]";

assertTrue(Helpers.HasFeature(license, "f2.voice.all"));

Expand Down

0 comments on commit 32e127c

Please sign in to comment.