Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix resource path URL decoding in tx validator tests and clean similar tests #7025

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions core/src/test/java/bisq/core/app/BisqHelpFormatterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import java.io.IOException;
import java.io.PrintStream;

import java.util.Objects;

import org.junit.jupiter.api.Test;

import static org.hamcrest.CoreMatchers.equalTo;
Expand Down Expand Up @@ -125,11 +127,11 @@ public void testHelpFormatter() throws IOException, URISyntaxException {
.defaultsTo(AnEnum.foo);

ByteArrayOutputStream actual = new ByteArrayOutputStream();
String expected = new String(Files.readAllBytes(Paths.get(getClass().getResource("cli-output.txt").toURI())));
String expected = Files.readString(Paths.get(Objects.requireNonNull(getClass().getResource("cli-output.txt")).toURI()));
if (System.getProperty("os.name").startsWith("Windows")) {
// Load the expected content from a different file for Windows due to different path separator
// And normalize line endings to LF in case the file has CRLF line endings
expected = new String(Files.readAllBytes(Paths.get(getClass().getResource("cli-output_windows.txt").toURI())))
expected = Files.readString(Paths.get(Objects.requireNonNull(getClass().getResource("cli-output_windows.txt")).toURI()))
.replaceAll("\\r\\n?", "\n");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoSession;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.stubbing.Answer;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

Expand Down Expand Up @@ -91,8 +91,8 @@ public void testGetDecayedAmount() {
}

@Nested
@ExtendWith(MockitoExtension.class)
public class BurnShareTest {
private MockitoSession mockitoSession;
@Mock
private DaoStateService daoStateService;
@Mock
Expand All @@ -104,18 +104,12 @@ public class BurnShareTest {

@BeforeEach
public void setUp() {
mockitoSession = Mockito.mockitoSession().initMocks(this).startMocking();
when(cyclesInDaoStateService.getChainHeightOfPastCycle(800000, BurningManService.NUM_CYCLES_BURN_AMOUNT_DECAY))
.thenReturn(750000);
when(cyclesInDaoStateService.getChainHeightOfPastCycle(800000, BurningManService.NUM_CYCLES_COMP_REQUEST_DECAY))
.thenReturn(700000);
}

@AfterEach
public void tearDown() {
mockitoSession.finishMocking();
}

private void addProofOfBurnTxs(Tx... txs) {
var txsById = Arrays.stream(txs)
.collect(Collectors.toMap(Tx::getId, tx -> tx));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,22 @@
import java.util.Collections;

import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoSession;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import static org.mockito.Mockito.*;


/**
* Tests of the P2PDataStorage::onRemoved callback behavior to ensure that the proper number of signal events occur.
*/
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT) // the two stubs in setUp() are not used in every test
public class ProposalServiceP2PDataStorageListenerTest {
private MockitoSession mockitoSession;

private ProposalService proposalService;
@Mock
private PeriodService periodService;
Expand All @@ -63,11 +62,6 @@ public class ProposalServiceP2PDataStorageListenerTest {

@BeforeEach
public void setUp() {
mockitoSession = Mockito.mockitoSession()
.initMocks(this)
.strictness(Strictness.LENIENT) // the two stubs below are not used in every test
.startMocking();

this.proposalService = new ProposalService(
mock(P2PService.class),
this.periodService,
Expand All @@ -83,11 +77,6 @@ public void setUp() {
when(this.daoStateService.isParseBlockChainComplete()).thenReturn(false);
}

@AfterEach
public void tearDown() {
mockitoSession.finishMocking();
}

private static ProtectedStorageEntry buildProtectedStorageEntry() {
ProtectedStorageEntry protectedStorageEntry = mock(ProtectedStorageEntry.class);
TempProposalPayload tempProposalPayload = mock(TempProposalPayload.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import java.util.Objects;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand All @@ -52,7 +54,6 @@
import com.googlecode.jsonrpc4j.HttpException;
import com.googlecode.jsonrpc4j.JsonRpcClientException;
import com.googlecode.jsonrpc4j.RequestIDGenerator;
import kotlin.text.Charsets;

public class BitcoindClientTest {
private static final String TEST_BLOCK_HASH = "015f37a20d517645a11a6cdd316049f41bc77b4a4057b2dd092114b78147f42c";
Expand All @@ -66,7 +67,7 @@ public class BitcoindClientTest {
private boolean canConnect = true;
private ByteArrayInputStream mockResponse;
private ByteArrayInputStream mockErrorResponse;
private ByteArrayOutputStream mockOutputStream = new ByteArrayOutputStream();
private final ByteArrayOutputStream mockOutputStream = new ByteArrayOutputStream();

@BeforeEach
public void setUp() throws Exception {
Expand Down Expand Up @@ -116,7 +117,7 @@ public void testGetBlockCount_noConnection() {
}

@Test
public void testGetBlockCount_wrongCredentials() throws Exception {
public void testGetBlockCount_wrongCredentials() {
mockResponseCode = 401;
// mockResponseCustomHeaders.put("WWW-Authenticate", "[Basic realm=\"jsonrpc\"]");
assertThrows(HttpException.class, () -> client.getBlockCount());
Expand Down Expand Up @@ -218,8 +219,8 @@ private static ByteArrayInputStream toJsonIS(String json) {

private static String readFromResourcesUnPrettified(String resourceName) {
try {
var path = Paths.get(BitcoindClientTest.class.getResource(resourceName).toURI());
return new String(Files.readAllBytes(path), Charsets.UTF_8).replaceAll("(\\s+\\B|\\B\\s+|\\v)", "");
var path = Paths.get(Objects.requireNonNull(BitcoindClientTest.class.getResource(resourceName)).toURI());
return Files.readString(path).replaceAll("(\\s+\\B|\\B\\s+|\\v)", "");
} catch (Exception e) {
return "";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@
import java.net.URL;

import java.nio.file.Files;
import java.nio.file.Path;

import java.io.IOException;
import java.nio.file.Paths;

import java.util.List;
import java.util.Objects;
Expand Down Expand Up @@ -225,19 +223,12 @@ void responseHasDifferentTxId() {
}

public static String getValidBtcMakerFeeMempoolJsonResponseString() {
URL resource = MakerTxValidatorSanityCheckTests.class.getClassLoader()
.getResource("mempool_test_data/valid_btc_maker_fee.json");
String path = Objects.requireNonNull(resource).getPath();

if (System.getProperty("os.name").toLowerCase().startsWith("win")) {
// We need to remove the first character on Windows because the path starts with a
// leading slash "/C:/Users/..."
path = path.substring(1);
}

try {
return Files.readString(Path.of(path));
} catch (IOException e) {
URL resource = MakerTxValidatorSanityCheckTests.class.getClassLoader()
.getResource("mempool_test_data/valid_btc_maker_fee.json");
var path = Paths.get(Objects.requireNonNull(resource).toURI());
return Files.readString(path);
} catch (Exception e) {
throw new IllegalStateException("Couldn't read valid_btc_maker_fee.json.", e);
}
}
Expand Down
Loading
Loading