Skip to main content
summaryrefslogtreecommitdiffstats
blob: af90338d37d40d62e33a33ee83bb314b25e77c08 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.orcs.db.internal.proxy;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URI;
import java.util.Arrays;
import org.eclipse.osee.framework.core.exception.OseeDataStoreException;
import org.eclipse.osee.framework.core.exception.OseeExceptions;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.framework.jdk.core.type.PropertyStore;
import org.eclipse.osee.framework.jdk.core.util.Lib;
import org.eclipse.osee.framework.resource.management.IResource;
import org.eclipse.osee.framework.resource.management.IResourceLocator;
import org.eclipse.osee.framework.resource.management.IResourceManager;
import org.eclipse.osee.framework.resource.management.util.ResourceLocator;
import org.eclipse.osee.orcs.db.mocks.MockResource;
import org.eclipse.osee.orcs.db.mocks.MockResourceManager;
import org.eclipse.osee.orcs.db.mocks.MockResourceNameResolver;
import org.eclipse.osee.orcs.db.mocks.Utility;
import org.junit.Assert;
import org.junit.Test;

/**
 * Test Case for {@link ResourceHandler}
 * 
 * @author Roberto E. Escobar
 */
public class ResourceHandlerTest {

   @Test(expected = OseeCoreException.class)
   public void testAcquireException() throws OseeCoreException {
      DataResource resource = new DataResource();
      ResourceHandler handler = new ResourceHandler(null);
      handler.acquire(resource);
   }

   @Test(expected = OseeCoreException.class)
   public void testAcquireException2() throws OseeCoreException {
      DataResource resource = new DataResource();
      resource.setLocator("MyLocator");
      MockResourceManager resMgr = new MockResourceManager();
      ResourceHandler handler = new ResourceHandler(resMgr);
      handler.acquire(resource);
   }

   @Test
   public void testAcquire() throws Exception {
      String data = Utility.generateData(1000);
      byte[] zippedData = Utility.asZipped(data, "internalFileName");

      DataResource resource = new DataResource();
      resource.setLocator("MyLocator");
      resource.setResolver(new MockResourceNameResolver("storageName", "internalFileName"));

      final URI uri = new URI("file://mylocation.zip");
      final ResourceLocator locator = new ResourceLocator(uri);
      final MockResource remoteResource = new MockResource("data.zip", uri, zippedData, true);
      MockResourceManager resMgr = new MockResourceManager() {

         @Override
         public IResource acquire(IResourceLocator actualLoc, PropertyStore options) {
            Assert.assertEquals(locator, actualLoc);
            return remoteResource;
         }

         @Override
         public IResourceLocator getResourceLocator(String path) {
            Assert.assertEquals("MyLocator", path);
            return locator;
         }
      };
      ResourceHandler handler = new ResourceHandler(resMgr);
      byte[] actual = handler.acquire(resource);

      ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
      String fileName = Lib.decompressStream(new ByteArrayInputStream(actual), outputStream);
      Assert.assertEquals("internalFileName", fileName);
      Assert.assertEquals(data, outputStream.toString("UTF-8"));

      Assert.assertEquals("zip", resource.getExtension());
      Assert.assertEquals("application/zip", resource.getContentType());
      Assert.assertEquals("ISO-8859-1", resource.getEncoding());
   }

   @Test
   public void testSave() throws Exception {
      String data = Utility.generateData(1000);
      final byte[] zippedData = Utility.asZipped(data, "internalFileName");

      final int storageId = 45;
      DataResource resource = new DataResource("application/osee", "UTF-8", "osee", "attr://123/123/hello.txt");
      resource.setResolver(new MockResourceNameResolver("storageName", "internalFileName"));
      final URI uri = new URI("file://mylocation.zip");
      final ResourceLocator locator = new ResourceLocator(uri);
      MockResourceManager resMgr = new MockResourceManager() {

         @Override
         public IResourceLocator save(IResourceLocator actualLoc, IResource resource, PropertyStore options) throws OseeCoreException {
            Assert.assertEquals(locator, actualLoc);
            Assert.assertEquals(false, resource.isCompressed());
            try {
               byte[] data = Lib.inputStreamToBytes(resource.getContent());
               Assert.assertTrue(Arrays.equals(zippedData, data));
            } catch (IOException ex) {
               OseeExceptions.wrapAndThrow(ex);
            }
            return locator;
         }

         @Override
         public IResourceLocator generateResourceLocator(String protocol, String seed, String name) {
            Assert.assertEquals("attr", protocol);
            Assert.assertEquals(String.valueOf(storageId), seed);
            Assert.assertEquals("storageName.osee", name);
            return locator;
         }
      };

      ResourceHandler handler = new ResourceHandler(resMgr);
      Assert.assertEquals("attr://123/123/hello.txt", resource.getLocator());
      handler.save(storageId, resource, zippedData);
      Assert.assertEquals("file://mylocation.zip", resource.getLocator());
   }

   @Test(expected = OseeCoreException.class)
   public void testDeleteException() throws OseeCoreException {
      DataResource resource = new DataResource();
      ResourceHandler handler = new ResourceHandler(null);
      handler.delete(resource);
   }

   @Test(expected = OseeCoreException.class)
   public void testDeleteException2() throws OseeCoreException {
      DataResource resource = new DataResource();
      resource.setLocator("MyLocator");
      MockResourceManager resMgr = new MockResourceManager();
      ResourceHandler handler = new ResourceHandler(resMgr);
      handler.delete(resource);
   }

   @Test(expected = OseeDataStoreException.class)
   public void testDeleteFail() throws Exception {
      DataResource resource = new DataResource();
      resource.setLocator("MyLocator");
      resource.setResolver(new MockResourceNameResolver("storageName", "internalFileName"));

      final URI uri = new URI("file://mylocation");
      final ResourceLocator locator = new ResourceLocator(uri);
      MockResourceManager resMgr = new MockResourceManager() {
         @Override
         public int delete(IResourceLocator actualLoc) {
            Assert.assertEquals(locator, actualLoc);
            return IResourceManager.FAIL;
         }

         @Override
         public IResourceLocator getResourceLocator(String path) {
            Assert.assertEquals("MyLocator", path);
            return locator;
         }
      };
      ResourceHandler handler = new ResourceHandler(resMgr);
      handler.delete(resource);
   }

   @Test(expected = OseeDataStoreException.class)
   public void testDeleteResourceNotFound() throws Exception {
      DataResource resource = new DataResource();
      resource.setLocator("MyLocator");
      resource.setResolver(new MockResourceNameResolver("storageName", "internalFileName"));

      final URI uri = new URI("file://mylocation");
      final ResourceLocator locator = new ResourceLocator(uri);
      MockResourceManager resMgr = new MockResourceManager() {
         @Override
         public int delete(IResourceLocator actualLoc) {
            Assert.assertEquals(locator, actualLoc);
            return IResourceManager.RESOURCE_NOT_FOUND;
         }

         @Override
         public IResourceLocator getResourceLocator(String path) {
            Assert.assertEquals("MyLocator", path);
            return locator;
         }
      };
      ResourceHandler handler = new ResourceHandler(resMgr);
      handler.delete(resource);
   }

   @Test
   public void testDeleteOk() throws Exception {
      DataResource resource = new DataResource();
      resource.setLocator("MyLocator");
      resource.setResolver(new MockResourceNameResolver("storageName", "internalFileName"));

      final URI uri = new URI("file://mylocation");
      final ResourceLocator locator = new ResourceLocator(uri);
      MockResourceManager resMgr = new MockResourceManager() {
         @Override
         public int delete(IResourceLocator actualLoc) {
            Assert.assertEquals(locator, actualLoc);
            return IResourceManager.OK;
         }

         @Override
         public IResourceLocator getResourceLocator(String path) {
            Assert.assertEquals("MyLocator", path);
            return locator;
         }
      };
      ResourceHandler handler = new ResourceHandler(resMgr);
      handler.delete(resource);
   }

}

Back to the top