@Test public void toStringReturnsCorrectRepresentationWhenStatusCodeIsUnknown() { // Assert: Assert.assertThat( "Http Status Code -123: badness", IsEqual.equalTo(new ErrorResponse(new TimeInstant(4), "badness", -123).toString())); Assert.assertThat( "Http Status Code -123", IsEqual.equalTo(new ErrorResponse(new TimeInstant(4), null, -123).toString())); }
@Test public void canBeCreatedAroundMessage() { // Arrange: final ErrorResponse response = new ErrorResponse(new TimeInstant(29), "badness", 500); // Assert: Assert.assertThat(response.getTimeStamp(), IsEqual.equalTo(new TimeInstant(29))); Assert.assertThat(response.getError(), IsEqual.equalTo("Internal Server Error")); Assert.assertThat(response.getMessage(), IsEqual.equalTo("badness")); Assert.assertThat(response.getStatus(), IsEqual.equalTo(500)); }
@Test public void canBeCreatedAroundUnknownHttpStatus() { // Arrange: final ErrorResponse response = new ErrorResponse(new TimeInstant(18), "exception message", -123); // Assert: Assert.assertThat(response.getTimeStamp(), IsEqual.equalTo(new TimeInstant(18))); Assert.assertThat(response.getError(), IsNull.nullValue()); Assert.assertThat(response.getMessage(), IsEqual.equalTo("exception message")); Assert.assertThat(response.getStatus(), IsEqual.equalTo(-123)); }
@Test public void responseWithoutDescriptionsCanBeRoundTripped() { // Act: final ErrorResponse response = createRoundTrippedResponse(new ErrorResponse(new TimeInstant(54), null, 890)); // Assert: Assert.assertThat(response.getTimeStamp(), IsEqual.equalTo(new TimeInstant(54))); Assert.assertThat(response.getError(), IsNull.nullValue()); Assert.assertThat(response.getMessage(), IsNull.nullValue()); Assert.assertThat(response.getStatus(), IsEqual.equalTo(890)); }
@Test public void responseCanBeRoundTripped() { // Act: final ErrorResponse response = createRoundTrippedResponse(new ErrorResponse(new TimeInstant(18), "badness", 500)); // Assert: Assert.assertThat(response.getTimeStamp(), IsEqual.equalTo(new TimeInstant(18))); Assert.assertThat(response.getError(), IsEqual.equalTo("Internal Server Error")); Assert.assertThat(response.getMessage(), IsEqual.equalTo("badness")); Assert.assertThat(response.getStatus(), IsEqual.equalTo(500)); }
/** Example test that tests a specific DoFn. */ @Test public void testExtractWordsFn() { DoFnTester<String, String> extractWordsFn = DoFnTester.of(new ExtractWordsFn()); Assert.assertThat(extractWordsFn.processBatch(" some input words "), CoreMatchers.hasItems("some", "input", "words")); Assert.assertThat(extractWordsFn.processBatch(" "), CoreMatchers.<String>hasItems()); Assert.assertThat(extractWordsFn.processBatch(" some ", " input", " words"), CoreMatchers.hasItems("some", "input", "words")); }
@Test public void iIsAsExpected() { // Arrange: final BigInteger I = new BigInteger( "19681161376707505956807079304988542015446066515923890162744021073123829784752"); // Assert (i^2 == -1): Assert.assertThat(I, IsEqual.equalTo(MathUtils.toBigInteger(Ed25519Field.I))); Assert.assertThat( I.multiply(I).mod(Ed25519Field.P), IsEqual.equalTo(BigInteger.ONE.shiftLeft(255).subtract(new BigInteger("20")))); }
@Test public void canBeCreatedAroundException() { // Arrange: final ErrorResponse response = new ErrorResponse( new TimeInstant(18), new RuntimeException("exception message"), HttpStatus.NOT_FOUND); // Assert: Assert.assertThat(response.getTimeStamp(), IsEqual.equalTo(new TimeInstant(18))); Assert.assertThat(response.getError(), IsEqual.equalTo("Not Found")); Assert.assertThat(response.getMessage(), IsEqual.equalTo("exception message")); Assert.assertThat(response.getStatus(), IsEqual.equalTo(404)); }
@Test public void deleteDeletesUnderlyingStorableEntityFile() throws IOException { // Arrange: final File file = new File(TEST_FILE_DIRECTORY, "to-be-deleted.bar"); final TEntityFileDescriptor descriptor = this.createDescriptor(file); Assert.assertThat(file.createNewFile(), IsEqual.equalTo(true)); // Act: descriptor.delete(); // Assert: Assert.assertThat(file.exists(), IsEqual.equalTo(false)); }
@Test public void addressConstantIsConsistentWithNemesisBlock() { // Arrange: final Block block = this.loadNemesisBlock(); final Address blockAddress = block.getSigner().getAddress(); // Assert: Assert.assertThat(blockAddress, IsEqual.equalTo(NEMESIS_BLOCK_INFO.getAddress())); Assert.assertThat( blockAddress.getPublicKey(), IsEqual.equalTo(NEMESIS_BLOCK_INFO.getAddress().getPublicKey())); Assert.assertThat(blockAddress.getPublicKey(), IsNull.notNullValue()); }
@Test public void copyToCopiesBytesToGivenOutputStream() { // Arrange: final TestContext context = new TestContext(); final WalletNamePasswordPair pair = createPair("n", "p"); final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); Mockito.when(context.descriptor.openRead(StorableEntityReadMode.Raw)) .thenReturn(new ByteArrayInputStream("test".getBytes())); // Act: context.walletServices.copyTo(pair, outputStream); // Assert: Assert.assertThat(outputStream.size(), IsEqual.equalTo(4)); Assert.assertThat(outputStream.toString(), IsEqual.equalTo("test")); }
@Test public void canChangeWalletNameAndPasswordIfWalletIsAlreadyOpen() { // Arrange: final WalletNamePasswordPair pair2 = createPair("n2", "p2"); final WalletDescriptor descriptor2 = createDescriptor("n2"); final TestContext context = new TestContext(); Mockito.when(context.descriptorFactory.createNew(pair2, FILE_EXTENSION)) .thenReturn(descriptor2); // Act: context.walletServices.open(context.pair); context.walletServices.move(context.pair, pair2); // Assert: // - the original is opened and the target is created Mockito.verify(context.descriptorFactory, Mockito.times(2)) .openExisting(context.pair, FILE_EXTENSION); Mockito.verify(context.descriptorFactory, Mockito.times(1)).createNew(pair2, FILE_EXTENSION); // - the original is loaded Mockito.verify(context.repository, Mockito.times(2)).load(context.descriptor); // - the target is saved final Wallet updatedWallet = context.walletServices.get(new WalletName("n2")); Assert.assertThat(updatedWallet.getName(), IsEqual.equalTo(new WalletName("n2"))); // TODO BR: Fix this // Mockito.verify(context.repository, Mockito.times(1)).save(descriptor2, updatedWallet); Mockito.verify(context.repository, Mockito.times(1)).save(Mockito.any(), Mockito.any()); // - the original wallet is deleted Mockito.verify(context.descriptor, Mockito.times(1)).delete(); }
@Test public void responseCanBeSerialized() { // Arrange: final JsonSerializer serializer = new JsonSerializer(); final ErrorResponse response = new ErrorResponse(new TimeInstant(18), "badness", 500); // Act: response.serialize(serializer); final JSONObject jsonObject = serializer.getObject(); // Assert: Assert.assertThat(jsonObject.get("timeStamp"), IsEqual.equalTo(18)); Assert.assertThat(jsonObject.get("error"), IsEqual.equalTo("Internal Server Error")); Assert.assertThat(jsonObject.get("message"), IsEqual.equalTo("badness")); Assert.assertThat(jsonObject.get("status"), IsEqual.equalTo(500)); }
@Test public void descriptorCanBeSerialized() { // Arrange: final File file = new File(Paths.get(TEST_FILE_DIRECTORY.toString(), "blah").toString(), "foo.bar"); final TEntityFileDescriptor descriptor = this.createDescriptor(file); // Act: final JSONObject jsonObject = JsonSerializer.serializeToJson(descriptor); // Assert: Assert.assertThat(jsonObject.size(), IsEqual.equalTo(2)); Assert.assertThat(jsonObject.get(descriptor.getName().getLabel()), IsEqual.equalTo("foo")); Assert.assertThat( jsonObject.get("location"), IsEqual.equalTo(Paths.get(TEST_FILE_DIRECTORY.toString(), "blah", "foo.bar").toString())); }
@Test public void descriptorCanBeCreatedAroundStorableEntityWithMixedCaseExtension() { // Arrange: final File file = new File(Paths.get(TEST_FILE_DIRECTORY.toString(), "BlAh").toString(), "FoO.BaR"); // Act: final TEntityFileDescriptor descriptor = this.createDescriptor(file); // Assert: Assert.assertThat(descriptor.getName(), IsEqual.equalTo(this.createEntityName("FoO"))); Assert.assertThat( descriptor.getFileExtension(), IsEqual.equalTo(this.createEntityFileExtension(".BaR"))); Assert.assertThat( descriptor.getStorableEntityLocation(), IsEqual.equalTo(Paths.get(TEST_FILE_DIRECTORY.toString(), "BlAh", "FoO.BaR").toString())); }
@Test public void pIsAsExpected() { // Arrange: final BigInteger P = BigInteger.ONE.shiftLeft(255).subtract(new BigInteger("19")); // Assert: Assert.assertThat(P, IsEqual.equalTo(Ed25519Field.P)); }
@Test public void openLoadsWalletIfWalletIsNotOpened() { // Arrange: final TestContext context = new TestContext(); // Act: final Wallet wallet = context.walletServices.open(context.pair); // Assert: Assert.assertThat(wallet.getName(), IsEqual.equalTo(context.originalWallet.getName())); Assert.assertThat( context.walletServices.getOpenWalletNames(), IsEquivalent.equivalentTo(Collections.singletonList(context.originalWallet.getName()))); Mockito.verify(context.descriptorFactory, Mockito.times(1)) .openExisting(context.pair, FILE_EXTENSION); Mockito.verify(context.repository, Mockito.times(1)).load(context.descriptor); }
@Test public void nemesisBlockIsVerifiable() { // Arrange: final Block block = this.loadNemesisBlock(); // Assert: Assert.assertThat(block.verify(), IsEqual.equalTo(true)); }
@Test public void generationHashConstantIsConsistentWithNemesisBlock() { // Arrange: final Block block = this.loadNemesisBlock(); // Assert: Assert.assertThat( block.getGenerationHash(), IsEqual.equalTo(NEMESIS_BLOCK_INFO.getGenerationHash())); }
@Test public void shouldReturnUnparsableClassIfErrorOccursWhileParsing() { ClassParser parser = mock(ClassParser.class); when(parser.getClass("MyClassName")).thenThrow(new RuntimeException(new NotFoundException(""))); builder = new JavaClassBuilder(parser); Assert.assertThat(builder.createClass("MyClassName"), instanceOf(UnparsableClass.class)); }
@Test public void nemesisTransactionsAreVerifiable() { // Arrange: final Block block = this.loadNemesisBlock(); // Assert: for (final Transaction transaction : block.getTransactions()) { Assert.assertThat(transaction.verify(), IsEqual.equalTo(true)); } }
@Test public void dIsAsExpected() { // Arrange: final BigInteger D = new BigInteger( "37095705934669439343138083508754565189542113879843219016388785533085940283555"); // Assert: Assert.assertThat(D, IsEqual.equalTo(MathUtils.toBigInteger(Ed25519Field.D))); }
@Test public void dTimesTwoIsAsExpected() { // Arrange: final BigInteger DTimesTwo = new BigInteger( "16295367250680780974490674513165176452449235426866156013048779062215315747161"); // Assert: Assert.assertThat(DTimesTwo, IsEqual.equalTo(MathUtils.toBigInteger(Ed25519Field.D_Times_TWO))); }
@Test public void openWriteCanOpenFileThatExists() throws IOException { // Arrange: final TEntityFileDescriptor descriptor = this.createDescriptor(TEST_FILE); // Act: try (final OutputStream os = descriptor.openWrite()) { // Assert: Assert.assertThat(os, IsNull.notNullValue()); } }
@Test public void getCanReturnOpenWallet() { // Arrange: final TestContext context = new TestContext(); // Act: final Wallet wallet1 = context.walletServices.open(context.pair); final Wallet wallet2 = context.walletServices.get(context.pair.getName()); // Assert: Assert.assertThat(wallet2, IsEqual.equalTo(wallet1)); }
@Test public void descriptorCanBeCreatedAroundUrlEncodedStorableEntityWithValidExtension() { // Arrange: final File file = new File( Paths.get(TEST_FILE_DIRECTORY.toString(), "blah").toString(), "%C3%B6%C3%A4%C3%BC%40.bar"); // Act: final TEntityFileDescriptor descriptor = this.createDescriptor(file); // Assert: Assert.assertThat(descriptor.getName(), IsEqual.equalTo(this.createEntityName("öäü@"))); Assert.assertThat( descriptor.getFileExtension(), IsEqual.equalTo(this.createEntityFileExtension(".bar"))); Assert.assertThat( descriptor.getStorableEntityLocation(), IsEqual.equalTo( Paths.get(TEST_FILE_DIRECTORY.toString(), "blah", "%C3%B6%C3%A4%C3%BC%40.bar") .toString())); }
@Test public void openWriteCanOpenFileThatDoesNotExist() throws IOException { // Arrange: final File file = new File(TEST_FILE_DIRECTORY, "imaginary-write.bar"); final TEntityFileDescriptor descriptor = this.createDescriptor(file); // Act: try (final OutputStream os = descriptor.openWrite()) { // Assert: Assert.assertThat(os, IsNull.notNullValue()); } }
@Test public void nemesisBlockCanBeCreated() { // Act: final Block block = this.loadNemesisBlock(); // Assert: Assert.assertThat( block.getSigner().getAddress(), IsEqual.equalTo(NEMESIS_BLOCK_INFO.getAddress())); Assert.assertThat(block.getType(), IsEqual.equalTo(-1)); Assert.assertThat(block.getVersion(), IsEqual.equalTo(EXPECTED_VERSION)); Assert.assertThat(block.getTimeStamp(), IsEqual.equalTo(TimeInstant.ZERO)); // 2 multisig aggregate transactions Assert.assertThat( block.getTotalFee(), IsEqual.equalTo(EXPECTED_MULTISIG_AGGREGATE_FEE.multiply(2))); Assert.assertThat(block.getPreviousBlockHash(), IsEqual.equalTo(Hash.ZERO)); Assert.assertThat(block.getHeight(), IsEqual.equalTo(BlockHeight.ONE)); Assert.assertThat(block.getTransactions().size(), IsEqual.equalTo(NUM_NEMESIS_TRANSACTIONS)); Assert.assertThat(block.getDifficulty(), IsEqual.equalTo(BlockDifficulty.INITIAL_DIFFICULTY)); Assert.assertThat(block.getGenerationHash(), IsNull.notNullValue()); }
@Test public void getStorableEntityLocationReturnsAbsolutePathToStorableEntity() { // Arrange: final File file = new File(Paths.get(TEST_FILE_DIRECTORY.toString(), "blah").toString(), "foo.bar"); final TEntityFileDescriptor descriptor = this.createDescriptor(file); // Act: final String path = descriptor.getStorableEntityLocation(); // Assert: Assert.assertThat(path, IsEqual.equalTo(file.getAbsolutePath())); }
@Test public void nemesisTransactionsHaveCorrectFees() { // Arrange: final Block block = this.loadNemesisBlock(); // Assert: for (final Transaction transaction : block.getTransactions()) { final Amount expectedFee = TransactionTypes.TRANSFER == transaction.getType() ? Amount.ZERO : NemGlobals.getTransactionFeeCalculator().calculateMinimumFee(transaction); Assert.assertThat(transaction.getFee(), IsEqual.equalTo(expectedFee)); } }