PageRenderTime 25ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/servers/sip-presence/xdm/server/tests/src/test/java/org/openxdm/xcap/client/test/error/CannotInsertTest.java

http://mobicents.googlecode.com/
Java | 220 lines | 119 code | 39 blank | 62 comment | 35 complexity | 05b4c310da58583ca06b64ed9e501215 MD5 | raw file
Possible License(s): LGPL-3.0, GPL-3.0, LGPL-2.1, GPL-2.0, CC-BY-SA-3.0, CC0-1.0, Apache-2.0, BSD-3-Clause
  1. /*
  2. * JBoss, Home of Professional Open Source
  3. * Copyright 2011, Red Hat, Inc. and individual contributors
  4. * by the @authors tag. See the copyright.txt in the distribution for a
  5. * full listing of individual contributors.
  6. *
  7. * This is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This software is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this software; if not, write to the Free
  19. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21. */
  22. package org.openxdm.xcap.client.test.error;
  23. import static org.junit.Assert.assertTrue;
  24. import java.io.IOException;
  25. import java.util.LinkedList;
  26. import javax.xml.bind.JAXBException;
  27. import junit.framework.JUnit4TestAdapter;
  28. import org.apache.commons.httpclient.HttpException;
  29. import org.junit.Test;
  30. import org.openxdm.xcap.client.Response;
  31. import org.openxdm.xcap.client.test.AbstractXDMJunitOldClientTest;
  32. import org.openxdm.xcap.common.error.CannotInsertConflictException;
  33. import org.openxdm.xcap.common.key.UserAttributeUriKey;
  34. import org.openxdm.xcap.common.key.UserDocumentUriKey;
  35. import org.openxdm.xcap.common.key.UserElementUriKey;
  36. import org.openxdm.xcap.common.resource.AttributeResource;
  37. import org.openxdm.xcap.common.resource.ElementResource;
  38. import org.openxdm.xcap.common.uri.AttributeSelector;
  39. import org.openxdm.xcap.common.uri.ElementSelector;
  40. import org.openxdm.xcap.common.uri.ElementSelectorStep;
  41. import org.openxdm.xcap.common.uri.ElementSelectorStepByAttr;
  42. import org.openxdm.xcap.common.uri.ElementSelectorStepByPos;
  43. import org.openxdm.xcap.common.uri.ElementSelectorStepByPosAttr;
  44. public class CannotInsertTest extends AbstractXDMJunitOldClientTest {
  45. public static junit.framework.Test suite() {
  46. return new JUnit4TestAdapter(CannotInsertTest.class);
  47. }
  48. @Test
  49. public void test() throws HttpException, IOException, JAXBException, InterruptedException {
  50. // create uri
  51. UserDocumentUriKey key = new UserDocumentUriKey(appUsage.getAUID(),user,documentName);
  52. String content =
  53. "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
  54. "<resource-lists xmlns=\"urn:ietf:params:xml:ns:resource-lists\">" +
  55. "<list name=\"friends\" />" +
  56. "</resource-lists>";
  57. // send put request and get response
  58. Response response = client.put(key,appUsage.getMimetype(),content,null);
  59. // check put response
  60. assertTrue("Put response must exists",response != null);
  61. assertTrue("Put response code should be 201",response.getCode() == 201);
  62. // create element selector
  63. LinkedList<ElementSelectorStep> elementSelectorSteps = new LinkedList<ElementSelectorStep>();
  64. ElementSelectorStep step1 = new ElementSelectorStep("resource-lists");
  65. ElementSelectorStep step2 = new ElementSelectorStepByAttr("list","name","friends");
  66. elementSelectorSteps.add(step1);
  67. elementSelectorSteps.addLast(step2);
  68. ElementSelector elementSelector = new ElementSelector(elementSelectorSteps);
  69. // create exception for return codes
  70. CannotInsertConflictException exception = new CannotInsertConflictException();
  71. // 1st test is for attr replace
  72. // create attr uri
  73. AttributeSelector attributeSelector = new AttributeSelector("name");
  74. UserAttributeUriKey attrKey = new UserAttributeUriKey(appUsage.getAUID(),user,documentName,elementSelector,attributeSelector,null);
  75. // send put and get response
  76. Response putResponse = client.put(attrKey,AttributeResource.MIMETYPE,"enemies",null);
  77. assertTrue("Put response must exists",putResponse != null);
  78. assertTrue("Put response content must be the expected and the response code should be "+exception.getResponseStatus(),putResponse.getCode() == exception.getResponseStatus() && putResponse.getContent().equals(exception.getResponseContent()));
  79. // 2nd is for elem replace
  80. // create elem uri
  81. UserElementUriKey elementKey = new UserElementUriKey(appUsage.getAUID(),user,documentName,elementSelector,null);
  82. // element content to put
  83. String elementContent = "<list name=\"enemies\" />";
  84. // send put request and get response
  85. putResponse = client.put(elementKey,ElementResource.MIMETYPE,elementContent,null);
  86. assertTrue("Put response must exists",putResponse != null);
  87. assertTrue("Put response content must be the expected and the response code should be "+exception.getResponseStatus(),putResponse.getCode() == exception.getResponseStatus() && putResponse.getContent().equals(exception.getResponseContent()));
  88. // element content to put
  89. elementContent = "<cannot-insert name=\"friends\" />";
  90. // send put request and get response
  91. putResponse = client.put(elementKey,ElementResource.MIMETYPE,elementContent,null);
  92. assertTrue("Put response must exists",putResponse != null);
  93. assertTrue("Put response content must be the expected and the response code should be "+exception.getResponseStatus(),putResponse.getCode() == exception.getResponseStatus() && putResponse.getContent().equals(exception.getResponseContent()));
  94. // 3rd is for new elem put by pos and attr
  95. // create elem uri
  96. elementSelectorSteps.removeLast();
  97. step2 = new ElementSelectorStepByPosAttr("list",1,"name","3n3m13s");
  98. elementSelectorSteps.addLast(step2);
  99. elementKey = new UserElementUriKey(appUsage.getAUID(),user,documentName,elementSelector,null);
  100. // element content to put
  101. elementContent = "<list name=\"enemies\" />";
  102. // send put request and get response
  103. putResponse = client.put(elementKey,ElementResource.MIMETYPE,elementContent,null);
  104. assertTrue("Put response must exists",putResponse != null);
  105. assertTrue("Put response content must be the expected and the response code should be "+exception.getResponseStatus(),putResponse.getCode() == exception.getResponseStatus() && putResponse.getContent().equals(exception.getResponseContent()));
  106. // element content to put
  107. elementContent = "<cannot-insert name=\"3n3m13s\" />";
  108. // send put request and get response
  109. putResponse = client.put(elementKey,ElementResource.MIMETYPE,elementContent,null);
  110. assertTrue("Put response must exists",putResponse != null);
  111. assertTrue("Put response content must be the expected and the response code should be "+exception.getResponseStatus(),putResponse.getCode() == exception.getResponseStatus() && putResponse.getContent().equals(exception.getResponseContent()));
  112. // create elem uri
  113. elementSelectorSteps.removeLast();
  114. step2 = new ElementSelectorStepByPosAttr("list",3,"name","3n3m13s");
  115. elementSelectorSteps.addLast(step2);
  116. elementKey = new UserElementUriKey(appUsage.getAUID(),user,documentName,elementSelector,null);
  117. // element content to put
  118. elementContent = "<list name=\"3n3m13s\" />";
  119. // send put request and get response
  120. putResponse = client.put(elementKey,ElementResource.MIMETYPE,elementContent,null);
  121. assertTrue("Put response must exists",putResponse != null);
  122. assertTrue("Put response content must be the expected and the response code should be "+exception.getResponseStatus(),putResponse.getCode() == exception.getResponseStatus() && putResponse.getContent().equals(exception.getResponseContent()));
  123. // 4th is for new elem put by pos
  124. // create elem uri
  125. elementSelectorSteps.removeLast();
  126. step2 = new ElementSelectorStepByPos("list",2);
  127. elementSelectorSteps.addLast(step2);
  128. elementKey = new UserElementUriKey(appUsage.getAUID(),user,documentName,elementSelector,null);
  129. // element content to put
  130. elementContent = "<cannot-insert name=\"3n3m13s\" />";
  131. // send put request and get response
  132. putResponse = client.put(elementKey,ElementResource.MIMETYPE,elementContent,null);
  133. assertTrue("Put response must exists",putResponse != null);
  134. assertTrue("Put response content must be the expected and the response code should be "+exception.getResponseStatus(),putResponse.getCode() == exception.getResponseStatus() && putResponse.getContent().equals(exception.getResponseContent()));
  135. // create elem uri
  136. elementSelectorSteps.removeLast();
  137. step2 = new ElementSelectorStepByPos("list",3);
  138. elementSelectorSteps.addLast(step2);
  139. elementKey = new UserElementUriKey(appUsage.getAUID(),user,documentName,elementSelector,null);
  140. // element content to put
  141. elementContent = "<list name=\"3n3m13s\" />";
  142. // send put request and get response
  143. putResponse = client.put(elementKey,ElementResource.MIMETYPE,elementContent,null);
  144. assertTrue("Put response must exists",putResponse != null);
  145. assertTrue("Put response content must be the expected and the response code should be "+exception.getResponseStatus(),putResponse.getCode() == exception.getResponseStatus() && putResponse.getContent().equals(exception.getResponseContent()));
  146. // 5th is for new elem put by attr
  147. // create elem uri
  148. elementSelectorSteps.removeLast();
  149. step2 = new ElementSelectorStepByAttr("list","name","3n3m13s");
  150. elementSelectorSteps.addLast(step2);
  151. elementKey = new UserElementUriKey(appUsage.getAUID(),user,documentName,elementSelector,null);
  152. // element content to put
  153. elementContent = "<list name=\"enemies\" />";
  154. // send put request and get response
  155. putResponse = client.put(elementKey,ElementResource.MIMETYPE,elementContent,null);
  156. assertTrue("Put response must exists",putResponse != null);
  157. assertTrue("Put response content must be the expected and the response code should be "+exception.getResponseStatus(),putResponse.getCode() == exception.getResponseStatus() && putResponse.getContent().equals(exception.getResponseContent()));
  158. // element content to put
  159. elementContent = "<cannot-insert name=\"3n3m13s\" />";
  160. // send put request and get response
  161. putResponse = client.put(elementKey,ElementResource.MIMETYPE,elementContent,null);
  162. assertTrue("Put response must exists",putResponse != null);
  163. assertTrue("Put response content must be the expected and the response code should be "+exception.getResponseStatus(),putResponse.getCode() == exception.getResponseStatus() && putResponse.getContent().equals(exception.getResponseContent()));
  164. // 6th is for new elem put by name only
  165. // create elem uri
  166. elementSelectorSteps.removeLast();
  167. step2 = new ElementSelectorStep("list");
  168. elementSelectorSteps.addLast(step2);
  169. ElementSelectorStep step3 = new ElementSelectorStep("list");
  170. elementSelectorSteps.addLast(step3);
  171. elementKey = new UserElementUriKey(appUsage.getAUID(),user,documentName,elementSelector,null);
  172. // element content to put
  173. elementContent = "<cannot-insert name=\"3n3m13s\" />";
  174. // send put request and get response
  175. putResponse = client.put(elementKey,ElementResource.MIMETYPE,elementContent,null);
  176. assertTrue("Put response must exists",putResponse != null);
  177. assertTrue("Put response content must be the expected and the response code should be "+exception.getResponseStatus(),putResponse.getCode() == exception.getResponseStatus() && putResponse.getContent().equals(exception.getResponseContent()));
  178. // clean up
  179. client.delete(key,null);
  180. }
  181. }