Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: dcf8ca352a38c90261e6ba4f50a03c5ff748aa8d (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
/*********************************************************************
 * Copyright (c) 2004, 2007 Boeing
 *
 * This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License 2.0
 * which is available at https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     Boeing - initial API and implementation
 **********************************************************************/

package org.eclipse.osee.orcs.db.internal.resource;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.Collection;
import org.eclipse.osee.framework.core.data.OseeClient;
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.IResourceProvider;
import org.eclipse.osee.framework.resource.management.util.ResourceLocator;
import org.eclipse.osee.orcs.SystemProperties;
import org.eclipse.osee.orcs.db.mocks.MockResource;
import org.eclipse.osee.orcs.db.mocks.MockSystemPreferences;
import org.eclipse.osee.orcs.db.mocks.Utility;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

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

   @Rule
   public TemporaryFolder folder = new TemporaryFolder();

   @Test
   public void testGetSupportedProtocols() {
      IResourceProvider provider = new AttributeProvider();
      Collection<String> protocols = provider.getSupportedProtocols();
      Assert.assertEquals(1, protocols.size());
      Assert.assertEquals("attr", protocols.iterator().next());
   }

   @Test
   public void testIsValid() throws URISyntaxException {
      IResourceProvider provider = new AttributeProvider();
      Assert.assertFalse(provider.isValid(null));
      Assert.assertFalse(provider.isValid(new ResourceLocator(new URI("http://hello"))));
      Assert.assertTrue(provider.isValid(new ResourceLocator(new URI("attr://hello"))));
   }

   @Test(expected = OseeCoreException.class)
   public void testInitializationException1() throws Exception {
      AttributeProvider provider = new AttributeProvider();
      provider.getAttributeDataPath();
   }

   @Test(expected = OseeCoreException.class)
   public void testInitializationException2() throws Exception {
      AttributeProvider provider = new AttributeProvider();
      provider.getBinaryDataPath();
   }

   @Test(expected = OseeCoreException.class)
   public void testInitializationException3() throws Exception {
      MockSystemPreferences properties = new MockSystemPreferences() {
         @Override
         public String getValue(String key) {
            Assert.assertEquals(OseeClient.OSEE_APPLICATION_SERVER_DATA, key);
            return null;
         }
      };
      AttributeProvider provider = new AttributeProvider();
      provider.setSystemProperties(properties);
      provider.start();
      provider.getAttributeDataPath();
   }

   @Test
   public void testOps() throws Exception {
      String rawData = Utility.generateData(4001);
      byte[] zippedData = Utility.asZipped(rawData, "testData.txt");

      SystemProperties properties = new MockSystemPreferences() {
         @Override
         public String getValue(String key) {
            Assert.assertEquals(OseeClient.OSEE_APPLICATION_SERVER_DATA, key);
            return folder.getRoot().getAbsolutePath();
         }
      };

      AttributeProvider provider = new AttributeProvider();
      provider.setSystemProperties(properties);
      provider.start();

      IResource resource = new MockResource("testData.txt", new URI("file://path"), zippedData, true);
      IResourceLocator locator = new ResourceLocator(new URI("attr://123/456/testData.zip"));

      PropertyStore options = new PropertyStore();
      IResourceLocator savedLocator = provider.save(locator, resource, options);

      Assert.assertEquals("123/456/testData.zip", savedLocator.getRawPath());
      Assert.assertEquals("attr://123/456/testData.zip", savedLocator.getLocation().toASCIIString());
      Assert.assertEquals(ResourceConstants.ATTRIBUTE_RESOURCE_PROTOCOL, savedLocator.getProtocol());

      //check file was written
      File file = new File(folder.getRoot().getAbsolutePath() + "/attr/123/456/", "testData.zip");
      Assert.assertTrue(file.exists());

      byte[] actual = Lib.fileToBytes(file);
      Assert.assertTrue(Arrays.equals(zippedData, actual));

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

      Assert.assertTrue(provider.exists(savedLocator));

      IResource remoteResource = provider.acquire(savedLocator, options);

      byte[] resourceBytes = null;
      InputStream inputStream = null;
      try {
         inputStream = remoteResource.getContent();
         resourceBytes = Lib.inputStreamToBytes(inputStream);
      } finally {
         Lib.close(inputStream);
      }

      Assert.assertTrue(Arrays.equals(zippedData, resourceBytes));

      ByteArrayOutputStream outputStream1 = new ByteArrayOutputStream();
      String fileName1 = Lib.decompressStream(new ByteArrayInputStream(resourceBytes), outputStream1);
      Assert.assertEquals("testData.txt", fileName1);
      Assert.assertEquals(rawData, outputStream1.toString("UTF-8"));

      Assert.assertNotNull(resourceBytes);

      int result = provider.delete(savedLocator);
      Assert.assertEquals(IResourceManager.OK, result);

      Assert.assertFalse(provider.exists(savedLocator));
      Assert.assertFalse(file.exists());
   }
}

Back to the top