IBMWatsonTranscriptionService extends AbstractJobProducer implements TranscriptionService { String getAndSaveJobResults(String jobId) throws TranscriptionServiceException { CloseableHttpClient httpClient = makeHttpClient(); CloseableHttpResponse response = null; String mpId = "unknown"; try { HttpGet httpGet = new HttpGet(UrlSupport.concat(watsonServiceUrl, RECOGNITIONS, jobId)); response = httpClient.execute(httpGet); int code = response.getStatusLine().getStatusCode(); switch (code) { case HttpStatus.SC_OK: HttpEntity entity = response.getEntity(); String jsonString = EntityUtils.toString(entity); JSONParser jsonParser = new JSONParser(); JSONObject jsonObject = (JSONObject) jsonParser.parse(jsonString); String jobStatus = (String) jsonObject.get("status"); mpId = (String) jsonObject.get("user_token"); if (mpId == null) { TranscriptionJobControl jc = database.findByJob(jobId); if (jc != null) mpId = jc.getMediaPackageId(); } logger.info("Recognitions job {} has been found, status {}", jobId, jobStatus); EntityUtils.consume(entity); if (jobStatus.indexOf(RecognitionJobStatus.COMPLETED) > -1 && jsonObject.get("results") != null) { transcriptionDone(mpId, jsonObject); } return jobStatus; case HttpStatus.SC_NOT_FOUND: logger.info("Job not found: {}", jobId); break; case HttpStatus.SC_SERVICE_UNAVAILABLE: logger.info("Service unavailable returned, status: {}", code); break; default: logger.info("Unknown return status: {}.", code); break; } throw new TranscriptionServiceException( String.format("Could not check recognition job for media package %s, job id %s. Status returned: %d", mpId, jobId, code), code); } catch (TranscriptionServiceException e) { throw e; } catch (Exception e) { logger.warn("Exception when calling the recognitions endpoint for media package {}, job id {}", mpId, jobId, e); throw new TranscriptionServiceException(String.format( "Exception when calling the recognitions endpoint for media package %s, job id %s", mpId, jobId), e); } finally { try { httpClient.close(); if (response != null) response.close(); } catch (IOException e) { } } } IBMWatsonTranscriptionService(); void activate(ComponentContext cc); @Override Job startTranscription(String mpId, Track track); @Override Job startTranscription(String mpId, Track track, String language); @Override void transcriptionDone(String mpId, Object obj); @Override void transcriptionError(String mpId, Object obj); @Override String getLanguage(); @Override MediaPackageElement getGeneratedTranscription(String mpId, String jobId); boolean isCallbackAlreadyRegistered(); void setServiceRegistry(ServiceRegistry serviceRegistry); void setSecurityService(SecurityService securityService); void setUserDirectoryService(UserDirectoryService userDirectoryService); void setOrganizationDirectoryService(OrganizationDirectoryService organizationDirectoryService); void setSmtpService(SmtpService service); void setWorkspace(Workspace ws); void setWorkingFileRepository(WorkingFileRepository wfr); void setDatabase(TranscriptionDatabase service); void setAssetManager(AssetManager service); void setWorkflowService(WorkflowService service); boolean isEnabled(); static final String DEFAULT_WF_DEF; static final String ADMIN_URL_PROPERTY; static final float DEFAULT_START_TRANSCRIPTION_JOB_LOAD; static final String START_TRANSCRIPTION_JOB_LOAD_KEY; static final String ENABLED_CONFIG; static final String IBM_WATSON_SERVICE_URL_CONFIG; static final String IBM_WATSON_USER_CONFIG; static final String IBM_WATSON_PSW_CONFIG; static final String IBM_WATSON_API_KEY_CONFIG; static final String IBM_WATSON_MODEL_CONFIG; static final String WORKFLOW_CONFIG; static final String DISPATCH_WORKFLOW_INTERVAL_CONFIG; static final String COMPLETION_CHECK_BUFFER_CONFIG; static final String MAX_PROCESSING_TIME_CONFIG; static final String NOTIFICATION_EMAIL_CONFIG; static final String CLEANUP_RESULTS_DAYS_CONFIG; static final String MAX_ATTEMPTS_CONFIG; static final String RETRY_WORKLFOW_CONFIG; }
@Test public void testGetAndSaveJobResults() throws Exception { service.activate(cc); InputStream stream = IBMWatsonTranscriptionServiceTest.class.getResourceAsStream("/" + PULLED_TRANSCRIPTION_FILE); database.storeJobControl(MP_ID, TRACK_ID, JOB_ID, TranscriptionJobControl.Status.InProgress.name(), TRACK_DURATION, DATE_EXPECTED, PROVIDER); Capture<String> capturedCollection = Capture.newInstance(); Capture<String> capturedFileName = Capture.newInstance(); EasyMock.expect(workspace.putInCollection(EasyMock.capture(capturedCollection), EasyMock.capture(capturedFileName), EasyMock.anyObject(InputStream.class))).andReturn(new URI("http: EasyMock.replay(workspace); HttpEntity httpEntity = EasyMock.createNiceMock(HttpEntity.class); EasyMock.expect(httpEntity.getContent()).andReturn(stream); CloseableHttpResponse response = EasyMock.createNiceMock(CloseableHttpResponse.class); StatusLine status = EasyMock.createNiceMock(StatusLine.class); EasyMock.expect(response.getStatusLine()).andReturn(status).anyTimes(); EasyMock.expect(response.getEntity()).andReturn(httpEntity).anyTimes(); EasyMock.expect(status.getStatusCode()).andReturn(HttpStatus.SC_OK).anyTimes(); EasyMock.replay(httpEntity, response, status); Capture<HttpGet> capturedGet = Capture.newInstance(); EasyMock.expect(httpClient.execute(EasyMock.capture(capturedGet))).andReturn(response).anyTimes(); EasyMock.replay(httpClient); long before = System.currentTimeMillis(); service.getAndSaveJobResults(JOB_ID); long after = System.currentTimeMillis(); Assert.assertEquals(WATSON_URL + "/v1/recognitions/" + JOB_ID, capturedGet.getValue().getURI().toString()); TranscriptionJobControl job = database.findByJob(JOB_ID); Assert.assertNotNull(job); Assert.assertEquals(TranscriptionJobControl.Status.TranscriptionComplete.name(), job.getStatus()); Assert.assertNotNull(job.getDateCompleted()); Assert.assertTrue(before <= job.getDateCompleted().getTime() && job.getDateCompleted().getTime() <= after); Assert.assertEquals(IBMWatsonTranscriptionService.TRANSCRIPT_COLLECTION, capturedCollection.getValue()); Assert.assertEquals(JOB_ID + ".json", capturedFileName.getValue()); }