Skip to content

Commit b699c44

Browse files
committed
Add docs to GetKey + testing
1 parent f37b626 commit b699c44

File tree

2 files changed

+76
-14
lines changed

2 files changed

+76
-14
lines changed

src/main/java/io/cryptolens/methods/Key.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,26 @@ public static LicenseKey Activate (String token, String RSAPubKey, ActivateModel
9494
return LicenseKey.LoadFromString(RSAPubKey, result.RawResponse);
9595
}
9696

97+
/**
98+
* Calls the GetKey method (https://app.cryptolens.io/docs/api/v3/GetKey?modelVersion=3).
99+
* <p>This method allows you to retrieve the error message from the Web API.</p>
100+
* <p>
101+
* To retrieve the error message, you need to initialize an APIError object and pass it in into the
102+
* "error" parameter. For example,
103+
* </p>
104+
* <code>
105+
* APIError error = new APIError();<br>
106+
* LicenseKey license = Key.GetKey(auth, RSAPubKey, new GetKeyModel(3349, "ICVLD-VVSZR-ZTICT-YKGXL"), error);<br>
107+
* System.out.println(error.message);
108+
* </code>
109+
* @param token The access token with 'GetKey' permission.
110+
* @param RSAPubKey Your RSA Public Key, which can be found at https://app.cryptolens.io/docs/api/v3/QuickStart.
111+
* @param model Method parameters.
112+
* @param error The error object whose Message field will be populated if an error has occurred. Please initialize
113+
* this parameter, i.e. define <code>APIError error = new APIError();</code> and then pass
114+
* <code>error</code> into this parameter.
115+
* @return A LicenseKey object if success and null otherwise.
116+
*/
97117
public static LicenseKey GetKey (String token, String RSAPubKey, GetKeyModel model, APIError error) {
98118

99119
Map<String,String> extraParams = new HashMap<>();

src/test/java/io/cryptolens/KeyTest.java

Lines changed: 56 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,63 @@
11
package io.cryptolens;
22

3+
import com.google.gson.Gson;
4+
import com.google.gson.reflect.TypeToken;
35
import io.cryptolens.methods.Helpers;
46
import io.cryptolens.methods.Key;
5-
import io.cryptolens.models.APIError;
6-
import io.cryptolens.models.ActivateModel;
7-
import io.cryptolens.models.DeactivateModel;
8-
import io.cryptolens.models.LicenseKey;
7+
import io.cryptolens.models.*;
98
import junit.framework.Test;
109
import junit.framework.TestCase;
1110
import junit.framework.TestSuite;
1211

12+
import java.lang.reflect.Type;
13+
import java.nio.file.Files;
14+
import java.nio.file.Paths;
15+
import java.util.HashMap;
16+
1317
/**
1418
* Unit test for simple App.
1519
*/
1620
public class KeyTest
17-
extends TestCase
18-
{
21+
extends TestCase {
22+
23+
HashMap<String, String> APIKey = null;
24+
25+
1926
/**
2027
* Create the test case
2128
*
2229
* @param testName name of the test case
2330
*/
24-
public KeyTest(String testName )
25-
{
26-
super( testName );
31+
public KeyTest(String testName) {
32+
super(testName);
2733
}
2834

2935
/**
3036
* @return the suite of tests being tested
3137
*/
32-
public static Test suite()
33-
{
34-
return new TestSuite( KeyTest.class );
38+
public static Test suite() {
39+
return new TestSuite(KeyTest.class);
40+
}
41+
42+
public void init() throws Exception{
43+
String api_content = new String(Files.readAllBytes(Paths.get("apikeys.json")), "UTF-8");
44+
Type type = new TypeToken<HashMap<String, String>>(){}.getType();
45+
APIKey = new Gson().fromJson(api_content, type);
46+
47+
assertTrue( true );
48+
3549
}
3650

51+
3752
/**
3853
* Rigourous Test :-)
3954
*/
4055
public void testApp() throws Exception {
4156

57+
init();
58+
4259
String RSAPubKey = "<RSAKeyValue><Modulus>sGbvxwdlDbqFXOMlVUnAF5ew0t0WpPW7rFpI5jHQOFkht/326dvh7t74RYeMpjy357NljouhpTLA3a6idnn4j6c3jmPWBkjZndGsPL4Bqm+fwE48nKpGPjkj4q/yzT4tHXBTyvaBjA8bVoCTnu+LiC4XEaLZRThGzIn5KQXKCigg6tQRy0GXE13XYFVz/x1mjFbT9/7dS8p85n8BuwlY5JvuBIQkKhuCNFfrUxBWyu87CFnXWjIupCD2VO/GbxaCvzrRjLZjAngLCMtZbYBALksqGPgTUN7ZM24XbPWyLtKPaXF2i4XRR9u6eTj5BfnLbKAU5PIVfjIS+vNYYogteQ==</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>";
43-
String auth = "WyIyNjA5IiwiaWE5b0VFT3Q2eDlNR2FvbHBHK2VOYUZ4bzNjT3h5UkNrMCtiYnhPRSJd";
60+
String auth = APIKey.get("getkeyactivate");
4461

4562
APIError error = new APIError();
4663

@@ -65,7 +82,32 @@ public void testApp() throws Exception {
6582
//Key.Deactivate("", new DeactivateModel(3349, "ICVLD-VVSZR-ZTICT-YKGXL", Helpers.GetMachineCode()));
6683
System.out.println(Helpers.GetMachineCode());
6784

68-
assertTrue( true );
85+
assertTrue(true);
6986

7087
}
88+
89+
90+
public void testGetKey() throws Exception {
91+
92+
init();
93+
94+
String RSAPubKey = "<RSAKeyValue><Modulus>sGbvxwdlDbqFXOMlVUnAF5ew0t0WpPW7rFpI5jHQOFkht/326dvh7t74RYeMpjy357NljouhpTLA3a6idnn4j6c3jmPWBkjZndGsPL4Bqm+fwE48nKpGPjkj4q/yzT4tHXBTyvaBjA8bVoCTnu+LiC4XEaLZRThGzIn5KQXKCigg6tQRy0GXE13XYFVz/x1mjFbT9/7dS8p85n8BuwlY5JvuBIQkKhuCNFfrUxBWyu87CFnXWjIupCD2VO/GbxaCvzrRjLZjAngLCMtZbYBALksqGPgTUN7ZM24XbPWyLtKPaXF2i4XRR9u6eTj5BfnLbKAU5PIVfjIS+vNYYogteQ==</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>";
95+
String auth = APIKey.get("getkeyactivate");
96+
97+
APIError error = new APIError();
98+
99+
LicenseKey license = Key.GetKey(auth, RSAPubKey, new GetKeyModel(3349, "ICVLD-VVSZR-ZTICT-YKGXL"), error);
100+
101+
if (license == null) {
102+
System.out.println("The license does not work.");
103+
System.out.println("Error: " + error.message);
104+
} else {
105+
System.out.println("The license is valid!");
106+
System.out.println("It will expire: " + license.Expires);
107+
}
108+
109+
assertTrue(true);
110+
111+
}
112+
71113
}

0 commit comments

Comments
 (0)