View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.construct.builder;
8   
9   import org.mule.component.AbstractJavaComponent;
10  import org.mule.component.DefaultJavaComponent;
11  import org.mule.component.SimpleCallableJavaComponent;
12  import org.mule.component.simple.EchoComponent;
13  import org.mule.construct.SimpleService;
14  import org.mule.construct.SimpleService.Type;
15  import org.mule.exception.DefaultServiceExceptionStrategy;
16  import org.mule.tck.junit4.AbstractMuleContextTestCase;
17  import org.mule.tck.services.SimpleMathsComponent;
18  import org.mule.transformer.compression.GZipCompressTransformer;
19  import org.mule.transformer.simple.ObjectToByteArray;
20  import org.mule.transformer.simple.StringAppendTransformer;
21  
22  import org.junit.Test;
23  
24  import static org.junit.Assert.assertEquals;
25  
26  public class SimpleServiceBuilderTestCase extends AbstractMuleContextTestCase
27  {
28      @Test
29      public void testFullConfiguration() throws Exception
30      {
31          SimpleService simpleService = new SimpleServiceBuilder().name("test-simple-service-full")
32              .inboundAddress("test://foo")
33              .inboundTransformers(new StringAppendTransformer("bar"))
34              .inboundResponseTransformers(new ObjectToByteArray(), new GZipCompressTransformer())
35              .component(EchoComponent.class)
36              .type(Type.DIRECT)
37              .exceptionStrategy(new DefaultServiceExceptionStrategy(muleContext))
38              .build(muleContext);
39  
40          assertEquals("test-simple-service-full", simpleService.getName());
41          assertEquals(EchoComponent.class,
42              ((AbstractJavaComponent) simpleService.getComponent()).getObjectType());
43      }
44  
45      @Test
46      public void testShortConfiguration() throws Exception
47      {
48          SimpleService simpleService = new SimpleServiceBuilder().name("test-simple-service-short")
49              .inboundEndpoint(getTestInboundEndpoint("test"))
50              .component(new EchoComponent())
51              .build(muleContext);
52  
53          assertEquals("test-simple-service-short", simpleService.getName());
54          assertEquals(EchoComponent.class,
55              ((SimpleCallableJavaComponent) simpleService.getComponent()).getObjectType());
56      }
57  
58      @Test
59      public void testPojoComponentConfiguration() throws Exception
60      {
61          SimpleMathsComponent pojoComponent = new SimpleMathsComponent();
62  
63          SimpleService simpleService = new SimpleServiceBuilder().name("test-simple-service-pojo-component")
64              .inboundEndpoint(getTestInboundEndpoint("test"))
65              .component(pojoComponent)
66              .build(muleContext);
67  
68          assertEquals("test-simple-service-pojo-component", simpleService.getName());
69          assertEquals(pojoComponent, ((DefaultJavaComponent) simpleService.getComponent()).getObjectFactory()
70              .getInstance(muleContext));
71      }
72  }