use of org.opencastproject.transcription.ibmwatson.persistence.TranscriptionJobControl in project opencast by opencast.
the class IBMWatsonTranscriptionServiceTest method testWorkflowDispatcherJobInProgressTooLong.
@Test
public void testWorkflowDispatcherJobInProgressTooLong() throws Exception {
InputStream stream = IBMWatsonTranscriptionServiceTest.class.getResourceAsStream("/" + IN_PROGRESS_JOB);
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);
database.storeJobControl(MP_ID, TRACK_ID, JOB_ID, TranscriptionJobControl.Status.Progress.name(), 0);
EasyMock.replay(workspace);
WorkflowDispatcher dispatcher = service.new WorkflowDispatcher();
dispatcher.run();
// Check if it called the external service to get the results
Assert.assertEquals("https://stream.watsonplatform.net/speech-to-text/api/v1/recognitions/" + JOB_ID, capturedGet.getValue().getURI().toString());
// Check if the job status was updated and email was sent
TranscriptionJobControl j = database.findByJob(JOB_ID);
Assert.assertNotNull(j);
Assert.assertEquals(TranscriptionJobControl.Status.Canceled.toString(), j.getStatus());
EasyMock.verify(smtpService);
}
use of org.opencastproject.transcription.ibmwatson.persistence.TranscriptionJobControl in project opencast by opencast.
the class IBMWatsonTranscriptionServiceTest method testCreateRecognitionsJob.
@Test
public void testCreateRecognitionsJob() throws Exception {
EasyMock.expect(workspace.get(EasyMock.anyObject(URI.class))).andReturn(audioFile);
EasyMock.replay(workspace);
HttpEntity httpEntity = EasyMock.createNiceMock(HttpEntity.class);
EasyMock.expect(httpEntity.getContent()).andReturn(new ByteArrayInputStream(new String("{\"id\": \"" + JOB_ID + "\", \"status\": \"waiting\", \"url\": \"http://stream.watsonplatform.net/speech-to-text/api/v1/recognitions/" + JOB_ID + "\"}").getBytes(StandardCharsets.UTF_8)));
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_CREATED).anyTimes();
EasyMock.replay(httpEntity, response, status);
Capture<HttpPost> capturedPost = Capture.newInstance();
EasyMock.expect(httpClient.execute(EasyMock.capture(capturedPost))).andReturn(response).anyTimes();
EasyMock.replay(httpClient);
service.createRecognitionsJob(MP_ID, mediaPackage.getTrack("audioTrack1"));
Assert.assertEquals("https://stream.watsonplatform.net/speech-to-text/api/v1/recognitions?user_token=" + MP_ID + "&inactivity_timeout=-1×tamps=true&smart_formatting=true" + "&callback_url=http://ADMIN_SERVER/transcripts/watson/results&events=recognitions.completed_with_results,recognitions.failed", capturedPost.getValue().getURI().toString());
TranscriptionJobControl j = database.findByJob(JOB_ID);
Assert.assertNotNull(j);
Assert.assertEquals(MP_ID, j.getMediaPackageId());
Assert.assertEquals(TRACK_ID, j.getTrackId());
}
use of org.opencastproject.transcription.ibmwatson.persistence.TranscriptionJobControl in project opencast by opencast.
the class IBMWatsonTranscriptionServiceTest method testGetAndSaveJobResults.
@Test
public void testGetAndSaveJobResults() throws Exception {
InputStream stream = IBMWatsonTranscriptionServiceTest.class.getResourceAsStream("/" + PULLED_TRANSCRIPTION_FILE);
database.storeJobControl(MP_ID, TRACK_ID, JOB_ID, TranscriptionJobControl.Status.Progress.name(), TRACK_DURATION);
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://anything"));
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();
// Check if correct url was invoked
Assert.assertEquals("https://stream.watsonplatform.net/speech-to-text/api/v1/recognitions/" + JOB_ID, capturedGet.getValue().getURI().toString());
// Check if status and date in db was updated
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);
// Check if results were saved into a collection
Assert.assertEquals(IBMWatsonTranscriptionService.TRANSCRIPT_COLLECTION, capturedCollection.getValue());
Assert.assertEquals(JOB_ID + ".json", capturedFileName.getValue());
}
use of org.opencastproject.transcription.ibmwatson.persistence.TranscriptionJobControl in project opencast by opencast.
the class IBMWatsonTranscriptionServiceTest method testWorkflowDispatcherJobNotFound.
@Test
public void testWorkflowDispatcherJobNotFound() throws Exception {
CloseableHttpResponse response = EasyMock.createNiceMock(CloseableHttpResponse.class);
StatusLine status = EasyMock.createNiceMock(StatusLine.class);
EasyMock.expect(response.getStatusLine()).andReturn(status).anyTimes();
EasyMock.expect(status.getStatusCode()).andReturn(HttpStatus.SC_NOT_FOUND).anyTimes();
EasyMock.replay(response, status);
Capture<HttpGet> capturedGet = Capture.newInstance();
EasyMock.expect(httpClient.execute(EasyMock.capture(capturedGet))).andReturn(response).anyTimes();
EasyMock.replay(httpClient);
database.storeJobControl(MP_ID, TRACK_ID, JOB_ID, TranscriptionJobControl.Status.Progress.name(), 0);
EasyMock.replay(workspace);
WorkflowDispatcher dispatcher = service.new WorkflowDispatcher();
dispatcher.run();
// Check if it called the external service to get the results
Assert.assertEquals("https://stream.watsonplatform.net/speech-to-text/api/v1/recognitions/" + JOB_ID, capturedGet.getValue().getURI().toString());
// Check if the job status was updated and email was sent
TranscriptionJobControl j = database.findByJob(JOB_ID);
Assert.assertNotNull(j);
Assert.assertEquals(TranscriptionJobControl.Status.Canceled.toString(), j.getStatus());
EasyMock.verify(smtpService);
}
use of org.opencastproject.transcription.ibmwatson.persistence.TranscriptionJobControl in project opencast by opencast.
the class IBMWatsonTranscriptionService method getAndSaveJobResults.
/**
* From: https://www.ibm.com/watson/developercloud/speech-to-text/api/v1 Check a job: GET /v1/recognitions/{id}
*
* curl -X GET -u "{username}":"{password}"
* "https://stream.watsonplatform.net/speech-to-text/api/v1/recognitions/{id}"
*
* Response: { "results": [ { "result_index": 0, "results": [ { "final": true, "alternatives": [ { "transcript":
* "several tornadoes touch down as a line of severe thunderstorms swept through Colorado on Sunday ", "timestamps": [
* [ "several", 1, 1.52 ], [ "tornadoes", 1.52, 2.15 ], . . . [ "Sunday", 5.74, 6.33 ] ], "confidence": 0.885 } ] } ]
* } ], "created": "2016-08-17T19:11:04.298Z", "updated": "2016-08-17T19:11:16.003Z", "status": "completed" }
*/
boolean getAndSaveJobResults(String jobId) throws TranscriptionServiceException {
CloseableHttpClient httpClient = makeHttpClient();
CloseableHttpResponse response = null;
String mpId = "unknown";
try {
HttpGet httpGet = new HttpGet(IBM_WATSON_SERVICE_URL + RECOGNITIONS + "/" + jobId);
response = httpClient.execute(httpGet);
int code = response.getStatusLine().getStatusCode();
switch(code) {
case // 200
HttpStatus.SC_OK:
HttpEntity entity = response.getEntity();
// Response returned is a json object described above
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");
// user_token doesn't come back if this is not in the context of a callback so get the mpId from the db
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("completed") > -1 && jsonObject.get("results") != null) {
transcriptionDone(mpId, jsonObject);
return true;
}
return false;
case // 404
HttpStatus.SC_NOT_FOUND:
logger.info("Job not found: {}", jobId);
break;
case // 503
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) {
String msg = String.format("Exception when calling the recognitions endpoint for media package %s, job id %s", mpId, jobId);
logger.warn(String.format(msg, 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) {
}
}
}
Aggregations