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.tck.testmodels.services;
8   
9   import org.mule.api.component.simple.EchoService;
10  import org.mule.api.lifecycle.Disposable;
11  import org.mule.tck.functional.FunctionalTestComponent;
12  import org.mule.util.StringUtils;
13  
14  import java.util.Collections;
15  import java.util.Date;
16  import java.util.HashMap;
17  import java.util.Map;
18  
19  /**
20   * <code>TestServiceComponent</code> is a test WebServices service.
21   */
22  public class TestServiceComponent extends FunctionalTestComponent
23      implements EchoService, DateService, PeopleService, Disposable
24  {
25      // we keep two collections - one static for testing the return of complex types
26      // and one for modifying by methods invoked on the TestComponent instance
27  
28      private static final Person[] originalPeople = new Person[]{new Person("Barney", "Rubble"),
29          new Person("Fred", "Flintstone"), new Person("Wilma", "Flintstone")};
30  
31      private final Map people = Collections.synchronizedMap(new HashMap());
32  
33      public TestServiceComponent()
34      {
35          super();
36          people.put("Barney", originalPeople[0]);
37          people.put("Fred", originalPeople[1]);
38          people.put("Wilma", originalPeople[2]);
39      }
40  
41      public String echo(String echo)
42      {
43          return echo;
44      }
45  
46      public String getDate()
47      {
48          return new Date().toString();
49      }
50  
51      public Person getPerson(String firstName)
52      {
53          if (StringUtils.isEmpty(firstName))
54          {
55              throw new IllegalArgumentException("Name parameter cannot be null");
56          }
57          return (Person)people.get(firstName);
58      }
59  
60      public Person[] getPeople()
61      {
62          return originalPeople;
63      }
64  
65      public void addPerson(Person person) throws Exception
66      {
67          if (person == null || person.getFirstName() == null || person.getLastName() == null)
68          {
69              throw new IllegalArgumentException("null person, first name or last name");
70          }
71          if (person.getFirstName().equals("Ross"))
72          {
73              throw new Exception("Ross is banned");
74          }
75          people.put(person.getFirstName(), person);
76          logger.debug("Added Person: " + person);
77      }
78  
79      public Person addPerson(String firstname, String surname) throws Exception
80      {
81          Person p = new Person(firstname, surname);
82          addPerson(p);
83          logger.debug("Added Person: " + p);
84          return p;
85      }
86  
87      /**
88       * A lifecycle method where implementor should free up any resources. If an
89       * exception is thrown it should just be logged and processing should continue.
90       * This method should not throw RuntimeExceptions.
91       */
92      public void dispose()
93      {
94          people.clear();
95      }
96  
97  }