AnnotationsQueryService implements Query.Service { @Override public Query prepareQuery( final String sql, final int maxRows, final Map<String, String> parameters ) throws KettleException { return prepareQuery( sql, null, 0, 0, 0, parameters ); } AnnotationsQueryService( final MetastoreLocator metastoreLocator, final DataServiceResolver resolver ); @Override Query prepareQuery( final String sql, final int maxRows, final Map<String, String> parameters ); @Override Query prepareQuery( final String sql, IDataServiceClientService.StreamingMode windowMode,
                                       long windowSize, long windowEvery, long windowLimit,
                                       final Map<String, String> parameters );  }
@Test public void testLogsExceptionOnWriteEror() throws Exception { final DataServiceDelegate dataServiceFactory = mock( DataServiceDelegate.class ); final DataServiceContext dataServiceContext = mock( DataServiceContext.class ); final DataServiceResolver dataServiceResolver = mock( DataServiceResolver.class ); final MetastoreLocator metastoreLocator = mock( MetastoreLocator.class ); when( dataServiceContext.getDataServiceDelegate() ).thenReturn( dataServiceFactory ); when( metastoreLocator.getMetastore() ).thenReturn( null ); final AnnotationsQueryService queryService = new AnnotationsQueryService( metastoreLocator, dataServiceResolver ); Query query = queryService.prepareQuery( "show annotations from annotatedService", 0, Collections.<String, String>emptyMap() ); MetaStoreException metaStoreException = new MetaStoreException( "Unable to load dataservice annotatedService" ); when( dataServiceResolver.getDataService( "annotatedService" ) ).thenReturn( null ); try { query.writeTo( new ByteArrayOutputStream( ) ); fail( "should have got exception" ); } catch ( IOException e ) { assertEquals( "Error while executing 'show annotations from annotatedService'", e.getMessage() ); } }
@Test public void testGetsAnnotationsDefinedByStream() throws Exception { final DataServiceDelegate dataServiceFactory = mock( DataServiceDelegate.class ); final DataServiceContext dataServiceContext = mock( DataServiceContext.class ); final DataServiceResolver dataServiceResolver = mock( DataServiceResolver.class ); final MetastoreLocator metastoreLocator = mock( MetastoreLocator.class ); when( dataServiceContext.getDataServiceDelegate() ).thenReturn( dataServiceFactory ); when( metastoreLocator.getMetastore() ).thenReturn( null ); final AnnotationsQueryService queryService = new AnnotationsQueryService( metastoreLocator, dataServiceResolver ); URL resource = getClass().getClassLoader().getResource( "showAnnotations.ktr" ); @SuppressWarnings( "ConstantConditions" ) final TransMeta transMeta = new TransMeta( resource.getPath(), "show annotations" ); Document document = XMLHandler.loadXMLFile( resource ); transMeta.loadXML( document.getFirstChild(), null, true ); final DataServiceMeta dataServiceMeta = new DataServiceMeta( transMeta ); dataServiceMeta.setName( "annotatedService" ); dataServiceMeta.setStepname( "Annotate Stream" ); when( dataServiceResolver.getDataService( "annotatedService" ) ).thenReturn( dataServiceMeta ); Query query = queryService.prepareQuery( "show annotations from annotatedService", 0, Collections.<String, String>emptyMap() ); AnnotationsQueryService.AnnotationsQuery spy = (AnnotationsQueryService.AnnotationsQuery) Mockito.spy( query ); final ModelAnnotationGroup mag = new ModelAnnotationGroup( new ModelAnnotation<>( new CreateMeasure() ) ); final Trans[] transSpy = { null }; when( spy.getTrans( transMeta ) ).then( new Answer<Trans>() { @Override public Trans answer( final InvocationOnMock invocationOnMock ) throws Throwable { Trans trans = (Trans) invocationOnMock.callRealMethod(); transSpy[ 0 ] = Mockito.spy( trans ); trans.getExtensionDataMap().put( "KEY_MODEL_ANNOTATIONS", mag ); return transSpy[ 0 ]; } } ); final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); spy.writeTo( outputStream ); verify( transSpy[ 0 ] ).cleanup(); ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream( outputStream.toByteArray() ); DataInputStream dataInputStream = new DataInputStream( byteArrayInputStream ); ThinResultSet thinResultSet = new ThinResultFactory().loadResultSet( dataInputStream, null ); thinResultSet.next(); String output = thinResultSet.getString( 1 ); assertEquals( new ModelAnnotationGroupXmlWriter( mag ).getXML().trim(), output ); verify( metastoreLocator ).getMetastore(); assertEquals( 0, query.getTransList().size() ); thinResultSet.close(); }
@Test public void testNoAnnotationsReturnsEmpty() throws Exception { final DataServiceDelegate dataServiceFactory = mock( DataServiceDelegate.class ); final DataServiceContext dataServiceContext = mock( DataServiceContext.class ); final DataServiceResolver dataServiceResolver = mock( DataServiceResolver.class ); final MetastoreLocator metastoreLocator = mock( MetastoreLocator.class ); when( dataServiceContext.getDataServiceDelegate() ).thenReturn( dataServiceFactory ); when( metastoreLocator.getMetastore() ).thenReturn( null ); final AnnotationsQueryService queryService = new AnnotationsQueryService( metastoreLocator, dataServiceResolver ); URL resource = getClass().getClassLoader().getResource( "showAnnotations.ktr" ); @SuppressWarnings( "ConstantConditions" ) final TransMeta transMeta = new TransMeta( resource.getPath(), "show annotations" ); Document document = XMLHandler.loadXMLFile( resource ); transMeta.loadXML( document.getFirstChild(), null, true ); final DataServiceMeta dataServiceMeta = new DataServiceMeta( transMeta ); dataServiceMeta.setName( "gridService" ); dataServiceMeta.setStepname( "Data Grid" ); when( dataServiceResolver.getDataService( "gridService" ) ).thenReturn( dataServiceMeta ); Query query = queryService.prepareQuery( "show annotations from gridService", 0, Collections.<String, String>emptyMap() ); final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); query.writeTo( outputStream ); String output = getResultString( outputStream ); assertEquals( "<annotations></annotations>", output.trim() ); }
@Test public void testAnnotationsOnCurrentStep() throws Exception { TransMeta transMeta = new TransMeta(); DummyTransMeta src1Meta = new DummyTransMeta(); StepMeta src1 = new StepMeta( "src", src1Meta ); transMeta.addStep( src1 ); final ModelAnnotationGroup mag1 = new ModelAnnotationGroup( new ModelAnnotation<CreateAttribute>( new CreateAttribute() ) ); final String name1 = mag1.get( 0 ).getName(); DummyTransMeta annot1Meta = createPseudoAnnotate( mag1 ); StepMeta annot1 = Mockito.spy( new StepMeta( "annot", annot1Meta ) ); transMeta.addStep( annot1 ); TransHopMeta src1ToAnnot1 = new TransHopMeta( src1, annot1 ); transMeta.addTransHop( src1ToAnnot1 ); final DataServiceMeta dsA = new DataServiceMeta( transMeta ); dsA.setName( "dsa" ); dsA.setStepname( "annot" ); final DataServiceMeta ds1 = new DataServiceMeta( transMeta ); ds1.setName( "ds" ); ds1.setStepname( "src" ); final DataServiceDelegate dataServiceFactory = mock( DataServiceDelegate.class ); final DataServiceContext dataServiceContext = mock( DataServiceContext.class ); when( dataServiceContext.getDataServiceDelegate() ).thenReturn( dataServiceFactory ); final DataServiceResolver dataServiceResolver = mock( DataServiceResolver.class ); when( dataServiceResolver.getDataService( "ds" ) ).thenReturn( ds1 ); when( dataServiceResolver.getDataService( "dsa" ) ).thenReturn( dsA ); final MetastoreLocator metastoreLocator = mock( MetastoreLocator.class ); when( metastoreLocator.getMetastore() ).thenReturn( null ); final AnnotationsQueryService queryService = new AnnotationsQueryService( metastoreLocator, dataServiceResolver ); Query query = queryService.prepareQuery( "show annotations from dsa", 0, Collections.<String, String>emptyMap() ); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); query.writeTo( outputStream ); String result = getResultString( outputStream ); assertTrue( result.contains( name1 ) ); query = queryService.prepareQuery( "show annotations from ds", 0, Collections.<String, String>emptyMap() ); outputStream = new ByteArrayOutputStream(); query.writeTo( outputStream ); result = getResultString( outputStream ); String output = getResultString( outputStream ); assertEquals( "<annotations></annotations>", output.trim() ); }
@Test public void testAnnotationsOnCurrentStep2Paths() throws Exception { TransMeta transMeta = new TransMeta(); DummyTransMeta src1Meta = new DummyTransMeta(); StepMeta src1 = new StepMeta( "src1", src1Meta ); transMeta.addStep( src1 ); final ModelAnnotationGroup mag1 = new ModelAnnotationGroup( new ModelAnnotation<CreateAttribute>( new CreateAttribute() ) ); final String name1 = mag1.get( 0 ).getName(); DummyTransMeta annot1Meta = createPseudoAnnotate( mag1 ); StepMeta annot1 = new StepMeta( "annot1", annot1Meta ); transMeta.addStep( annot1 ); TransHopMeta src1ToAnnot1 = new TransHopMeta( src1, annot1 ); transMeta.addTransHop( src1ToAnnot1 ); DummyTransMeta src2Meta = new DummyTransMeta(); StepMeta src2 = new StepMeta( "src2", src2Meta ); transMeta.addStep( src2 ); final ModelAnnotationGroup mag2 = new ModelAnnotationGroup( new ModelAnnotation<CreateAttribute>( new CreateAttribute() ) ); final String name2 = mag2.get( 0 ).getName(); DummyTransMeta annot2Meta = createPseudoAnnotate( mag2 ); StepMeta annot2 = new StepMeta( "annot2", annot2Meta ); transMeta.addStep( annot2 ); TransHopMeta src2ToAnnot2 = new TransHopMeta( src2, annot2 ); transMeta.addTransHop( src2ToAnnot2 ); DummyTransMeta mergedMeta = new DummyTransMeta(); StepMeta mergedStepMeta = new StepMeta( "merged", mergedMeta ); transMeta.addStep( mergedStepMeta ); transMeta.addTransHop( new TransHopMeta( annot1, mergedStepMeta ) ); transMeta.addTransHop( new TransHopMeta( annot2, mergedStepMeta ) ); final DataServiceMeta ds1 = new DataServiceMeta( (TransMeta) transMeta.clone() ); ds1.setName( "dsa" ); ds1.setStepname( "annot1" ); final DataServiceMeta dsAll = new DataServiceMeta( (TransMeta) transMeta.clone() ); dsAll.setName( "ds" ); dsAll.setStepname( "merged" ); final DataServiceDelegate dataServiceFactory = mock( DataServiceDelegate.class ); final DataServiceResolver dataServiceResolver = mock( DataServiceResolver.class ); when( dataServiceResolver.getDataService( "dsAll" ) ).thenReturn( dsAll ); when( dataServiceResolver.getDataService( "ds1" ) ).thenReturn( ds1 ); final DataServiceContext dataServiceContext = mock( DataServiceContext.class ); when( dataServiceContext.getDataServiceDelegate() ).thenReturn( dataServiceFactory ); final MetastoreLocator metastoreLocator = mock( MetastoreLocator.class ); when( metastoreLocator.getMetastore() ).thenReturn( null ); AnnotationsQueryService queryService = new AnnotationsQueryService( metastoreLocator, dataServiceResolver ); Query query = queryService.prepareQuery( "show annotations from ds1", 0, Collections.<String, String>emptyMap() ); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); query.writeTo( outputStream ); String result = getResultString( outputStream ); assertTrue( result.contains( name1 ) ); assertFalse( result.contains( name2 ) ); queryService = new AnnotationsQueryService( metastoreLocator, dataServiceResolver ); query = queryService.prepareQuery( "show annotations from dsAll", 0, Collections.<String, String>emptyMap() ); outputStream = new ByteArrayOutputStream(); query.writeTo( outputStream ); String result2 = getResultString( outputStream ); assertTrue( result2.contains( name1 ) ); assertTrue( result2.contains( name2 ) ); }