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.module.xml.functional;
8   
9   import org.mule.api.MuleException;
10  import org.mule.api.MuleMessage;
11  import org.mule.module.client.MuleClient;
12  
13  import java.io.IOException;
14  import java.util.Arrays;
15  import java.util.LinkedList;
16  import java.util.List;
17  
18  import org.dom4j.Document;
19  import org.dom4j.Element;
20  
21  import static org.junit.Assert.assertEquals;
22  import static org.junit.Assert.assertNotNull;
23  import static org.junit.Assert.assertTrue;
24  
25  public abstract class AbstractXmlSplitterOutboundFunctionalTestCase extends AbstractXmlFunctionalTestCase
26  {
27  
28      public static final String SERVICE_SPLITTER = "service splitter";
29      public static final String ROUND_ROBIN_DET = "round robin deterministic";
30      public static final String ROUND_ROBIN_INDET = "round robin indeterministic";
31      public static final String SPLITTER_ENDPOINT_PREFIX = "service";
32      public static final String ROUND_ROBIN_ENDPOINT_PREFIX = "robin";
33      public static final String NAME = "name";
34  
35      @Override
36      protected String getConfigResources()
37      {
38          return "org/mule/module/xml/xml-outbound-functional-test.xml";
39      }
40  
41      protected void doSend(String endpoint) throws IOException, MuleException
42      {
43          String xml = getConfigAsString();
44          MuleClient client = new MuleClient(muleContext);
45          client.dispatch(endpoint, xml, null);
46      }
47  
48      protected void assertService(String prefix, int index, String service) throws MuleException, IOException
49      {
50          MuleClient client = new MuleClient(muleContext);
51          MuleMessage response = client.request(prefix + index, TIMEOUT);
52          assertNotNull(response);
53          assertNotNull(response.getPayload());
54          assertTrue(response.getPayload().getClass().getName(), response.getPayload() instanceof Document);
55          Document document = (Document) response.getPayload();
56          assertEquals("service", document.getRootElement().getName());
57          Element element = document.getRootElement();
58          assertEquals(service, element.attributeValue(NAME));
59      }
60  
61      protected void assertServices(String prefix, int index, String[] services) throws MuleException, IOException
62      {
63          List remaining = new LinkedList(Arrays.asList(services)); // asList is immutable
64          while (remaining.size() > 0)
65          {
66              MuleClient client = new MuleClient(muleContext);
67              MuleMessage response = client.request(prefix + index, TIMEOUT);
68              assertNotNull(response);
69              assertNotNull(response.getPayload());
70              assertTrue(response.getPayload().getClass().getName(), response.getPayload() instanceof Document);
71              Document document = (Document) response.getPayload();
72              assertEquals("service", document.getRootElement().getName());
73              Element element = document.getRootElement();
74              String name = element.attributeValue(NAME);
75              assertTrue(name, remaining.contains(name));
76              int size = remaining.size();
77              remaining.remove(name);
78              // check we don't delete all instances of same value
79              // (apparently not - which makes sense, this is a list, not a set).
80              assertEquals(size, remaining.size() + 1);
81          }
82      }
83  
84  }