diff --git a/src/main/java/co/rsk/federate/signing/hsm/advanceBlockchain/ConfirmedBlockHeadersProvider.java b/src/main/java/co/rsk/federate/signing/hsm/advanceblockchain/ConfirmedBlockHeadersProvider.java similarity index 100% rename from src/main/java/co/rsk/federate/signing/hsm/advanceBlockchain/ConfirmedBlockHeadersProvider.java rename to src/main/java/co/rsk/federate/signing/hsm/advanceblockchain/ConfirmedBlockHeadersProvider.java diff --git a/src/main/java/co/rsk/federate/signing/hsm/advanceBlockchain/HSMBookeepingServiceListener.java b/src/main/java/co/rsk/federate/signing/hsm/advanceblockchain/HSMBookeepingServiceListener.java similarity index 100% rename from src/main/java/co/rsk/federate/signing/hsm/advanceBlockchain/HSMBookeepingServiceListener.java rename to src/main/java/co/rsk/federate/signing/hsm/advanceblockchain/HSMBookeepingServiceListener.java diff --git a/src/main/java/co/rsk/federate/signing/hsm/advanceBlockchain/HSMBookkeepingService.java b/src/main/java/co/rsk/federate/signing/hsm/advanceblockchain/HSMBookkeepingService.java similarity index 100% rename from src/main/java/co/rsk/federate/signing/hsm/advanceBlockchain/HSMBookkeepingService.java rename to src/main/java/co/rsk/federate/signing/hsm/advanceblockchain/HSMBookkeepingService.java diff --git a/src/main/java/co/rsk/federate/signing/hsm/client/HSMClientProvider.java b/src/main/java/co/rsk/federate/signing/hsm/client/HSMClientProvider.java index 435aa3b3..2a6b42ab 100644 --- a/src/main/java/co/rsk/federate/signing/hsm/client/HSMClientProvider.java +++ b/src/main/java/co/rsk/federate/signing/hsm/client/HSMClientProvider.java @@ -33,8 +33,6 @@ * @author Ariel Mendelzon */ public class HSMClientProvider { - private static final int MIN_SUPPORTED_VERSION = 1; - private static final int MAX_SUPPORTED_VERSION = 3; private static final Logger logger = LoggerFactory.getLogger(HSMClientProvider.class); private final HSMClientProtocol hsmClientProtocol; @@ -49,33 +47,35 @@ public HSMClient getClient() throws HSMClientException { int version = this.hsmClientProtocol.getVersion(); HSMClient client; logger.debug("[getClient] version: {}, keyId: {}", version, keyId); - switch (version) { - case 1: - client = new HSMClientVersion1(this.hsmClientProtocol); + if (version == 1) { + client = new HSMClientVersion1(this.hsmClientProtocol); + } else if (version >= 2) { + client = buildPowHSMClient(version); + } else { + String message = String.format("Unsupported HSM version %d", version); + logger.debug("[getClient] {}", message); + throw new HSMUnsupportedVersionException(message); + } + + logger.debug("[getClient] HSM client: {}", client.getClass()); + return client; + } + + private HSMClient buildPowHSMClient(int version) throws HSMUnsupportedTypeException { + HSMClient client; + switch (keyId) { + case "BTC": + client = new HSMClientVersion2BTC(this.hsmClientProtocol, version); break; - case 2: - case 3: - switch (keyId) { - case "BTC": - client = new HSMClientVersion2BTC(this.hsmClientProtocol, version); - break; - case "RSK": - case "MST": - client = new HSMClientVersion2RskMst(this.hsmClientProtocol, version); - break; - default: - String message = String.format("Unsupported key id %s", keyId); - logger.debug("[getClient] {}", message); - throw new HSMUnsupportedTypeException(message); - } + case "RSK": + case "MST": + client = new HSMClientVersion2RskMst(this.hsmClientProtocol, version); break; default: - String message = String.format("Unsupported HSM version %d, the node supports versions between %d and %d", version, MIN_SUPPORTED_VERSION, MAX_SUPPORTED_VERSION); + String message = String.format("Unsupported key id %s", keyId); logger.debug("[getClient] {}", message); - throw new HSMUnsupportedVersionException(message); + throw new HSMUnsupportedTypeException(message); } - - logger.debug("[getClient] HSM client: {}", client.getClass()); return client; } } diff --git a/src/main/java/co/rsk/federate/signing/hsm/client/HSMClientVersion2BTC.java b/src/main/java/co/rsk/federate/signing/hsm/client/HSMClientVersion2BTC.java index cb7a8abd..ecbeb6aa 100644 --- a/src/main/java/co/rsk/federate/signing/hsm/client/HSMClientVersion2BTC.java +++ b/src/main/java/co/rsk/federate/signing/hsm/client/HSMClientVersion2BTC.java @@ -137,11 +137,11 @@ protected void sendBlockHeadersChunks( blocksFieldData.add(blockHeader); } payload.set(BLOCKS_FIELD, blocksFieldData); - if (this.version == 3 && actualMethod.equals("advanceBlockchain")) { + if (this.version >= 3 && actualMethod.equals("advanceBlockchain")) { ArrayNode brothersFieldData = new ObjectMapper().createArrayNode(); for (String blockHeader : blockHeaderChunk) { - // TODO: This is currently sending empty arrays as brothers to the HSM V3 for compatibility with V2 - // This should be changed to sending the actual brothers when HSM V3 is fully implemented + // TODO: This is currently sending empty arrays as brothers to the HSM V3, V4 for compatibility with V2 + // This should be changed to sending the actual brothers when HSM V3, V4 is fully implemented brothersFieldData.add(new ObjectMapper().createArrayNode()); } payload.set(BROTHERS_FIELD, brothersFieldData); diff --git a/src/main/java/co/rsk/federate/signing/hsm/message/ReleaseCreationInformationGetter.java b/src/main/java/co/rsk/federate/signing/hsm/message/ReleaseCreationInformationGetter.java index 299d69f9..9eda22a7 100644 --- a/src/main/java/co/rsk/federate/signing/hsm/message/ReleaseCreationInformationGetter.java +++ b/src/main/java/co/rsk/federate/signing/hsm/message/ReleaseCreationInformationGetter.java @@ -3,10 +3,6 @@ import co.rsk.bitcoinj.core.BtcTransaction; import co.rsk.crypto.Keccak256; import co.rsk.peg.BridgeEvents; -import java.util.Arrays; -import java.util.List; -import java.util.Optional; -import java.util.stream.Collectors; import org.ethereum.core.Block; import org.ethereum.core.CallTransaction; import org.ethereum.core.Transaction; @@ -18,6 +14,11 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.Arrays; +import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; + // First the class tries to find the event associated with the transaction. If it cannot find it, it requests the following // events until it is found or until it reaches the last block. public class ReleaseCreationInformationGetter { @@ -53,14 +54,12 @@ public ReleaseCreationInformation getTxInfoToSign( BtcTransaction btcTransaction, Keccak256 informingRskTxHash ) throws HSMReleaseCreationInformationException { - switch (version) { - case 1: - return getBaseReleaseCreationInformation(rskTxHash, btcTransaction, informingRskTxHash); - case 2: - case 3: - return getTxInfoToSignVersion2(rskTxHash, btcTransaction, informingRskTxHash); - default: - throw new HSMReleaseCreationInformationException("Unsupported version " + version); + if (version == 1) { + return getBaseReleaseCreationInformation(rskTxHash, btcTransaction, informingRskTxHash); + } else if (version >= 2) { + return getTxInfoToSignVersion2(rskTxHash, btcTransaction, informingRskTxHash); + } else { + throw new HSMReleaseCreationInformationException("Unsupported version " + version); } } diff --git a/src/main/java/co/rsk/federate/signing/hsm/message/SignerMessageBuilderFactory.java b/src/main/java/co/rsk/federate/signing/hsm/message/SignerMessageBuilderFactory.java index 899d37f4..2b5db4df 100644 --- a/src/main/java/co/rsk/federate/signing/hsm/message/SignerMessageBuilderFactory.java +++ b/src/main/java/co/rsk/federate/signing/hsm/message/SignerMessageBuilderFactory.java @@ -19,20 +19,15 @@ public SignerMessageBuilder buildFromConfig( ReleaseCreationInformation releaseCreationInformation ) throws HSMUnsupportedVersionException { SignerMessageBuilder messageBuilder; - switch (version) { - case 1: - messageBuilder = new SignerMessageBuilderVersion1(releaseCreationInformation.getBtcTransaction()); - break; - case 2: - case 3: - messageBuilder = new SignerMessageBuilderVersion2(receiptStore, releaseCreationInformation); - break; - default: - String message = String.format("Unsupported HSM signer version: %d", version); - logger.debug("[buildFromConfig] {}", message); - throw new HSMUnsupportedVersionException(message); + if (version == 1) { + messageBuilder = new SignerMessageBuilderVersion1(releaseCreationInformation.getBtcTransaction()); + } else if (version >= 2) { + messageBuilder = new SignerMessageBuilderVersion2(receiptStore, releaseCreationInformation); + } else { + String message = String.format("Unsupported HSM signer version: %d", version); + logger.debug("[buildFromConfig] {}", message); + throw new HSMUnsupportedVersionException(message); } - logger.trace("[buildFromConfig] SignerMessageBuilder built {}", messageBuilder.getClass()); return messageBuilder; } diff --git a/src/main/java/co/rsk/federate/signing/hsm/requirements/ReleaseRequirementsEnforcer.java b/src/main/java/co/rsk/federate/signing/hsm/requirements/ReleaseRequirementsEnforcer.java index 95b34056..81b47b10 100644 --- a/src/main/java/co/rsk/federate/signing/hsm/requirements/ReleaseRequirementsEnforcer.java +++ b/src/main/java/co/rsk/federate/signing/hsm/requirements/ReleaseRequirementsEnforcer.java @@ -15,24 +15,23 @@ public ReleaseRequirementsEnforcer(AncestorBlockUpdater ancestorBlockUpdater) { public void enforce(int version, ReleaseCreationInformation releaseCreationInformation) throws ReleaseRequirementsEnforcerException { - switch (version) { - case 1: - logger.trace("[enforce] Version 1 doesn't have release requirements to enforce"); - return; - case 2: - case 3: - logger.trace("[enforce] Version 2 or 3 requires ancestor in position. ENFORCING"); - try { - ancestorBlockUpdater.ensureAncestorBlockInPosition(releaseCreationInformation.getBlock()); - return; - } catch (Exception e) { - String message = "error trying to enforce ancestor"; - logger.error("[enforce]" + message, e); - throw new ReleaseRequirementsEnforcerException(message, e); - } - default: - throw new ReleaseRequirementsEnforcerException("Unsupported version " + version); + if (version == 1) { + logger.trace("[enforce] Version 1 doesn't have release requirements to enforce"); + } else if (version >= 2) { + logger.trace("[enforce] Version 2+ requires ancestor in position. ENFORCING"); + enforceReleaseRequirements(releaseCreationInformation); + } else { + throw new ReleaseRequirementsEnforcerException("Unsupported version " + version); } } + private void enforceReleaseRequirements(ReleaseCreationInformation releaseCreationInformation) throws ReleaseRequirementsEnforcerException { + try { + ancestorBlockUpdater.ensureAncestorBlockInPosition(releaseCreationInformation.getBlock()); + } catch (Exception e) { + String message = "error trying to enforce ancestor"; + logger.error("[enforce]" + message, e); + throw new ReleaseRequirementsEnforcerException(message, e); + } + } } diff --git a/src/test/java/co/rsk/federate/signing/hsm/advanceBlockchain/ConfirmedBlockHeadersProviderTest.java b/src/test/java/co/rsk/federate/signing/hsm/advanceblockchain/ConfirmedBlockHeadersProviderTest.java similarity index 100% rename from src/test/java/co/rsk/federate/signing/hsm/advanceBlockchain/ConfirmedBlockHeadersProviderTest.java rename to src/test/java/co/rsk/federate/signing/hsm/advanceblockchain/ConfirmedBlockHeadersProviderTest.java diff --git a/src/test/java/co/rsk/federate/signing/hsm/advanceBlockchain/HSMBookkeepingServiceTest.java b/src/test/java/co/rsk/federate/signing/hsm/advanceblockchain/HSMBookkeepingServiceTest.java similarity index 100% rename from src/test/java/co/rsk/federate/signing/hsm/advanceBlockchain/HSMBookkeepingServiceTest.java rename to src/test/java/co/rsk/federate/signing/hsm/advanceblockchain/HSMBookkeepingServiceTest.java diff --git a/src/test/java/co/rsk/federate/signing/hsm/client/HSMClientProviderTest.java b/src/test/java/co/rsk/federate/signing/hsm/client/HSMClientProviderTest.java index ddc6678d..555e75c4 100644 --- a/src/test/java/co/rsk/federate/signing/hsm/client/HSMClientProviderTest.java +++ b/src/test/java/co/rsk/federate/signing/hsm/client/HSMClientProviderTest.java @@ -104,11 +104,44 @@ public void getClientV3MST() throws Exception { Assert.assertTrue(client instanceof HSMClientVersion2RskMst); } + @Test + public void getClientV4BTC() throws Exception { + HSMClientProtocol protocol = mock(HSMClientProtocol.class); + when(protocol.getVersion()).thenReturn(4); + + HSMClientProvider clientProvider = new HSMClientProvider(protocol, "BTC"); + HSMClient client = clientProvider.getClient(); + + Assert.assertTrue(client instanceof HSMClientVersion2BTC); + } + + @Test + public void getClientV4RSK() throws Exception { + HSMClientProtocol protocol = mock(HSMClientProtocol.class); + when(protocol.getVersion()).thenReturn(4); + + HSMClientProvider clientProvider = new HSMClientProvider(protocol, "RSK"); + HSMClient client = clientProvider.getClient(); + + Assert.assertTrue(client instanceof HSMClientVersion2RskMst); + } + + @Test + public void getClientV4MST() throws Exception { + HSMClientProtocol protocol = mock(HSMClientProtocol.class); + when(protocol.getVersion()).thenReturn(4); + + HSMClientProvider clientProvider = new HSMClientProvider(protocol, "MST"); + HSMClient client = clientProvider.getClient(); + + Assert.assertTrue(client instanceof HSMClientVersion2RskMst); + } + @Test(expected = HSMUnsupportedVersionException.class) public void getClientUnsupportedVersion() throws Exception { HSMClientProtocol protocol = mock(HSMClientProtocol.class); HSMClientProvider clientProvider = new HSMClientProvider(protocol, "BTC"); - when(protocol.getVersion()).thenReturn(999); + when(protocol.getVersion()).thenReturn(-5); clientProvider.getClient(); } diff --git a/src/test/java/co/rsk/federate/signing/hsm/client/HSMClientVersion2BTCTest.java b/src/test/java/co/rsk/federate/signing/hsm/client/HSMClientVersion2BTCTest.java index 0f0c25fd..00133bcd 100644 --- a/src/test/java/co/rsk/federate/signing/hsm/client/HSMClientVersion2BTCTest.java +++ b/src/test/java/co/rsk/federate/signing/hsm/client/HSMClientVersion2BTCTest.java @@ -31,7 +31,6 @@ import static org.mockito.Mockito.*; public class HSMClientVersion2BTCTest { - private JsonRpcClientProvider jsonRpcClientProviderMock; private HSMClientProtocol hsmClientProtocol; private JsonRpcClient jsonRpcClientMock; private HSMClientVersion2BTC client; @@ -40,7 +39,7 @@ public class HSMClientVersion2BTCTest { @Before public void createClient() throws JsonRpcException { - jsonRpcClientProviderMock = mock(JsonRpcClientProvider.class); + JsonRpcClientProvider jsonRpcClientProviderMock = mock(JsonRpcClientProvider.class); jsonRpcClientMock = mock(JsonRpcClient.class); when(jsonRpcClientProviderMock.acquire()).thenReturn(jsonRpcClientMock); @@ -418,37 +417,25 @@ public void sendBlockHeadersChunks_keepPreviousChunkLastItem_false() throws Json @Test public void updateAncestorBlock_ok() throws HSMClientException, JsonRpcException { - when(jsonRpcClientMock.send(any(JsonNode.class))).thenReturn(buildResponse(0)); - when(jsonRpcClientMock.send(hsmClientProtocol.buildCommand("blockchainState", 2))) - .thenReturn(buildResponse(0, "state", buildStateResponse(false))); - - client.setMaxChunkSizeToHsm(2); - - BlockHeader blockHeader = mock(BlockHeader.class); - when(blockHeader.getEncoded(true, false)).thenReturn(new byte[]{}); - List blockHeaders = Arrays.asList(blockHeader, blockHeader, blockHeader); - - client.updateAncestorBlock(new UpdateAncestorBlockMessage(blockHeaders)); - ArgumentCaptor captor = ArgumentCaptor.forClass(JsonNode.class); - verify(jsonRpcClientMock, times(3)).send(captor.capture()); - List capturedArguments = captor.getAllValues(); - Assert.assertEquals("blockchainState", capturedArguments.get(0).get("command").asText()); - Assert.assertEquals("updateAncestorBlock", capturedArguments.get(1).get("command").asText()); - Assert.assertEquals("updateAncestorBlock", capturedArguments.get(2).get("command").asText()); + // hsm version 2 + test_updateAncestorBlock(2); + // hsm version 3 + test_updateAncestorBlock(3); + // hsm version 4 + test_updateAncestorBlock(4); } - @Test - public void updateAncestorBlock_hsm_version_3() throws HSMClientException, JsonRpcException { + public void test_updateAncestorBlock(int hsmVersion) throws HSMClientException, JsonRpcException { JsonRpcClientProvider jsonRpcClientProvider = mock(JsonRpcClientProvider.class); JsonRpcClient jsonRpcClient = mock(JsonRpcClient.class); when(jsonRpcClientProvider.acquire()).thenReturn(jsonRpcClient); HSMClientProtocol hsmClientProtocol = new HSMClientProtocol(jsonRpcClientProvider, ECDSASignerFactory.DEFAULT_ATTEMPTS, ECDSASignerFactory.DEFAULT_INTERVAL); - HSMClientVersion2BTC client = new HSMClientVersion2BTC(hsmClientProtocol, 3); + HSMClientVersion2BTC client = new HSMClientVersion2BTC(hsmClientProtocol, hsmVersion); client.setMaxChunkSizeToHsm(2); when(jsonRpcClient.send(any(JsonNode.class))).thenReturn(buildResponse(0)); - when(jsonRpcClient.send(hsmClientProtocol.buildCommand("blockchainState", 3))) + when(jsonRpcClient.send(hsmClientProtocol.buildCommand("blockchainState", hsmVersion))) .thenReturn(buildResponse(0, "state", buildStateResponse(false))); BlockHeader blockHeader = mock(BlockHeader.class); diff --git a/src/test/java/co/rsk/federate/signing/hsm/message/ReleaseCreationInformationGetterTest.java b/src/test/java/co/rsk/federate/signing/hsm/message/ReleaseCreationInformationGetterTest.java index b62b7ee7..4eba02b1 100644 --- a/src/test/java/co/rsk/federate/signing/hsm/message/ReleaseCreationInformationGetterTest.java +++ b/src/test/java/co/rsk/federate/signing/hsm/message/ReleaseCreationInformationGetterTest.java @@ -71,6 +71,9 @@ public void createGetTxInfoToSign_returnOK() throws HSMReleaseCreationInformatio // HSM V3 createGetTxInfoToSign_returnOK(information, rskTxHash, btcTransaction, block, transactionReceipt, 3); + + // HSM V4 + createGetTxInfoToSign_returnOK(information, rskTxHash, btcTransaction, block, transactionReceipt, 4); } private void createGetTxInfoToSign_returnOK(ReleaseCreationInformationGetter information, diff --git a/src/test/java/co/rsk/federate/signing/hsm/message/SignerMessageBuilderFactoryTest.java b/src/test/java/co/rsk/federate/signing/hsm/message/SignerMessageBuilderFactoryTest.java index ab2b4bcf..f6492d85 100644 --- a/src/test/java/co/rsk/federate/signing/hsm/message/SignerMessageBuilderFactoryTest.java +++ b/src/test/java/co/rsk/federate/signing/hsm/message/SignerMessageBuilderFactoryTest.java @@ -25,7 +25,7 @@ public void createFactory() { @Test(expected = HSMUnsupportedVersionException.class) public void buildWithWrongVersion() throws HSMClientException { - factory.buildFromConfig(4, mock(ReleaseCreationInformation.class)); + factory.buildFromConfig(-5, mock(ReleaseCreationInformation.class)); } @Test @@ -36,23 +36,22 @@ public void buildFromHSMVersion1() throws HSMClientException { @Test public void buildFromConfig_hsm_2_ok() throws HSMClientException { - SignerMessageBuilder messageBuilder = factory.buildFromConfig( - 2, - new ReleaseCreationInformation( - TestUtils.mockBlock(1), - mock(TransactionReceipt.class), - Keccak256.ZERO_HASH, - mock(BtcTransaction.class), - createHash(1) - ) - ); - assertTrue(messageBuilder instanceof SignerMessageBuilderVersion2); + test_buildFromConfig_hsm(2); } @Test public void buildFromConfig_hsm_3_ok() throws HSMClientException { + test_buildFromConfig_hsm(3); + } + + @Test + public void buildFromConfig_hsm_4_ok() throws HSMClientException { + test_buildFromConfig_hsm(4); + } + + public void test_buildFromConfig_hsm(int version) throws HSMUnsupportedVersionException { SignerMessageBuilder messageBuilder = factory.buildFromConfig( - 3, + version, new ReleaseCreationInformation( TestUtils.mockBlock(1), mock(TransactionReceipt.class), diff --git a/src/test/java/co/rsk/federate/signing/hsm/requirements/ReleaseRequirementsEnforcerTest.java b/src/test/java/co/rsk/federate/signing/hsm/requirements/ReleaseRequirementsEnforcerTest.java index dd5689a7..c568871e 100644 --- a/src/test/java/co/rsk/federate/signing/hsm/requirements/ReleaseRequirementsEnforcerTest.java +++ b/src/test/java/co/rsk/federate/signing/hsm/requirements/ReleaseRequirementsEnforcerTest.java @@ -1,20 +1,25 @@ package co.rsk.federate.signing.hsm.requirements; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.doThrow; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; - import co.rsk.federate.signing.hsm.message.ReleaseCreationInformation; +import org.junit.Before; import org.junit.Test; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.*; + public class ReleaseRequirementsEnforcerTest { + private AncestorBlockUpdater ancestorBlockUpdater; + private ReleaseRequirementsEnforcer enforcer; + + @Before + public void setup() { + ancestorBlockUpdater = mock(AncestorBlockUpdater.class); + enforcer = new ReleaseRequirementsEnforcer(ancestorBlockUpdater); + } + @Test - public void enforce_does_nothing_if_version_one() - throws Exception { + public void enforce_does_nothing_if_version_one() throws Exception { AncestorBlockUpdater ancestorBlockUpdater = mock(AncestorBlockUpdater.class); ReleaseRequirementsEnforcer enforcer = new ReleaseRequirementsEnforcer(ancestorBlockUpdater); @@ -24,19 +29,28 @@ public void enforce_does_nothing_if_version_one() } @Test - public void enforce_version_two_ok() - throws Exception { - AncestorBlockUpdater ancestorBlockUpdater = mock(AncestorBlockUpdater.class); - ReleaseRequirementsEnforcer enforcer = new ReleaseRequirementsEnforcer(ancestorBlockUpdater); + public void enforce_version_two_ok() throws Exception { + test_enforce_version(ancestorBlockUpdater, enforcer, 2); + } - enforcer.enforce(2, mock(ReleaseCreationInformation.class)); + @Test + public void enforce_version_three_ok() throws Exception { + test_enforce_version(ancestorBlockUpdater, enforcer, 3); + } + + @Test + public void enforce_version_four_ok() throws Exception { + test_enforce_version(ancestorBlockUpdater, enforcer, 4); + } + + public void test_enforce_version(AncestorBlockUpdater ancestorBlockUpdater, ReleaseRequirementsEnforcer enforcer, int version) throws Exception { + enforcer.enforce(version, mock(ReleaseCreationInformation.class)); verify(ancestorBlockUpdater, times(1)).ensureAncestorBlockInPosition(any()); } @Test(expected = ReleaseRequirementsEnforcerException.class) - public void enforce_version_two_updater_fails() - throws Exception { + public void enforce_version_two_updater_fails() throws Exception { AncestorBlockUpdater ancestorBlockUpdater = mock(AncestorBlockUpdater.class); doThrow(new Exception()).when(ancestorBlockUpdater).ensureAncestorBlockInPosition(any()); ReleaseRequirementsEnforcer enforcer = new ReleaseRequirementsEnforcer(ancestorBlockUpdater); @@ -44,24 +58,10 @@ public void enforce_version_two_updater_fails() enforcer.enforce(2, mock(ReleaseCreationInformation.class)); } - @Test - public void enforce_version_three_ok() - throws Exception { - AncestorBlockUpdater ancestorBlockUpdater = mock(AncestorBlockUpdater.class); - ReleaseRequirementsEnforcer enforcer = new ReleaseRequirementsEnforcer(ancestorBlockUpdater); - - enforcer.enforce(3, mock(ReleaseCreationInformation.class)); - - verify(ancestorBlockUpdater, times(1)).ensureAncestorBlockInPosition(any()); - } - @Test(expected = ReleaseRequirementsEnforcerException.class) - public void enforce_invalid_version() - throws Exception { - ReleaseRequirementsEnforcer enforcer = new ReleaseRequirementsEnforcer( - mock(AncestorBlockUpdater.class) - ); + public void enforce_invalid_version() throws Exception { + ReleaseRequirementsEnforcer enforcer = new ReleaseRequirementsEnforcer(mock(AncestorBlockUpdater.class)); - enforcer.enforce(4, mock(ReleaseCreationInformation.class)); + enforcer.enforce(-5, mock(ReleaseCreationInformation.class)); } }