개발/java
steemit java commitor 되기 2 새로운 프로젝트 만들기
소확행개발자
2018. 9. 28. 18:28
steemit java commitor 되기 2 새로운 프로젝트 만들기
기존의 steemj 는 android용으로 개발되었기 때문에 spring boot 에서 사용할때 최적화 되기 위하여 프로젝트를 새로 만든다.
추후에 jitpack에 등록하여 library 형식으로 사용할 것이기 때문에 intelliJ에서 spring initializer 가 아니라 gradle project 의 java project로 만들어 주어서
jar 로 떨궈주는게 좋다.
AccounName 만들기
기존의 AccountName 은 jackson으로 되어있었지만 이걸 gson으로 변환시켜주는 작업을 해준다.
AccountNameTest
public class AccountNameTest {
private final String EXPECTED_BYTE_REPRESENTATION = "0764657a31333337";
/**
* Test the
* {@link models.AccountName#toByteArray()
* toByteArray()} method.
*
* @throws Exception
* In case of a problem.
*/
@Test
public void testAccountNameToByteArray() throws Exception {
AccountName myAccount = new AccountName("derekpark");
assertThat("Expect that the accountName object has the given byte representation.",
CryptoUtils.HEX.encode(myAccount.toByteArray()), equalTo(EXPECTED_BYTE_REPRESENTATION));
}
/**
* Test the validation of the
* {@link models.AccountName#setName(String name)
* setName(String name)} method by providing an account name which is too
* long.
*/
@Test(expected = InvalidParameterException.class)
public void testAccountNameValidationTooLong() {
new AccountName("thisaccountnameistoolong");
}
/**
* Test the validation of the
* {@link models.AccountName#setName(String name)
* setName(String name)} method by providing an account name which is too
* short.
*/
@Test(expected = InvalidParameterException.class)
public void testAccountNameValidationTooShort() {
new AccountName("sh");
}
/**
* Test the validation of the
* {@link models.AccountName#setName(String name)
* setName(String name)} method by providing an account name with an invalid
* start character.
*/
@Test(expected = InvalidParameterException.class)
public void testAccountNameValidationWrongStartChar() {
new AccountName("-dez1337");
}
/**
* Test the validation of the
* {@link models.AccountName#setName(String name)
* setName(String name)} method by providing an account name with an invalid
* end character.
*/
@Test(expected = InvalidParameterException.class)
public void testAccountNameValidationWrongEndChar() {
new AccountName("dez1337-");
}
/**
* Test the validation of the
* {@link models.AccountName#setName(String name)
* setName(String name)} method by providing valid account names.
*/
@Test
public void testAccountNameValidation() {
new AccountName("dez-1337");
new AccountName("dez");
new AccountName("dez1337-steemj");
}
}
steem account exception 처리에 대한 test code이다.
AccountName
public class AccountName implements ByteTransformable {
private String name;
public AccountName() {
this.setName("");
}
public AccountName(String name) {
this.setName(name);
}
public String getName() {
return name;
}
public void setName(String name) {
if (name == null) {
this.name = "";
} else {
if (!name.isEmpty()) {
if (name.length() < 3 || name.length() > 16) {
throw new InvalidParameterException(
"An account name needs to have a minimum length of 3 and a maximum length of 16.");
} else if (!name.split("\\.")[0].matches("^[a-z]{1}[a-z0-9\\-]{1,14}[a-z0-9]{1}")) {
/*
* It looks like only values infront of a "." are validated.
* Those characters in front of a dot must fullfil the
* following rules: The first char needs to be one of "a-z"
* while a "-" and "0-9" are allowed for further chars.
*/
throw new InvalidParameterException("The given account name '" + name
+ "' contains unsupported characters. The first character needs to be one"
+ " of 'a-z', characters in the middle can be 'a-z', '0,9' and a '-' and the last character of the "
+ "name has to be one of 'a-z' and '0-9'.");
}
}
this.name = name;
}
}
@Override
public byte[] toByteArray() throws SteemInvalidTransactionException {
return OvndSteemJUtil.transformStringToVarIntByteArray(this.getName());
}
}