public List<KeyPairs> fromCSV(InputStream inputStream) throws Exception { Reader reader = new InputStreamReader(inputStream); Iterable<CSVRecord> records = CSVFormat.RFC4180.withFirstRecordAsHeader().parse(reader); Iterable<CSVRecord> iterable = () -> records.iterator(); Stream<CSVRecord> stream = StreamSupport.stream(iterable.spliterator(), false); return stream.map((entry) -> { String ethAddress = entry.get(0); String btcAddress = entry.get(1); LOG.info("Reading: ETH={} BTC={}", ethAddress, btcAddress); return new KeyPairs(btcAddress, ethAddress, true); }).collect(Collectors.toList()); }
@Test public void testImport_1_Failed() throws Exception { KeyPairs k1 = new KeyPairs("mgfRrsDH56YfTgw3pWJg7j4yEaUgxJxxim", "0x0eB5C5de600D088AB0260d068E9765022FD5173b"); KeyPairs k2 = new KeyPairs("ms8Ux2eEMFTq4HKXiCiPGCXzrV9dPeALx", "0x0eB5C5de600D088AB0260d068E9765022FD5173b"); KeyPairs k3 = new KeyPairs("mhCKesVtR6coeLRGXg3V8D7cQmgjXzaQj2", "0x3Bf54439C6056B564C14986b08e46637dD438372"); KeyPairs k4 = new KeyPairs("muBFsGpYiDLAcTbqZ4vcHsf9dnUTjBoVKg", "0x2D4A5a6FDf6f29ce236251974572ead525eC319D"); List<KeyPairs> keyPairsList = Arrays.asList(k1, k2, k3, k4); when(mockCsvService.fromCSV(any())).thenReturn(keyPairsList); when(mockKeyPairsRepositoryService.addKeyPairsIfNotPresent(k1)).thenReturn(new Boolean(false)); when(mockKeyPairsRepositoryService.addKeyPairsIfNotPresent(k2)).thenReturn(new Boolean(true)); when(mockKeyPairsRepositoryService.addKeyPairsIfNotPresent(k3)).thenReturn(new Boolean(true)); when(mockKeyPairsRepositoryService.addKeyPairsIfNotPresent(k4)).thenReturn(new Boolean(true)); MvcResult mvcResult = this.mockMvc.perform(post("/keypairs/import") .with(httpBasic("user", "password")) .contentType(APPLICATION_OCTET_STREAM_VALUE) .content(IOUtils.toByteArray(csvStream))) .andExpect(status().isOk()) .andDo(print()) .andReturn(); assertThat(mvcResult.getResponse().getContentAsString()).isEqualTo( jsonKeyPairsImportResponse.write(new KeyPairsImportResponse(new Long(3), new Long(1))).getJson() ); }
@Test public void testImport_Successful() throws Exception { List<KeyPairs> keyPairsList = Arrays.asList( new KeyPairs("mgfRrsDH56YfTgw3pWJg7j4yEaUgxJxxim", "0x0eB5C5de600D088AB0260d068E9765022FD5173b"), new KeyPairs("ms8Ux2eEMFTq4HKXiCiPGCXzrV9dPeALx", "0x0eB5C5de600D088AB0260d068E9765022FD5173b"), new KeyPairs("mhCKesVtR6coeLRGXg3V8D7cQmgjXzaQj2", "0x3Bf54439C6056B564C14986b08e46637dD438372"), new KeyPairs("muBFsGpYiDLAcTbqZ4vcHsf9dnUTjBoVKg", "0x2D4A5a6FDf6f29ce236251974572ead525eC319D") ); when(mockCsvService.fromCSV(any())).thenReturn(keyPairsList); when(mockKeyPairsRepositoryService.addKeyPairsIfNotPresent(any())).thenReturn(new Boolean(true)); MvcResult mvcResult = this.mockMvc.perform(post("/keypairs/import") .with(httpBasic("user", "password")) .contentType(APPLICATION_OCTET_STREAM_VALUE) .content(IOUtils.toByteArray(csvStream))) .andExpect(status().isOk()) .andDo(print()) .andReturn(); assertThat(mvcResult.getResponse().getContentAsString()).isEqualTo( jsonKeyPairsImportResponse.write(new KeyPairsImportResponse(new Long(4), new Long(0))).getJson() ); }
@Test public void testAddKeyPairsIfNotPresent_New() { KeyPairs k1 = new KeyPairs("testPublicBtc", "testPublicEth", true); Optional<KeyPairs> resultFind = keyPairsRepositoryService.findByPublicBtc("testPublicBtc"); assertFalse(resultFind.isPresent()); boolean result1 = keyPairsRepositoryService.addKeyPairsIfNotPresent(k1); assertTrue(result1); }
@Test() public void testAddKeyPairsIfNotPresent_AlreadyInserted() { KeyPairs k1 = new KeyPairs("mgfRrsDH56YfTgw3pWJg7j4yEaUgxJxxim", "0x0eB5C5de600D088AB0260d068E9765022FD5173b", true); Optional<KeyPairs> resultFind = keyPairsRepositoryService.findByPublicBtc("mgfRrsDH56YfTgw3pWJg7j4yEaUgxJxxim"); assertTrue(resultFind.isPresent()); boolean result1 = keyPairsRepositoryService.addKeyPairsIfNotPresent(k1); assertFalse(result1); }