FossologyUploader { public int uploadToFossology(InputStream inputStream, AttachmentContent attachment, String clearingTeam) { try { final CapturerOutputStream os = new CapturerOutputStream(); final String attachmentFilename = attachment.getFilename(); final String id = attachment.getId(); assertNotEmpty(id); assertNotEmpty(clearingTeam); assertNotEmpty(attachmentFilename); final String command = String.format(FOSSOLOGY_COMMAND_UPLOAD, sanitizeQuotes(id), sanitizeQuotes(clearingTeam), sanitizeQuotes(attachmentFilename)); final int exitCode = fossologySshConnector.runInFossologyViaSsh(command, inputStream, os); String output = os.getContent(); if (exitCode != 0) { log.error("upload to fossology failed: " + output); return -1; } return parseResultUploadId(output); } catch (SW360Exception e) { log.error("cannot upload file with id " + attachment.getId() + " (" + attachment.getFilename() + ") to fossology", e); return -1; } } @Autowired  FossologyUploader(FossologySshConnector fossologySshConnector); int uploadToFossology(InputStream inputStream, AttachmentContent attachment, String clearingTeam); boolean duplicateInFossology(int uploadId, String clearingTeam); FossologyStatus getStatusInFossology(int uploadId, String clearingTeam); boolean copyToFossology(String filename, InputStream content, boolean execBit);  }
@Test public void testUploadToFossology() throws Exception { AttachmentContent attachment = mock(AttachmentContent.class); when(attachment.getId()).thenReturn("id"); when(attachment.getFilename()).thenReturn("fileName"); String clearingTeam = "cl"; final InputStream inputStream = mock(InputStream.class); doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { final Object[] arguments = invocation.getArguments(); String command = (String) arguments[0]; InputStream input = (InputStream) arguments[1]; OutputStream outputStream = (OutputStream) arguments[2]; assertThat(input, sameInstance(inputStream)); assertThat(command, is("./uploadFromSW360 -i 'id' -g 'cl' -f 'fileName'")); outputStream.write("uploadId=60".getBytes()); return 0; } }).when(sshConnector).runInFossologyViaSsh(anyString(), eq(inputStream), any(OutputStream.class)); final long uploadId = fossologyUploader.uploadToFossology(inputStream, attachment, clearingTeam); verify(sshConnector).runInFossologyViaSsh(anyString(), eq(inputStream), any(OutputStream.class)); assertThat(uploadId, is((long) 60)); }
@Test public void testUploadToFossologyWithNonIntReturn() throws Exception { AttachmentContent attachment = mock(AttachmentContent.class); when(attachment.getId()).thenReturn("id"); when(attachment.getFilename()).thenReturn("fileName"); String clearingTeam = "cl"; final InputStream inputStream = mock(InputStream.class); doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { OutputStream outputStream = (OutputStream) invocation.getArguments()[2]; outputStream.write("uploadId=60000000000000000".getBytes()); return 0; } }).when(sshConnector).runInFossologyViaSsh(anyString(), eq(inputStream), any(OutputStream.class)); final long uploadId = fossologyUploader.uploadToFossology(inputStream, attachment, clearingTeam); verify(sshConnector).runInFossologyViaSsh(anyString(), eq(inputStream), any(OutputStream.class)); assertThat(uploadId, is((long) -1)); }
@Test public void testUploadToFossologyWithBadReturn() throws Exception { AttachmentContent attachment = mock(AttachmentContent.class); when(attachment.getId()).thenReturn("id"); when(attachment.getFilename()).thenReturn("fileName"); String clearingTeam = "cl"; final InputStream inputStream = mock(InputStream.class); doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { OutputStream outputStream = (OutputStream) invocation.getArguments()[2]; outputStream.write("error".getBytes()); return 0; } }).when(sshConnector).runInFossologyViaSsh(anyString(), eq(inputStream), any(OutputStream.class)); final long uploadId = fossologyUploader.uploadToFossology(inputStream, attachment, clearingTeam); verify(sshConnector).runInFossologyViaSsh(anyString(), eq(inputStream), any(OutputStream.class)); assertThat(uploadId, is((long) -1)); }
@Test public void testUploadToFossologyReportsErrors() throws Exception { AttachmentContent attachment = mock(AttachmentContent.class); when(attachment.getId()).thenReturn("id"); when(attachment.getFilename()).thenReturn("fileName"); String clearingTeam = "cl"; final InputStream inputStream = mock(InputStream.class); doReturn(-1).when(sshConnector).runInFossologyViaSsh(anyString(), eq(inputStream), any(OutputStream.class)); final long uploadId = fossologyUploader.uploadToFossology(inputStream, attachment, clearingTeam); verify(sshConnector).runInFossologyViaSsh(anyString(), eq(inputStream), any(OutputStream.class)); assertThat(uploadId, is((long) -1)); }
@Test public void testUploadToFossologyWithEmptyId() throws Exception { AttachmentContent attachment = mock(AttachmentContent.class); when(attachment.getId()).thenReturn(null); when(attachment.getFilename()).thenReturn("fileName"); String clearingTeam = "cl"; final InputStream inputStream = mock(InputStream.class); final long uploadId = fossologyUploader.uploadToFossology(inputStream, attachment, clearingTeam); verify(sshConnector, never()).runInFossologyViaSsh(anyString(), any(InputStream.class), any(OutputStream.class)); assertThat(uploadId, is((long) -1)); }