View Javadoc

1   /*
2    * $Id: AbstractXmlSplitterOutboundFunctionalTestCase.java 20321 2010-11-24 15:21:24Z dfeist $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.module.xml.functional;
12  
13  import org.mule.api.MuleException;
14  import org.mule.api.MuleMessage;
15  import org.mule.module.client.MuleClient;
16  
17  import java.io.IOException;
18  import java.util.Arrays;
19  import java.util.LinkedList;
20  import java.util.List;
21  
22  import org.dom4j.Document;
23  import org.dom4j.Element;
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      protected String getConfigResources()
36      {
37          return "org/mule/module/xml/xml-outbound-functional-test.xml";
38      }
39  
40      protected void doSend(String endpoint) throws IOException, MuleException
41      {
42          String xml = getConfigAsString();
43          MuleClient client = new MuleClient(muleContext);
44          client.dispatch(endpoint, xml, null);
45      }
46  
47      protected void assertService(String prefix, int index, String service) throws MuleException, IOException
48      {
49          MuleClient client = new MuleClient(muleContext);
50          MuleMessage response = client.request(prefix + index, TIMEOUT);
51          assertNotNull(response);
52          assertNotNull(response.getPayload());
53          assertTrue(response.getPayload().getClass().getName(), response.getPayload() instanceof Document);
54          Document document = (Document) response.getPayload();
55          assertEquals("service", document.getRootElement().getName());
56          Element element = document.getRootElement();
57          assertEquals(service, element.attributeValue(NAME));
58      }
59  
60      protected void assertServices(String prefix, int index, String[] services) throws MuleException, IOException
61      {
62          List remaining = new LinkedList(Arrays.asList(services)); // asList is immutable
63          while (remaining.size() > 0)
64          {
65              MuleClient client = new MuleClient(muleContext);
66              MuleMessage response = client.request(prefix + index, TIMEOUT);
67              assertNotNull(response);
68              assertNotNull(response.getPayload());
69              assertTrue(response.getPayload().getClass().getName(), response.getPayload() instanceof Document);
70              Document document = (Document) response.getPayload();
71              assertEquals("service", document.getRootElement().getName());
72              Element element = document.getRootElement();
73              String name = element.attributeValue(NAME);
74              assertTrue(name, remaining.contains(name));
75              int size = remaining.size();
76              remaining.remove(name);
77              // check we don't delete all instances of same value
78              // (apparently not - which makes sense, this is a list, not a set).
79              assertEquals(size, remaining.size() + 1);
80          }
81      }
82  
83  }