본문 바로가기
  • Where there is a will there is a way.
개발/java

steemit java commitor 되기 2 새로운 프로젝트 만들기

by 소확행개발자 2018. 9. 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());
}
}


'개발 > java' 카테고리의 다른 글

자바 람다식이란?  (0) 2018.10.06
자바 개발 쓰레드에 대한 정리와 제네릭 사용이유  (0) 2018.09.29
steemit java commitor 되기 1 core 살펴보기  (0) 2018.09.27
RxJava Observerable  (0) 2018.09.21
RxJava 란 무엇인가  (1) 2018.09.21

댓글