Search in sources :

Example 11 with TranscriptionJobControl

use of org.opencastproject.transcription.ibmwatson.persistence.TranscriptionJobControl in project opencast by opencast.

the class IBMWatsonTranscriptionServiceTest method testTranscriptionError.

@Test
public void testTranscriptionError() throws Exception {
    InputStream stream = IBMWatsonTranscriptionServiceTest.class.getResourceAsStream("/" + PUSHED_TRANSCRIPTION_FILE);
    database.storeJobControl(MP_ID, TRACK_ID, JOB_ID, TranscriptionJobControl.Status.Progress.name(), TRACK_DURATION);
    JSONObject obj = (JSONObject) jsonParser.parse(new InputStreamReader(stream));
    service.transcriptionError(MP_ID, obj);
    // Check if status and date in db was updated
    TranscriptionJobControl job = database.findByJob(JOB_ID);
    Assert.assertNotNull(job);
    Assert.assertEquals(TranscriptionJobControl.Status.Error.name(), job.getStatus());
    Assert.assertNull(job.getDateCompleted());
    EasyMock.verify(smtpService);
}
Also used : TranscriptionJobControl(org.opencastproject.transcription.ibmwatson.persistence.TranscriptionJobControl) JSONObject(org.json.simple.JSONObject) InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Test(org.junit.Test)

Example 12 with TranscriptionJobControl

use of org.opencastproject.transcription.ibmwatson.persistence.TranscriptionJobControl in project opencast by opencast.

the class IBMWatsonTranscriptionServiceTest method testTranscriptionDone.

@Test
public void testTranscriptionDone() throws Exception {
    InputStream stream = IBMWatsonTranscriptionServiceTest.class.getResourceAsStream("/" + PUSHED_TRANSCRIPTION_FILE);
    database.storeJobControl(MP_ID, TRACK_ID, JOB_ID, TranscriptionJobControl.Status.Progress.name(), TRACK_DURATION);
    JSONObject obj = (JSONObject) jsonParser.parse(new InputStreamReader(stream));
    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);
    long before = System.currentTimeMillis();
    service.transcriptionDone(MP_ID, obj);
    long after = System.currentTimeMillis();
    // 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());
}
Also used : TranscriptionJobControl(org.opencastproject.transcription.ibmwatson.persistence.TranscriptionJobControl) JSONObject(org.json.simple.JSONObject) InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) URI(java.net.URI) Test(org.junit.Test)

Example 13 with TranscriptionJobControl

use of org.opencastproject.transcription.ibmwatson.persistence.TranscriptionJobControl in project opencast by opencast.

the class IBMWatsonTranscriptionService method transcriptionError.

@Override
public void transcriptionError(String mpId, Object obj) throws TranscriptionServiceException {
    JSONObject jsonObj = null;
    String jobId = null;
    try {
        jsonObj = (JSONObject) obj;
        jobId = (String) jsonObj.get("id");
        // Update state in database
        database.updateJobControl(jobId, TranscriptionJobControl.Status.Error.name());
        TranscriptionJobControl jobControl = database.findByJob(jobId);
        logger.warn(String.format("Error callback received for media package %s, job id %s", jobControl.getMediaPackageId(), jobId));
        // Send notification email
        sendEmail("Transcription ERROR", String.format("There was a transcription error for for media package %s, job id %s.", jobControl.getMediaPackageId(), jobId));
    } catch (TranscriptionDatabaseException e) {
        logger.warn("Transcription error. State in db could not be updated to error for mpId {}, jobId {}", mpId, jobId);
        throw new TranscriptionServiceException("Could not update transcription job control db", e);
    }
}
Also used : TranscriptionJobControl(org.opencastproject.transcription.ibmwatson.persistence.TranscriptionJobControl) JSONObject(org.json.simple.JSONObject) TranscriptionDatabaseException(org.opencastproject.transcription.ibmwatson.persistence.TranscriptionDatabaseException) TranscriptionServiceException(org.opencastproject.transcription.api.TranscriptionServiceException)

Example 14 with TranscriptionJobControl

use of org.opencastproject.transcription.ibmwatson.persistence.TranscriptionJobControl in project opencast by opencast.

the class IBMWatsonTranscriptionServiceTest method testWorkflowDispatcherRunProgressState.

@Test
public void testWorkflowDispatcherRunProgressState() throws Exception {
    InputStream stream = IBMWatsonTranscriptionServiceTest.class.getResourceAsStream("/" + PULLED_TRANSCRIPTION_FILE);
    database.storeJobControl(MP_ID, TRACK_ID, JOB_ID, TranscriptionJobControl.Status.Progress.name(), 0);
    database.storeJobControl("mpId2", "audioTrack2", "jobId2", TranscriptionJobControl.Status.Progress.name(), TRACK_DURATION);
    EasyMock.expect(workspace.putInCollection(EasyMock.anyObject(String.class), EasyMock.anyObject(String.class), 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);
    EasyMock.replay(httpClient);
    // enrich(q.select(q.snapshot()).where(q.mediaPackageId(mpId).and(q.version().isLatest())).run()).getSnapshots();
    // Mocks for query, result, etc
    Snapshot snapshot = EasyMock.createNiceMock(Snapshot.class);
    EasyMock.expect(snapshot.getOrganizationId()).andReturn(org.getId());
    ARecord aRec = EasyMock.createNiceMock(ARecord.class);
    EasyMock.expect(aRec.getSnapshot()).andReturn(Opt.some(snapshot));
    Stream<ARecord> recStream = Stream.mk(aRec);
    Predicate p = EasyMock.createNiceMock(Predicate.class);
    EasyMock.expect(p.and(p)).andReturn(p);
    AResult r = EasyMock.createNiceMock(AResult.class);
    EasyMock.expect(r.getSize()).andReturn(1L);
    EasyMock.expect(r.getRecords()).andReturn(recStream);
    Target t = EasyMock.createNiceMock(Target.class);
    ASelectQuery selectQuery = EasyMock.createNiceMock(ASelectQuery.class);
    EasyMock.expect(selectQuery.where(EasyMock.anyObject(Predicate.class))).andReturn(selectQuery);
    EasyMock.expect(selectQuery.run()).andReturn(r);
    AQueryBuilder query = EasyMock.createNiceMock(AQueryBuilder.class);
    EasyMock.expect(query.snapshot()).andReturn(t);
    EasyMock.expect(query.mediaPackageId(EasyMock.anyObject(String.class))).andReturn(p);
    EasyMock.expect(query.select(EasyMock.anyObject(Target.class))).andReturn(selectQuery);
    VersionField v = EasyMock.createNiceMock(VersionField.class);
    EasyMock.expect(v.isLatest()).andReturn(p);
    EasyMock.expect(query.version()).andReturn(v);
    EasyMock.expect(assetManager.createQuery()).andReturn(query);
    EasyMock.replay(snapshot, aRec, p, r, t, selectQuery, query, v, assetManager);
    Capture<Set<String>> capturedMpIds = Capture.newInstance();
    WorkflowDefinition wfDef = new WorkflowDefinitionImpl();
    EasyMock.expect(wfService.getWorkflowDefinitionById(IBMWatsonTranscriptionService.DEFAULT_WF_DEF)).andReturn(wfDef);
    List<WorkflowInstance> wfList = new ArrayList<WorkflowInstance>();
    wfList.add(new WorkflowInstanceImpl());
    Stream<WorkflowInstance> wfListStream = Stream.mk(wfList);
    Workflows wfs = EasyMock.createNiceMock(Workflows.class);
    EasyMock.expect(wfs.applyWorkflowToLatestVersion(EasyMock.capture(capturedMpIds), EasyMock.anyObject(ConfiguredWorkflow.class))).andReturn(wfListStream);
    service.setWfUtil(wfs);
    EasyMock.replay(wfService, wfs);
    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 only one mp has a workflow created for it
    Assert.assertEquals(1, capturedMpIds.getValue().size());
    // And if it was the correct one
    Assert.assertEquals(MP_ID, capturedMpIds.getValue().iterator().next());
    // Check if status in db was updated
    TranscriptionJobControl job = database.findByJob(JOB_ID);
    Assert.assertNotNull(job);
    Assert.assertEquals(TranscriptionJobControl.Status.Closed.name(), job.getStatus());
}
Also used : Set(java.util.Set) HttpEntity(org.apache.http.HttpEntity) HttpGet(org.apache.http.client.methods.HttpGet) ArrayList(java.util.ArrayList) AQueryBuilder(org.opencastproject.assetmanager.api.query.AQueryBuilder) WorkflowDispatcher(org.opencastproject.transcription.ibmwatson.IBMWatsonTranscriptionService.WorkflowDispatcher) WorkflowInstance(org.opencastproject.workflow.api.WorkflowInstance) URI(java.net.URI) Predicate(org.opencastproject.assetmanager.api.query.Predicate) ARecord(org.opencastproject.assetmanager.api.query.ARecord) Target(org.opencastproject.assetmanager.api.query.Target) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) AResult(org.opencastproject.assetmanager.api.query.AResult) TranscriptionJobControl(org.opencastproject.transcription.ibmwatson.persistence.TranscriptionJobControl) WorkflowDefinitionImpl(org.opencastproject.workflow.api.WorkflowDefinitionImpl) Workflows(org.opencastproject.assetmanager.util.Workflows) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) VersionField(org.opencastproject.assetmanager.api.query.VersionField) WorkflowDefinition(org.opencastproject.workflow.api.WorkflowDefinition) ConfiguredWorkflow(org.opencastproject.workflow.api.ConfiguredWorkflow) StatusLine(org.apache.http.StatusLine) Snapshot(org.opencastproject.assetmanager.api.Snapshot) WorkflowInstanceImpl(org.opencastproject.workflow.api.WorkflowInstanceImpl) ASelectQuery(org.opencastproject.assetmanager.api.query.ASelectQuery) Test(org.junit.Test)

Example 15 with TranscriptionJobControl

use of org.opencastproject.transcription.ibmwatson.persistence.TranscriptionJobControl in project opencast by opencast.

the class TranscriptionDatabaseTest method testUpdateJobControl.

@Test
public void testUpdateJobControl() throws Exception {
    long dt1 = System.currentTimeMillis();
    database.storeJobControl(MP_ID, TRACK_ID, JOB_ID, STATUS, TRACK_DURATION);
    long dt2 = System.currentTimeMillis();
    TranscriptionJobControl j = database.findByJob(JOB_ID);
    Assert.assertNotNull(j);
    Assert.assertEquals(MP_ID, j.getMediaPackageId());
    Assert.assertEquals(TRACK_ID, j.getTrackId());
    Assert.assertEquals(STATUS, j.getStatus());
    long created = j.getDateCreated().getTime();
    Assert.assertTrue(dt1 <= created && created <= dt2);
    database.updateJobControl(JOB_ID, STATUS2);
    j = database.findByJob(JOB_ID);
    Assert.assertNotNull(j);
    Assert.assertEquals(MP_ID, j.getMediaPackageId());
    Assert.assertEquals(TRACK_ID, j.getTrackId());
    Assert.assertEquals(STATUS2, j.getStatus());
}
Also used : TranscriptionJobControl(org.opencastproject.transcription.ibmwatson.persistence.TranscriptionJobControl) Test(org.junit.Test)

Aggregations

TranscriptionJobControl (org.opencastproject.transcription.ibmwatson.persistence.TranscriptionJobControl)16 Test (org.junit.Test)13 ByteArrayInputStream (java.io.ByteArrayInputStream)6 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)6 InputStream (java.io.InputStream)5 URI (java.net.URI)5 HttpEntity (org.apache.http.HttpEntity)5 StatusLine (org.apache.http.StatusLine)5 HttpGet (org.apache.http.client.methods.HttpGet)5 JSONObject (org.json.simple.JSONObject)4 WorkflowDispatcher (org.opencastproject.transcription.ibmwatson.IBMWatsonTranscriptionService.WorkflowDispatcher)4 TranscriptionServiceException (org.opencastproject.transcription.api.TranscriptionServiceException)3 TranscriptionDatabaseException (org.opencastproject.transcription.ibmwatson.persistence.TranscriptionDatabaseException)3 IOException (java.io.IOException)2 InputStreamReader (java.io.InputStreamReader)2 ArrayList (java.util.ArrayList)2 Set (java.util.Set)2 Snapshot (org.opencastproject.assetmanager.api.Snapshot)2 AQueryBuilder (org.opencastproject.assetmanager.api.query.AQueryBuilder)2 ARecord (org.opencastproject.assetmanager.api.query.ARecord)2