Search in sources :

Example 1 with PropertyDefinition

use of org.apache.heron.eco.definition.PropertyDefinition in project heron by twitter.

the class EcoParserTest method testParseFromInputStream_VerifyComponents_MapsAsExpected.

@Test
public void testParseFromInputStream_VerifyComponents_MapsAsExpected() throws Exception {
    InputStream inputStream = new ByteArrayInputStream(YAML_STR_1.getBytes());
    FileInputStream mockPropsStream = PowerMockito.mock(FileInputStream.class);
    EcoTopologyDefinition topologyDefinition = subject.parseFromInputStream(inputStream, mockPropsStream, false);
    List<BeanDefinition> components = topologyDefinition.getComponents();
    assertEquals("kafka-topology", topologyDefinition.getName());
    assertEquals("heron", topologyDefinition.getType());
    assertEquals(4, components.size());
    BeanDefinition stringSchemeComponent = components.get(0);
    assertEquals("stringScheme", stringSchemeComponent.getId());
    assertEquals("org.apache.storm.kafka.StringScheme", stringSchemeComponent.getClassName());
    BeanDefinition stringMultiSchemeComponent = components.get(1);
    assertEquals("stringMultiScheme", stringMultiSchemeComponent.getId());
    assertEquals("org.apache.storm.spout.SchemeAsMultiScheme", stringMultiSchemeComponent.getClassName());
    assertEquals(1, stringMultiSchemeComponent.getConstructorArgs().size());
    BeanReference multiStringReference = (BeanReference) stringMultiSchemeComponent.getConstructorArgs().get(0);
    assertEquals("stringScheme", multiStringReference.getId());
    BeanDefinition zkHostsComponent = components.get(2);
    assertEquals("zkHosts", zkHostsComponent.getId());
    assertEquals("org.apache.storm.kafka.ZkHosts", zkHostsComponent.getClassName());
    assertEquals(1, zkHostsComponent.getConstructorArgs().size());
    assertEquals("localhost:2181", zkHostsComponent.getConstructorArgs().get(0));
    BeanDefinition spoutConfigComponent = components.get(3);
    List<Object> spoutConstructArgs = spoutConfigComponent.getConstructorArgs();
    assertEquals("spoutConfig", spoutConfigComponent.getId());
    assertEquals("org.apache.storm.kafka.SpoutConfig", spoutConfigComponent.getClassName());
    BeanReference spoutBrokerHostComponent = (BeanReference) spoutConstructArgs.get(0);
    assertEquals("zkHosts", spoutBrokerHostComponent.getId());
    assertEquals("myKafkaTopic", spoutConstructArgs.get(1));
    assertEquals("/kafkaSpout", spoutConstructArgs.get(2));
    List<PropertyDefinition> properties = spoutConfigComponent.getProperties();
    assertEquals("ignoreZkOffsets", properties.get(0).getName());
    assertEquals(true, properties.get(0).getValue());
    assertEquals("scheme", properties.get(1).getName());
    assertEquals(true, properties.get(1).isReference());
    assertEquals("stringMultiScheme", properties.get(1).getRef());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) BeanReference(org.apache.heron.eco.definition.BeanReference) EcoTopologyDefinition(org.apache.heron.eco.definition.EcoTopologyDefinition) BeanDefinition(org.apache.heron.eco.definition.BeanDefinition) PropertyDefinition(org.apache.heron.eco.definition.PropertyDefinition) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 2 with PropertyDefinition

use of org.apache.heron.eco.definition.PropertyDefinition in project heron by twitter.

the class BuilderUtilityTest method applyProperties_NoSetterFound_BehavesAsExpected.

@Test
public void applyProperties_NoSetterFound_BehavesAsExpected() throws IllegalAccessException, NoSuchFieldException, InvocationTargetException {
    final String ref = "ref";
    final String fakeComponent = "component";
    MockComponent mockComponent = new MockComponent();
    List<PropertyDefinition> propertyDefinitions = new ArrayList<>();
    PropertyDefinition propertyDefinition = new PropertyDefinition();
    propertyDefinition.setRef(ref);
    propertyDefinition.setName("publicStr");
    propertyDefinitions.add(propertyDefinition);
    when(mockObjectDefinition.getProperties()).thenReturn(propertyDefinitions);
    when(mockContext.getComponent(eq(ref))).thenReturn(fakeComponent);
    subject.applyProperties(mockObjectDefinition, mockComponent, mockContext);
    verify(mockContext).getComponent(same(ref));
    verify(mockObjectDefinition).getProperties();
}
Also used : ArrayList(java.util.ArrayList) PropertyDefinition(org.apache.heron.eco.definition.PropertyDefinition) Test(org.junit.Test)

Example 3 with PropertyDefinition

use of org.apache.heron.eco.definition.PropertyDefinition in project heron by twitter.

the class BuilderUtility method applyProperties.

@SuppressWarnings("rawtypes")
protected void applyProperties(ObjectDefinition bean, Object instance, EcoExecutionContext context) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException {
    List<PropertyDefinition> props = bean.getProperties();
    Class clazz = instance.getClass();
    if (props != null) {
        for (PropertyDefinition prop : props) {
            Object value = prop.isReference() ? context.getComponent(prop.getRef()) : prop.getValue();
            Method setter = findSetter(clazz, prop.getName());
            if (setter != null) {
                LOG.debug("found setter, attempting with: " + instance.getClass() + "  " + value);
                // invoke setter
                setter.invoke(instance, new Object[] { value });
            } else {
                // look for a public instance variable
                LOG.debug("no setter found. Looking for a public instance variable...");
                Field field = findPublicField(clazz, prop.getName());
                if (field != null) {
                    field.set(instance, value);
                }
            }
        }
    }
}
Also used : Field(java.lang.reflect.Field) Method(java.lang.reflect.Method) PropertyDefinition(org.apache.heron.eco.definition.PropertyDefinition)

Example 4 with PropertyDefinition

use of org.apache.heron.eco.definition.PropertyDefinition in project heron by twitter.

the class BuilderUtilityTest method applyProperties_SetterFound_BehavesAsExpected.

@Test
public void applyProperties_SetterFound_BehavesAsExpected() throws IllegalAccessException, NoSuchFieldException, InvocationTargetException {
    final String id = "id";
    final String ref = "ref";
    final String fakeComponent = "component";
    BeanReference beanReference = new BeanReference(id);
    List<PropertyDefinition> propertyDefinitions = new ArrayList<>();
    PropertyDefinition propertyDefinition = new PropertyDefinition();
    propertyDefinition.setRef(ref);
    propertyDefinition.setName(id);
    propertyDefinitions.add(propertyDefinition);
    when(mockObjectDefinition.getProperties()).thenReturn(propertyDefinitions);
    when(mockContext.getComponent(eq(ref))).thenReturn(fakeComponent);
    subject.applyProperties(mockObjectDefinition, beanReference, mockContext);
    verify(mockContext).getComponent(same(ref));
    verify(mockObjectDefinition).getProperties();
}
Also used : ArrayList(java.util.ArrayList) BeanReference(org.apache.heron.eco.definition.BeanReference) PropertyDefinition(org.apache.heron.eco.definition.PropertyDefinition) Test(org.junit.Test)

Aggregations

PropertyDefinition (org.apache.heron.eco.definition.PropertyDefinition)4 Test (org.junit.Test)3 ArrayList (java.util.ArrayList)2 BeanReference (org.apache.heron.eco.definition.BeanReference)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 BeanDefinition (org.apache.heron.eco.definition.BeanDefinition)1 EcoTopologyDefinition (org.apache.heron.eco.definition.EcoTopologyDefinition)1