Skip to main content
summaryrefslogtreecommitdiffstats
blob: 6956059b30b78b598eefba03cb316294cb3d98fc (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
/*******************************************************************************
 * Copyright (c) 2012 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.loader;

import org.junit.Assert;
import org.eclipse.osee.framework.core.exception.OseeArgumentException;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.model.cache.AttributeTypeCache;
import org.eclipse.osee.framework.core.model.type.AttributeType;
import org.eclipse.osee.orcs.core.ds.DataProxy;
import org.eclipse.osee.orcs.core.ds.DataProxyFactory;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;

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

   @Rule
   public ExpectedException thrown = ExpectedException.none();

   //@formatter:off
   @Mock DataProxyFactoryProvider proxyProvider;
   @Mock AttributeTypeCache attributeTypeCache;
   @Mock AttributeType attributeType;
   @Mock DataProxyFactory dataProxyFactory;
   @Mock DataProxy proxy;
   //@formatter:on

   private AttributeDataProxyFactory factory;

   @Before
   public void setUp() {
      MockitoAnnotations.initMocks(this);
      factory = new AttributeDataProxyFactory(proxyProvider, attributeTypeCache);
   }

   @Test
   public void testCreateFromDataArrayNull() throws OseeCoreException {
      long typeUuid = 45L;

      thrown.expect(OseeArgumentException.class);
      thrown.expectMessage("data cannot be null");
      factory.createProxy(typeUuid, (Object[]) null);
   }

   @Test
   public void testCreateFromDataArrayNotEnoughFields() throws OseeCoreException {
      long typeUuid = 45L;
      Object[] data = new Object[] {"hello"};

      thrown.expect(OseeArgumentException.class);
      thrown.expectMessage("Data must have at least [2] elements - size was [1]");
      factory.createProxy(typeUuid, data);
   }

   @Test
   public void testCreateFromDataArrayTypeNotFound() throws OseeCoreException {
      long typeUuid = 45L;
      Object[] data = new Object[] {"hello", "uri"};

      thrown.expect(OseeArgumentException.class);
      thrown.expectMessage("AttributeType cannot be null - Unable to find attributeType for [45]");

      factory.createProxy(typeUuid, data);
   }

   @Test
   public void testCreateFromStringsNullDataProxyFactory() throws OseeCoreException {
      long typeUuid = 45L;
      String value = "hello";
      String uri = "theUri";

      Mockito.when(attributeTypeCache.getByGuid(45L)).thenReturn(attributeType);
      Mockito.when(attributeType.getAttributeProviderId()).thenReturn("org.eclipse.proxyfactory");

      thrown.expect(OseeArgumentException.class);
      thrown.expectMessage("DataProxyFactory cannot be null - Unable to find data proxy factory for [proxyfactory]");
      factory.createProxy(typeUuid, value, uri);
   }

   @Test
   public void testCreateFromStrings() throws OseeCoreException {
      long typeUuid = 45L;
      String value = "hello";
      String uri = "theUri";

      Mockito.when(attributeTypeCache.getByGuid(45L)).thenReturn(attributeType);
      Mockito.when(attributeType.getAttributeProviderId()).thenReturn("org.eclipse.proxyfactory");
      Mockito.when(proxyProvider.getFactory("proxyfactory")).thenReturn(dataProxyFactory);
      Mockito.when(dataProxyFactory.createInstance("proxyfactory")).thenReturn(proxy);

      DataProxy theProxy = factory.createProxy(typeUuid, value, uri);

      Assert.assertEquals(proxy, theProxy);
      Mockito.verify(proxy).setData("hello", "theUri");
   }

   @Test
   public void testCreateFromStringsNullUri() throws OseeCoreException {
      long typeUuid = 45L;
      String value = "hello";
      String uri = null;

      Mockito.when(attributeTypeCache.getByGuid(45L)).thenReturn(attributeType);
      Mockito.when(attributeType.getAttributeProviderId()).thenReturn("org.eclipse.proxyfactory");
      Mockito.when(proxyProvider.getFactory("proxyfactory")).thenReturn(dataProxyFactory);
      Mockito.when(dataProxyFactory.createInstance("proxyfactory")).thenReturn(proxy);

      DataProxy theProxy = factory.createProxy(typeUuid, value, uri);

      Assert.assertEquals(proxy, theProxy);
      Mockito.verify(proxy).setData("hello", null);
   }

   @Test
   public void testCreateFromStringsNullValue() throws OseeCoreException {
      long typeUuid = 45L;
      String value = null;
      String uri = "theUri";

      Mockito.when(attributeTypeCache.getByGuid(45L)).thenReturn(attributeType);
      Mockito.when(attributeType.getAttributeProviderId()).thenReturn("org.eclipse.proxyfactory");
      Mockito.when(proxyProvider.getFactory("proxyfactory")).thenReturn(dataProxyFactory);
      Mockito.when(dataProxyFactory.createInstance("proxyfactory")).thenReturn(proxy);

      DataProxy theProxy = factory.createProxy(typeUuid, value, uri);

      Assert.assertEquals(proxy, theProxy);
      Mockito.verify(proxy).setData(null, "theUri");
   }

   @Test
   public void testCreateFromStringsBothNull() throws OseeCoreException {
      long typeUuid = 45L;
      String value = null;
      String uri = null;

      Mockito.when(attributeTypeCache.getByGuid(45L)).thenReturn(attributeType);
      Mockito.when(attributeType.getAttributeProviderId()).thenReturn("org.eclipse.proxyfactory");
      Mockito.when(proxyProvider.getFactory("proxyfactory")).thenReturn(dataProxyFactory);
      Mockito.when(dataProxyFactory.createInstance("proxyfactory")).thenReturn(proxy);

      DataProxy theProxy = factory.createProxy(typeUuid, value, uri);

      Assert.assertEquals(proxy, theProxy);
      Mockito.verify(proxy).setData(null, null);
   }

   @Test
   public void testCreateFromStringsCheckIntern() throws OseeCoreException {
      long typeUuid = 45L;
      String value = "hello";
      String uri = null;

      Mockito.when(attributeTypeCache.getByGuid(45L)).thenReturn(attributeType);
      Mockito.when(attributeType.getAttributeProviderId()).thenReturn("org.eclipse.proxyfactory");
      Mockito.when(proxyProvider.getFactory("proxyfactory")).thenReturn(dataProxyFactory);
      Mockito.when(dataProxyFactory.createInstance("proxyfactory")).thenReturn(proxy);

      AttributeDataProxyFactory spy = Mockito.spy(factory);

      spy.createProxy(typeUuid, value, uri);
      Mockito.verify(spy, Mockito.times(0)).intern(value);

      Mockito.reset(spy);

      Mockito.when(attributeType.isEnumerated()).thenReturn(true);
      spy.createProxy(typeUuid, value, uri);
      Mockito.verify(spy).intern(value);

      Mockito.reset(spy);

      Mockito.when(attributeType.isEnumerated()).thenReturn(false);
      spy.createProxy(typeUuid, value, uri);
      Mockito.verify(spy, Mockito.times(0)).intern(value);

      Mockito.reset(spy);

      Mockito.when(attributeType.getBaseAttributeTypeId()).thenReturn("hellobooleanx");
      spy.createProxy(typeUuid, value, uri);
      Mockito.verify(spy).intern(value);
   }
}

Back to the top