1 17 package org.alfresco.example.webservice.content; 18 19 import java.io.InputStream ; 20 import java.net.URL ; 21 import java.net.URLConnection ; 22 23 import javax.xml.rpc.ServiceException ; 24 25 import junit.framework.AssertionFailedError; 26 27 import org.alfresco.example.webservice.BaseWebServiceSystemTest; 28 import org.alfresco.example.webservice.types.Content; 29 import org.alfresco.example.webservice.types.ContentFormat; 30 import org.alfresco.example.webservice.types.ParentReference; 31 import org.alfresco.example.webservice.types.Predicate; 32 import org.alfresco.example.webservice.types.Reference; 33 import org.apache.axis.EngineConfiguration; 34 import org.apache.axis.configuration.FileProvider; 35 import org.apache.commons.logging.Log; 36 import org.apache.commons.logging.LogFactory; 37 38 public class ContentServiceSystemTest extends BaseWebServiceSystemTest 39 { 40 private static Log logger = LogFactory.getLog(ContentServiceSystemTest.class); 41 private static final String CONTENT = "This is a small piece of content to test the create service call"; 42 private static final String UPDATED_CONTENT = "This is some updated content to test the write service call"; 43 44 private static String newContentId; 45 private ContentServiceSoapBindingStub contentService; 46 private String fileName = "unit-test.txt"; 47 48 49 @Override 50 protected void setUp() throws Exception 51 { 52 super.setUp(); 53 54 try 55 { 56 EngineConfiguration config = new FileProvider(getResourcesDir(), "client-deploy.wsdd"); 57 this.contentService = (ContentServiceSoapBindingStub)new ContentServiceLocator(config).getContentService(); 58 } 59 catch (ServiceException jre) 60 { 61 if (jre.getLinkedCause() != null) 62 { 63 jre.getLinkedCause().printStackTrace(); 64 } 65 66 throw new AssertionFailedError("JAX-RPC ServiceException caught: " + jre); 67 } 68 69 assertNotNull("contentService is null", this.contentService); 70 71 this.contentService.setTimeout(60000); 73 } 74 75 80 public void testCreate() throws Exception 81 { 82 ParentReference root = new ParentReference(); 84 root.setStore(STORE); 85 root.setUuid(companyHomeId); 86 87 String mimetype = "text/plain"; 88 Content content = this.contentService.create(root, this.fileName, new ContentFormat(mimetype, "UTF-8"), CONTENT.getBytes()); 89 assertNotNull("returned content should not be null", content); 90 assertNotNull("format should not be null", content.getFormat()); 91 assertEquals("Mimetype should match what was sent", mimetype, content.getFormat().getMimetype()); 92 newContentId = content.getReference().getUuid(); 93 logger.debug("created new content with id: " + newContentId); 94 } 95 96 101 public void testRead() throws Exception 102 { 103 if (newContentId == null) 104 { 105 fail("Failed to locate id of " + this.fileName); 106 } 107 108 Reference node = new Reference(); 109 node.setStore(STORE); 110 node.setUuid(newContentId); 111 112 ReadResult result = this.contentService.read(node); 113 assertNotNull("read result should not be null", result); 114 logger.debug("url for download is: " + result.getUrl()); 115 assertNotNull("Url to read content from must not be null", result.getUrl()); 116 117 assertEquals("Content length's don't match", CONTENT.length(), result.getContent().getLength()); 119 } 120 121 126 public void testWrite() throws Exception 127 { 128 if (newContentId == null) 129 { 130 fail("Failed to locate id of " + this.fileName); 131 } 132 133 Reference node = new Reference(); 134 node.setStore(STORE); 135 node.setUuid(newContentId); 136 137 this.contentService.write(node, UPDATED_CONTENT.getBytes()); 138 139 ReadResult result = this.contentService.read(node); 140 assertNotNull("read result should not be null", result); 141 assertNotNull("Url to read content from must not be null", result.getUrl()); 142 143 long contentLength = result.getContent().getLength(); 144 assertEquals("Content length's don't match", UPDATED_CONTENT.length(), contentLength); 145 assertTrue("Content length of update content should not be the same as the previous content length", 146 contentLength != CONTENT.length()); 147 148 151 152 StringBuilder readContent = new StringBuilder (); 154 URL url = new URL (result.getUrl()); 155 URLConnection conn = url.openConnection(); 156 InputStream is = conn.getInputStream(); 157 int read = is.read(); 158 while (read != -1) 159 { 160 readContent.append((char)read); 161 read = is.read(); 162 } 163 164 logger.debug("Content from repository: " + readContent.toString()); 166 assertEquals("Content does not match", UPDATED_CONTENT, readContent.toString()); 167 } 168 169 174 public void testExists() throws Exception 175 { 176 if (newContentId == null) 177 { 178 fail("Failed to locate id of " + this.fileName); 179 } 180 181 Reference ref = new Reference(); 183 ref.setStore(STORE); 184 ref.setUuid(newContentId); 185 Predicate predicate = new Predicate(new Reference[] {ref}, null, null); 186 187 ExistsResult[] existsResult = this.contentService.exists(predicate); 188 assertNotNull("exists result should not be null", existsResult); 189 190 assertTrue("There should be one result", existsResult.length == 1); 192 assertTrue("The node should have existed", existsResult[0].isExists()); 193 assertTrue("Content length should match", existsResult[0].getLength() == UPDATED_CONTENT.length()); 194 } 195 196 201 public void testDescribe() throws Exception 202 { 203 if (newContentId == null) 204 { 205 fail("Failed to locate id of " + this.fileName); 206 } 207 208 Reference ref = new Reference(); 210 ref.setStore(STORE); 211 ref.setUuid(newContentId); 212 Predicate predicate = new Predicate(new Reference[] {ref}, null, null); 213 214 Content[] contentDesc = this.contentService.describe(predicate); 215 assertNotNull("describe result should not be null", contentDesc); 216 217 assertTrue("There should be one result", contentDesc.length == 1); 219 220 Content content = contentDesc[0]; 222 String id = content.getReference().getUuid(); 223 String type = content.getType(); 224 String mimetype = content.getFormat().getMimetype(); 225 String encoding = content.getFormat().getEncoding(); 226 long length = content.getLength(); 227 if (logger.isDebugEnabled()) 228 { 229 logger.debug("id = " + id); 230 logger.debug("type = " + type); 231 logger.debug("mimetype = " + mimetype); 232 logger.debug("encoding = " + encoding); 233 logger.debug("length = " + length); 234 } 235 236 assertEquals("The id is incorrect", newContentId, id); 238 assertEquals("The type is incorrect", "{http://www.alfresco.org/model/content/1.0}content", type); 239 assertEquals("The mimetype is incorrect", "text/plain", mimetype); 240 assertEquals("The encoding is incorrect", "UTF-8", encoding); 241 assertEquals("The length is incorrect", UPDATED_CONTENT.length(), length); 242 } 243 244 249 public void testDelete() throws Exception 250 { 251 if (newContentId == null) 252 { 253 fail("Failed to locate id of " + this.fileName); 254 } 255 256 Reference ref = new Reference(); 258 ref.setStore(STORE); 259 ref.setUuid(newContentId); 260 Predicate predicate = new Predicate(new Reference[] {ref}, null, null); 261 262 Reference[] refs = this.contentService.delete(predicate); 263 assertNotNull("delete result should not be null", refs); 264 265 ExistsResult[] existsResult = this.contentService.exists(predicate); 267 assertNotNull("exists result should not be null", existsResult); 268 assertFalse("The node should no longer exist", existsResult[0].isExists()); 269 } 270 } 271 | Popular Tags |