-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #51 from chaoxinhu/feature/modify-context
Modify @context field and add Credential Serialization
- Loading branch information
Showing
7 changed files
with
225 additions
and
16 deletions.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
src/main/java/com/webank/weid/constant/CredentialConstant.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright© (2018-2019) WeBank Co., Ltd. | ||
* | ||
* This file is part of weidentity-java-sdk. | ||
* | ||
* weidentity-java-sdk is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* weidentity-java-sdk is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with weidentity-java-sdk. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package com.webank.weid.constant; | ||
|
||
/** | ||
* The Class CredentialConstant. | ||
* | ||
* @author chaoxinhu | ||
*/ | ||
|
||
public final class CredentialConstant { | ||
|
||
/** | ||
* The Constant default Credential Context. | ||
*/ | ||
public static final String DEFAULT_CREDENTIAL_CONTEXT = | ||
"https://www.w3.org/2018/credentials/v1"; | ||
/** | ||
* The Constant default Credential Context field name. | ||
*/ | ||
public static final String CREDENTIAL_CONTEXT_FIELD = "\"context\""; | ||
/** | ||
* The Constant default Credential Context field name in Credential Json String. | ||
*/ | ||
public static final String CREDENTIAL_CONTEXT_PORTABLE_JSON_FIELD = "\"@context\""; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
src/test/java/com/webank/weid/full/credential/TestGetCredentialJson.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Copyright© (2018-2019) WeBank Co., Ltd. | ||
* | ||
* This file is part of weidentity-java-sdk. | ||
* | ||
* weidentity-java-sdk is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* weidentity-java-sdk is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with weidentity-java-sdk. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package com.webank.weid.full.credential; | ||
|
||
import java.util.HashMap; | ||
import java.util.UUID; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import com.webank.weid.constant.CredentialConstant; | ||
import com.webank.weid.constant.ErrorCode; | ||
import com.webank.weid.full.TestBaseServcie; | ||
import com.webank.weid.protocol.base.Credential; | ||
import com.webank.weid.protocol.response.ResponseData; | ||
|
||
/** | ||
* Testing getCredentialJson method. | ||
* | ||
* @author chaoxinhu | ||
*/ | ||
public class TestGetCredentialJson extends TestBaseServcie { | ||
|
||
@Test | ||
public void testGetCredentialJsonCase1() { | ||
Credential credential = buildCredential(); | ||
ResponseData<String> response1 = credentialService.getCredentialJson(credential); | ||
Assert.assertEquals(ErrorCode.SUCCESS.getCode(), response1.getErrorCode().intValue()); | ||
Assert.assertNotNull(response1.getResult()); | ||
Assert.assertTrue( | ||
response1 | ||
.getResult() | ||
.contains(CredentialConstant.CREDENTIAL_CONTEXT_PORTABLE_JSON_FIELD)); | ||
Assert.assertTrue( | ||
response1 | ||
.getResult() | ||
.contains(CredentialConstant.DEFAULT_CREDENTIAL_CONTEXT)); | ||
|
||
credential = buildCredential(); | ||
credential.setIssuer("xxxxxxxxx"); | ||
response1 = credentialService.getCredentialHash(credential); | ||
Assert.assertNotEquals(ErrorCode.SUCCESS.getCode(), response1.getErrorCode().intValue()); | ||
Assert.assertEquals(StringUtils.EMPTY, response1.getResult()); | ||
} | ||
|
||
@Test | ||
public void testGetCredentialJsonCase2() { | ||
ResponseData<String> response1 = credentialService.getCredentialJson(null); | ||
Assert.assertNotEquals(ErrorCode.SUCCESS.getCode(), response1.getErrorCode().intValue()); | ||
Assert.assertEquals(StringUtils.EMPTY, response1.getResult()); | ||
} | ||
|
||
private Credential buildCredential() { | ||
Credential credential = new Credential(); | ||
HashMap<String, Object> claim = new HashMap<>(); | ||
claim.put("xxxxxxxxxxxxx", "xxxxxxxxxxxxx"); | ||
credential.setClaim(claim); | ||
credential.setContext(CredentialConstant.DEFAULT_CREDENTIAL_CONTEXT); | ||
credential.setCptId(Integer.valueOf(1002)); | ||
credential.setExpirationDate(System.currentTimeMillis() + 10000L); | ||
credential.setId(UUID.randomUUID().toString()); | ||
credential.setIssuer("did:weid:0x0000000000000001"); | ||
credential.setIssuranceDate(System.currentTimeMillis()); | ||
credential.setSignature("xxxxxxxxxxxx"); | ||
return credential; | ||
} | ||
} |