BAEL-1461: Combined modules and refactored
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
package com.baeldung.ethereumj.controllers;
|
||||
|
||||
import static junit.framework.TestCase.assertTrue;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.context.embedded.LocalServerPort;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.test.context.TestPropertySource;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import com.baeldung.ethereumj.ApplicationMain;
|
||||
import com.baeldung.ethereumj.Constants;
|
||||
import com.baeldung.ethereumj.transfer.EthResponse;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = ApplicationMain.class, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
|
||||
@TestPropertySource(properties = "server.port=8080")
|
||||
public class EthControllerLiveTest {
|
||||
|
||||
@LocalServerPort
|
||||
int port;
|
||||
|
||||
private RestTemplate restTemplate = new RestTemplate();
|
||||
|
||||
private String url(String uri) {
|
||||
String s = "http://localhost:" + port + uri;
|
||||
System.out.println(s);
|
||||
return s;
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
restTemplate = new RestTemplate();
|
||||
}
|
||||
|
||||
@Test()
|
||||
public void bestBlockTest() throws Exception {
|
||||
|
||||
Thread.sleep(20000);
|
||||
|
||||
EthResponse a = restTemplate.getForObject(url(Constants.ENDPOINT_ONE), EthResponse.class);
|
||||
assertNotNull(a);
|
||||
|
||||
ResponseEntity<EthResponse> b = restTemplate.exchange(
|
||||
url(Constants.ENDPOINT_ONE),
|
||||
HttpMethod.GET, new HttpEntity<EthResponse>(null, new HttpHeaders()), EthResponse.class);
|
||||
|
||||
assertTrue("Status 200?", b.getStatusCode().equals(HttpStatus.OK));
|
||||
System.out.println("Status 200?: " + b.getStatusCode().equals(HttpStatus.OK));
|
||||
assertTrue("Dynamic data returned?", b.hasBody());
|
||||
System.out.println("Dynamic data returned?: " + b.hasBody());
|
||||
}
|
||||
|
||||
@Test()
|
||||
public void difficultyTest() throws Exception {
|
||||
|
||||
Thread.sleep(20000);
|
||||
|
||||
ResponseEntity<EthResponse> a = restTemplate.exchange(
|
||||
url(Constants.ENDPOINT_TWO),
|
||||
HttpMethod.GET, new HttpEntity<EthResponse>(null, new HttpHeaders()), EthResponse.class);
|
||||
|
||||
assertTrue("Status 200?", a.getStatusCode().equals(HttpStatus.OK));
|
||||
System.out.println("Status 200?: " + a.getStatusCode().equals(HttpStatus.OK));
|
||||
assertTrue("Dynamic data returned?", a.hasBody());
|
||||
System.out.println("Dynamic data returned?: " + a.hasBody());
|
||||
}
|
||||
}
|
||||
+78
@@ -0,0 +1,78 @@
|
||||
package com.baeldung.web3j.controllers;
|
||||
|
||||
|
||||
import com.baeldung.web3j.config.AppConfig;
|
||||
import com.baeldung.web3j.constants.Constants;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.web.WebAppConfiguration;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.MvcResult;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.asyncDispatch;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = {AppConfig.class})
|
||||
@WebAppConfiguration
|
||||
public class EthereumRestControllerIntegrationTest {
|
||||
|
||||
private MockMvc mockMvc;
|
||||
|
||||
private void constructAsyncTest(String endpoint) {
|
||||
try {
|
||||
MvcResult asyncResult = mockMvc
|
||||
.perform(get(endpoint))
|
||||
.andReturn();
|
||||
|
||||
mockMvc.perform(asyncDispatch(asyncResult))
|
||||
.andDo(print())
|
||||
.andExpect(status().isOk());
|
||||
|
||||
} catch (Exception e) {
|
||||
System.out.println("Exception: " + e);
|
||||
}
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private WebApplicationContext wac;
|
||||
|
||||
@Before
|
||||
public void preTest() throws Exception {
|
||||
mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void accounts() {
|
||||
constructAsyncTest(Constants.API_ACCOUNTS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void transactions() {
|
||||
constructAsyncTest(Constants.API_TRANSACTIONS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void block() {
|
||||
constructAsyncTest(Constants.API_BLOCK);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void balance() {
|
||||
constructAsyncTest(Constants.API_BALANCE);
|
||||
}
|
||||
|
||||
@After
|
||||
public void postTest() {
|
||||
mockMvc = null;
|
||||
}
|
||||
}
|
||||
+76
@@ -0,0 +1,76 @@
|
||||
package com.baeldung.web3j.controllers;
|
||||
|
||||
import com.baeldung.web3j.constants.Constants;
|
||||
import com.baeldung.web3j.services.Web3Service;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.MvcResult;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.asyncDispatch;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
public class EthereumRestControllerUnitTest {
|
||||
|
||||
private MockMvc mockMvc;
|
||||
|
||||
private void constructAsyncTest(String endpoint) {
|
||||
try {
|
||||
MvcResult asyncResult = mockMvc
|
||||
.perform(get(endpoint))
|
||||
.andReturn();
|
||||
|
||||
mockMvc.perform(asyncDispatch(asyncResult))
|
||||
.andDo(print())
|
||||
.andExpect(status().isOk());
|
||||
|
||||
} catch (Exception e) {
|
||||
System.out.println("Exception: " + e);
|
||||
}
|
||||
}
|
||||
|
||||
@Mock
|
||||
private Web3Service web3Service;
|
||||
|
||||
@InjectMocks
|
||||
private EthereumRestController ethereumRestController;
|
||||
|
||||
@Before
|
||||
public void preTest() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
mockMvc = MockMvcBuilders.standaloneSetup(ethereumRestController).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void accounts() {
|
||||
constructAsyncTest(Constants.API_ACCOUNTS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void transactions() {
|
||||
constructAsyncTest(Constants.API_TRANSACTIONS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void block() {
|
||||
constructAsyncTest(Constants.API_BLOCK);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void balance() {
|
||||
constructAsyncTest(Constants.API_BALANCE);
|
||||
}
|
||||
|
||||
@After
|
||||
public void postTest() {
|
||||
mockMvc = null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.baeldung.web3j.services;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
public class EthereumContractUnitTest {
|
||||
|
||||
private Web3Service web3Service;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
web3Service = new Web3Service();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testContract() {
|
||||
CompletableFuture<String> result = web3Service.fromScratchContractExample();
|
||||
assert (result instanceof CompletableFuture);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sendTx() {
|
||||
CompletableFuture<String> result = web3Service.sendTx();
|
||||
assert (result instanceof CompletableFuture);
|
||||
}
|
||||
|
||||
@After
|
||||
public void cleanup() {
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user