Skip to content

Commit

Permalink
add address tests according to ton-sdk-use-cases rules
Browse files Browse the repository at this point in the history
  • Loading branch information
neodiX committed Jun 19, 2024
1 parent 631e2ec commit e6fc384
Show file tree
Hide file tree
Showing 3 changed files with 324 additions and 0 deletions.
15 changes: 15 additions & 0 deletions address/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,20 @@
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
276 changes: 276 additions & 0 deletions address/src/test/java/org/ton/java/address/TestTonSdkTestCases.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,276 @@
package org.ton.java.address;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.ToNumberPolicy;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import java.io.IOException;
import java.net.URL;
import java.nio.charset.Charset;

import static org.assertj.core.api.Assertions.assertThat;

@Slf4j
@RunWith(JUnit4.class)
public class TestTonSdkTestCases {

public static final String addressTestFileUrl = "https://raw.githubusercontent.com/neodix42/ton-sdk-test-cases/main/address.json";
Gson gson = new GsonBuilder().setObjectToNumberStrategy(ToNumberPolicy.LONG_OR_DOUBLE).create();

String fileContentWithUseCases = IOUtils.toString(new URL(addressTestFileUrl), Charset.defaultCharset());

public TestTonSdkTestCases() throws IOException {
}

@Test
public void testAddress1() {

// read the JSON file with tests cases
TonSdkTestCases tonSdkTestCases = gson.fromJson(fileContentWithUseCases, TonSdkTestCases.class);

String testId = "address-1";
// select particular test case by category name and test id
TonSdkTestCases.TestCase testCase = tonSdkTestCases.getTestCases().get(testId);

// fetch test's description and id. It's always good to show test id, since it is unique across all tests.
String description = testCase.getDescription();

log.info("testId: {}", testId);
log.info("description: {}", description);

// fetch input parameters and print test case details
Address address = Address.of((String) testCase.getInput().get("inputRawAddress"));

log.info("input parameters:");
log.info(" address: {}", address.toRaw());

// test the functionality of your library

// fetch the expected result and compare it against the actual one
String expectedBounceableUrlSafe = (String) testCase.getExpectedOutput().get("bounceableUrlSafe");
String expectedNonBounceableUrlSafe = (String) testCase.getExpectedOutput().get("nonBounceableUrlSafe");
String expectedBounceable = (String) testCase.getExpectedOutput().get("bounceable");
String expectedNonBounceable = (String) testCase.getExpectedOutput().get("nonBounceable");
String expectedBounceableUrlSafeTest = (String) testCase.getExpectedOutput().get("bounceableUrlSafeTest");
String expectedNonBounceableUrlSafeTest = (String) testCase.getExpectedOutput().get("nonBounceableUrlSafeTest");
String expectedBounceableTest = (String) testCase.getExpectedOutput().get("bounceableTest");
String expectedNonBounceableTest = (String) testCase.getExpectedOutput().get("nonBounceableTest");

assertThat(expectedBounceableUrlSafe).isEqualTo(address.toBounceable());
assertThat(expectedNonBounceableUrlSafe).isEqualTo(address.toNonBounceable());
assertThat(expectedBounceable).isEqualTo(address.toString(true, false, true));
assertThat(expectedNonBounceable).isEqualTo(address.toString(true, false, false));
assertThat(expectedBounceableUrlSafeTest).isEqualTo(address.toString(true, true, true, true));
assertThat(expectedNonBounceableUrlSafeTest).isEqualTo(address.toString(true, true, false, true));
assertThat(expectedBounceableTest).isEqualTo(address.toString(true, false, true, true));
assertThat(expectedNonBounceableTest).isEqualTo(address.toString(true, false, false, true));
}

@Test
public void testAddress2() {

TonSdkTestCases tonSdkTestCases = gson.fromJson(fileContentWithUseCases, TonSdkTestCases.class);

String testId = "address-2";
TonSdkTestCases.TestCase testCase = tonSdkTestCases.getTestCases().get(testId);

String description = testCase.getDescription();

log.info("testId: {}", testId);
log.info("description: {}", description);

Address address = Address.of((String) testCase.getInput().get("inputRawAddress"));

log.info("input parameters:");
log.info(" address: {}", address.toRaw());

String expectedBounceableUrlSafe = (String) testCase.getExpectedOutput().get("bounceableUrlSafe");
String expectedNonBounceableUrlSafe = (String) testCase.getExpectedOutput().get("nonBounceableUrlSafe");
String expectedBounceable = (String) testCase.getExpectedOutput().get("bounceable");
String expectedNonBounceable = (String) testCase.getExpectedOutput().get("nonBounceable");

assertThat(expectedBounceableUrlSafe).isEqualTo(address.toBounceable());
assertThat(expectedNonBounceableUrlSafe).isEqualTo(address.toNonBounceable());
assertThat(expectedBounceable).isEqualTo(address.toString(true, false, true));
assertThat(expectedNonBounceable).isEqualTo(address.toString(true, false, false));
}

@Test(expected = Error.class)
public void testAddress3() {

TonSdkTestCases tonSdkTestCases = gson.fromJson(fileContentWithUseCases, TonSdkTestCases.class);

String testId = "address-3";
TonSdkTestCases.TestCase testCase = tonSdkTestCases.getTestCases().get(testId);

String description = testCase.getDescription();

log.info("testId: {}", testId);
log.info("description: {}", description);

Address address = Address.of((String) testCase.getInput().get("inputRawAddress"));
log.info("address {}", address);
}

@Test(expected = Error.class)
public void testAddress4() {

TonSdkTestCases tonSdkTestCases = gson.fromJson(fileContentWithUseCases, TonSdkTestCases.class);

String testId = "address-4";
TonSdkTestCases.TestCase testCase = tonSdkTestCases.getTestCases().get(testId);

String description = testCase.getDescription();

log.info("testId: {}", testId);
log.info("description: {}", description);

Address address = Address.of((String) testCase.getInput().get("inputRawAddress"));
log.info("address {}", address);
}

@Test(expected = Error.class)
public void testAddress5() {

TonSdkTestCases tonSdkTestCases = gson.fromJson(fileContentWithUseCases, TonSdkTestCases.class);

String testId = "address-5";
TonSdkTestCases.TestCase testCase = tonSdkTestCases.getTestCases().get(testId);

String description = testCase.getDescription();

log.info("testId: {}", testId);
log.info("description: {}", description);

Address address = Address.of((String) testCase.getInput().get("inputRawAddress"));
log.info("address {}", address);
}

@Test
public void testAddress6() {

TonSdkTestCases tonSdkTestCases = gson.fromJson(fileContentWithUseCases, TonSdkTestCases.class);

String testId = "address-6";
TonSdkTestCases.TestCase testCase = tonSdkTestCases.getTestCases().get(testId);

String description = testCase.getDescription();

log.info("testId: {}", testId);
log.info("description: {}", description);

Address address = Address.of((String) testCase.getInput().get("inputRawAddress"));

log.info("input parameters:");
log.info(" address: {}", address.toRaw());

String expectedBounceableUrlSafe = (String) testCase.getExpectedOutput().get("bounceableUrlSafe");
String expectedNonBounceableUrlSafe = (String) testCase.getExpectedOutput().get("nonBounceableUrlSafe");
String expectedBounceable = (String) testCase.getExpectedOutput().get("bounceable");
String expectedNonBounceable = (String) testCase.getExpectedOutput().get("nonBounceable");

assertThat(expectedBounceableUrlSafe).isEqualTo(address.toBounceable());
assertThat(expectedNonBounceableUrlSafe).isEqualTo(address.toNonBounceable());
assertThat(expectedBounceable).isEqualTo(address.toString(true, false, true));
assertThat(expectedNonBounceable).isEqualTo(address.toString(true, false, false));
}

@Test
public void testAddress7() {

TonSdkTestCases tonSdkTestCases = gson.fromJson(fileContentWithUseCases, TonSdkTestCases.class);

String testId = "address-7";
TonSdkTestCases.TestCase testCase = tonSdkTestCases.getTestCases().get(testId);

String description = testCase.getDescription();

log.info("testId: {}", testId);
log.info("description: {}", description);

Address address = Address.of((String) testCase.getInput().get("inputRawAddress"));

log.info("input parameters:");
log.info(" address: {}", address.toRaw());

String expectedBounceableUrlSafe = (String) testCase.getExpectedOutput().get("bounceableUrlSafe");
String expectedNonBounceableUrlSafe = (String) testCase.getExpectedOutput().get("nonBounceableUrlSafe");
String expectedBounceable = (String) testCase.getExpectedOutput().get("bounceable");
String expectedNonBounceable = (String) testCase.getExpectedOutput().get("nonBounceable");

assertThat(expectedBounceableUrlSafe).isEqualTo(address.toBounceable());
assertThat(expectedNonBounceableUrlSafe).isEqualTo(address.toNonBounceable());
assertThat(expectedBounceable).isEqualTo(address.toString(true, false, true));
assertThat(expectedNonBounceable).isEqualTo(address.toString(true, false, false));
}

@Test
public void testAddress8() {

TonSdkTestCases tonSdkTestCases = gson.fromJson(fileContentWithUseCases, TonSdkTestCases.class);

String testId = "address-8";
TonSdkTestCases.TestCase testCase = tonSdkTestCases.getTestCases().get(testId);

String description = testCase.getDescription();

log.info("testId: {}", testId);
log.info("description: {}", description);

Address address = Address.of((String) testCase.getInput().get("bounceableUrlSafe"));

log.info("input parameters:");
log.info(" address: {}", address.toBounceable());

String expectedRawAddress = (String) testCase.getExpectedOutput().get("rawAddress");

assertThat(expectedRawAddress).isEqualTo(address.toRaw());
}

@Test
public void testAddress9() {

TonSdkTestCases tonSdkTestCases = gson.fromJson(fileContentWithUseCases, TonSdkTestCases.class);

String testId = "address-9";
TonSdkTestCases.TestCase testCase = tonSdkTestCases.getTestCases().get(testId);

String description = testCase.getDescription();

log.info("testId: {}", testId);
log.info("description: {}", description);

Address address = Address.of((String) testCase.getInput().get("nonBounceable"));

log.info("input parameters:");
log.info(" address: {}", address.toBounceable());

String expectedRawAddress = (String) testCase.getExpectedOutput().get("rawAddress");

assertThat(expectedRawAddress).isEqualTo(address.toRaw());
}

@Test(expected = Error.class)
public void testAddress10() {

TonSdkTestCases tonSdkTestCases = gson.fromJson(fileContentWithUseCases, TonSdkTestCases.class);

String testId = "address-10";
TonSdkTestCases.TestCase testCase = tonSdkTestCases.getTestCases().get(testId);

String description = testCase.getDescription();

log.info("testId: {}", testId);
log.info("description: {}", description);

Address address = Address.of((String) testCase.getInput().get("bounceableUrlSafe"));

log.info("input parameters:");
log.info(" address: {}", address.toBounceable());
}
}
33 changes: 33 additions & 0 deletions address/src/test/java/org/ton/java/address/TonSdkTestCases.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.ton.java.address;

import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

import java.io.Serializable;
import java.util.Map;

@Builder
@Getter
@Setter
@ToString
public class TonSdkTestCases implements Serializable {
private Map<String, TestCase> testCases;

@Setter
@Getter
public static class TestCase {

private String description;
private Map<String, Object> input;
private Map<String, Object> expectedOutput;

@Override
public String toString() {
return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
}
}
}

0 comments on commit e6fc384

Please sign in to comment.