FossologyUploader { public FossologyStatus getStatusInFossology(int uploadId, String clearingTeam) { try { final CapturerOutputStream os = new CapturerOutputStream(); if (uploadId < 0) { throw fail("bad Upload Id"); } assertNotEmpty(clearingTeam); final String command = String.format(FOSSOLOGY_COMMAND_GET_STATUS, uploadId, sanitizeQuotes(clearingTeam)); final int exitCode = fossologySshConnector.runInFossologyViaSsh(command, os); String output = os.getContent(); if (exitCode != 0) { log.error("get status in fossology failed: " + output); return FossologyStatus.CONNECTION_FAILED; } return parseResultStatus(output); } catch (SW360Exception e) { log.error("cannot check status of upload with id " + uploadId + " in fossology", e); return FossologyStatus.ERROR; } } @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 testGetStatusInFossology() throws Exception { int uploadId = 42; fossologyUploader = spy(fossologyUploader); String clearingTeam = "team d"; final String output = "output result\nfrom the get status script"; doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { final OutputStream os = (OutputStream) invocation.getArguments()[1]; os.write(output.getBytes()); return 0; } }).when(sshConnector).runInFossologyViaSsh(anyString(), any(OutputStream.class)); FossologyStatus parseResult = FossologyStatus.IN_PROGRESS; doReturn(parseResult).when(fossologyUploader).parseResultStatus(output); FossologyStatus statusInFossology = fossologyUploader.getStatusInFossology(uploadId, clearingTeam); assertThat(statusInFossology, is(parseResult)); verify(fossologyUploader).parseResultStatus(output); verify(sshConnector).runInFossologyViaSsh(anyString(), any(OutputStream.class)); verify(fossologyUploader).getStatusInFossology(uploadId, clearingTeam); }
@Test public void testGetStatusInFossologyIsConnectionErrorForServerNotAvailable() throws Exception { int uploadId = 43; String clearingTeam = "team d"; doReturn(1).when(sshConnector).runInFossologyViaSsh(anyString(), any(OutputStream.class)); FossologyStatus statusInFossology = fossologyUploader.getStatusInFossology(uploadId, clearingTeam); assertThat(statusInFossology, is(FossologyStatus.CONNECTION_FAILED)); verify(sshConnector).runInFossologyViaSsh(anyString(), any(OutputStream.class)); }
@Test public void testGetStatusInFossologyIsErrorForBadUploadId() throws Exception { assertThat(fossologyUploader.getStatusInFossology(-1, "a"), is(FossologyStatus.ERROR)); }