Skip to main content
summaryrefslogtreecommitdiffstats
blob: b89ff1dd0d3c5991b94114f037706e5d92d5d5f0 (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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*******************************************************************************
 * Copyright (c) 2013 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.core.internal.relation;

import static org.eclipse.osee.framework.core.enums.RelationSide.SIDE_A;
import static org.eclipse.osee.framework.core.enums.RelationSide.SIDE_B;
import static org.eclipse.osee.framework.core.enums.RelationTypeMultiplicity.MANY_TO_MANY;
import static org.eclipse.osee.framework.core.enums.RelationTypeMultiplicity.MANY_TO_ONE;
import static org.eclipse.osee.framework.core.enums.RelationTypeMultiplicity.ONE_TO_MANY;
import static org.eclipse.osee.framework.core.enums.RelationTypeMultiplicity.ONE_TO_ONE;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import org.eclipse.osee.framework.core.data.IArtifactType;
import org.eclipse.osee.framework.core.data.IRelationType;
import org.eclipse.osee.framework.core.data.TokenFactory;
import org.eclipse.osee.framework.core.exception.OseeArgumentException;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.exception.OseeStateException;
import org.eclipse.osee.orcs.core.internal.util.MultiplicityState;
import org.eclipse.osee.orcs.data.RelationTypes;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.mockito.Mock;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;

/**
 * Test Case for {@link RelationTypeValidity}
 * 
 * @author Roberto E. Escobar
 * @author Megumi Telles
 */
public class RelationTypeValidityTest {

   private static final IRelationType TYPE_1 = TokenFactory.createRelationType(123456789L, "TYPE_1");

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

   // @formatter:off
   @Mock private RelationTypes relTypes;
   @Mock private RelationNode node;
   @Mock private IArtifactType artifactType;   
   @Mock private IArtifactType artifactType2;
   
   @Mock private IRelationType relationType1;
   @Mock private IRelationType relationType2;
   @Mock private IRelationType relationType3;
   @Mock private IRelationType relationType4;
   // @formatter:on

   private RelationTypeValidity validity;

   @Before
   public void init() throws OseeCoreException {
      initMocks(this);

      validity = new RelationTypeValidity(relTypes);

      when(relTypes.exists(TYPE_1)).thenReturn(true);
      when(relTypes.exists(relationType1)).thenReturn(true);
      when(relTypes.exists(relationType2)).thenReturn(true);
      when(relTypes.exists(relationType3)).thenReturn(true);
      when(relTypes.exists(relationType4)).thenReturn(true);
   }

   @Test
   public void testMaximumRelationAllowedNullArtifactType() throws OseeCoreException {
      thrown.expect(OseeArgumentException.class);
      thrown.expectMessage("artifactType cannot be null");
      validity.getMaximumRelationsAllowed(TYPE_1, null, SIDE_A);
   }

   @Test
   public void testMaximumRelationAllowedNullRelationType() throws OseeCoreException {
      thrown.expect(OseeArgumentException.class);
      thrown.expectMessage("relationType cannot be null");
      validity.getMaximumRelationsAllowed(null, artifactType, SIDE_B);
   }

   @Test
   public void testMaximumRelationAllowedNullRelationSide() throws OseeCoreException {
      thrown.expect(OseeArgumentException.class);
      thrown.expectMessage("relationSide cannot be null");
      validity.getMaximumRelationsAllowed(TYPE_1, artifactType, null);
   }

   @Test
   public void testMaximumRelationAllowedTypeDoesNotExist() throws OseeCoreException {
      when(relTypes.exists(TYPE_1)).thenReturn(false);

      thrown.expect(OseeArgumentException.class);
      thrown.expectMessage(String.format("relationType [%s] does not exist", TYPE_1));
      validity.getMaximumRelationsAllowed(TYPE_1, artifactType, SIDE_A);
   }

   @Test
   public void testValidRelationTypesNullArtifactType() throws OseeCoreException {
      thrown.expect(OseeArgumentException.class);
      thrown.expectMessage("artifactType cannot be null");
      validity.getValidRelationTypes(null);
   }

   @Test
   public void testMaximumRelationAllowed1() throws OseeCoreException {
      when(relTypes.isArtifactTypeAllowed(TYPE_1, SIDE_B, artifactType)).thenReturn(true);
      when(relTypes.getMultiplicity(TYPE_1)).thenReturn(MANY_TO_MANY);

      int actual = validity.getMaximumRelationsAllowed(TYPE_1, artifactType, SIDE_B);
      assertEquals(Integer.MAX_VALUE, actual);
   }

   @Test
   public void testMaximumRelationAllowed2() throws OseeCoreException {
      when(relTypes.isArtifactTypeAllowed(TYPE_1, SIDE_B, artifactType)).thenReturn(true);
      when(relTypes.getMultiplicity(TYPE_1)).thenReturn(MANY_TO_ONE);

      int actual = validity.getMaximumRelationsAllowed(TYPE_1, artifactType, SIDE_B);
      assertEquals(1, actual);
   }

   @Test
   public void testMaximumRelationAllowed3() throws OseeCoreException {
      when(relTypes.isArtifactTypeAllowed(relationType1, SIDE_A, artifactType)).thenReturn(true);

      int actual = validity.getMaximumRelationsAllowed(TYPE_1, artifactType, SIDE_B);
      assertEquals(0, actual);
   }

   @Test
   public void testValidRelationTypes() throws OseeCoreException {
      final Collection<? extends IRelationType> types =
         Arrays.asList(relationType1, relationType2, relationType3, relationType4);
      when(relTypes.getAll()).thenAnswer(new Answer<Collection<? extends IRelationType>>() {

         @Override
         public Collection<? extends IRelationType> answer(InvocationOnMock invocation) throws Throwable {
            return types;
         }

      });
      when(relTypes.isArtifactTypeAllowed(relationType1, SIDE_B, artifactType)).thenReturn(true);
      when(relTypes.getMultiplicity(relationType1)).thenReturn(MANY_TO_MANY);

      when(relTypes.isArtifactTypeAllowed(relationType2, SIDE_A, artifactType)).thenReturn(false);
      when(relTypes.getMultiplicity(relationType2)).thenReturn(MANY_TO_ONE);

      when(relTypes.isArtifactTypeAllowed(relationType3, SIDE_A, artifactType)).thenReturn(true);
      when(relTypes.getMultiplicity(relationType3)).thenReturn(ONE_TO_ONE);

      when(relTypes.isArtifactTypeAllowed(relationType4, SIDE_A, artifactType)).thenReturn(false);
      when(relTypes.getMultiplicity(relationType4)).thenReturn(ONE_TO_MANY);

      List<IRelationType> actual = validity.getValidRelationTypes(artifactType);

      assertEquals(2, actual.size());
      assertTrue(actual.contains(relationType1));
      assertFalse(actual.contains(relationType2));
      assertTrue(actual.contains(relationType3));
      assertFalse(actual.contains(relationType4));
   }

   @Test
   public void testGetRelationMultiplicityState() throws OseeCoreException {
      when(relTypes.getMultiplicity(TYPE_1)).thenReturn(ONE_TO_ONE);

      MultiplicityState state = validity.getRelationMultiplicityState(TYPE_1, SIDE_B, 0);
      assertEquals(MultiplicityState.IS_VALID, state);

      state = validity.getRelationMultiplicityState(TYPE_1, SIDE_B, 1);
      assertEquals(MultiplicityState.IS_VALID, state);

      state = validity.getRelationMultiplicityState(TYPE_1, SIDE_B, 2);
      assertEquals(MultiplicityState.MAX_VIOLATION, state);
   }

   @Test
   public void testCheckRelationTypeMultiplicity() throws OseeCoreException {
      when(relTypes.getMultiplicity(TYPE_1)).thenReturn(ONE_TO_ONE);
      when(node.getExceptionString()).thenReturn("node message");

      thrown.expect(OseeStateException.class);
      thrown.expectMessage(String.format("Relation type [%s] on [%s] exceeds max occurrence rule on [node message]",
         TYPE_1, SIDE_A, node.getExceptionString()));

      validity.checkRelationTypeMultiplicity(TYPE_1, node, SIDE_A, 2);
   }

   @Test
   public void testCheckRelationTypeMultiplicityNoException() throws OseeCoreException {
      when(relTypes.getMultiplicity(TYPE_1)).thenReturn(ONE_TO_ONE);
      when(node.getExceptionString()).thenReturn("node message");

      ExpectedException.none();
      validity.checkRelationTypeMultiplicity(TYPE_1, node, SIDE_A, 0);
   }

   @Test
   public void testIsRelationTypeValid() throws OseeCoreException {
      when(relTypes.isArtifactTypeAllowed(TYPE_1, SIDE_A, artifactType)).thenReturn(true);

      boolean actual = validity.isRelationTypeValid(TYPE_1, artifactType, SIDE_B);
      assertEquals(false, actual);

      when(relTypes.isArtifactTypeAllowed(TYPE_1, SIDE_B, artifactType)).thenReturn(true);
      when(relTypes.getMultiplicity(TYPE_1)).thenReturn(ONE_TO_ONE);

      actual = validity.isRelationTypeValid(TYPE_1, artifactType, SIDE_B);
      assertEquals(true, actual);
   }

   @Test
   public void testCheckRelationTypeValid() throws OseeCoreException {
      when(artifactType.toString()).thenReturn("artType1");
      when(artifactType2.toString()).thenReturn("artType2");

      when(relTypes.isArtifactTypeAllowed(TYPE_1, SIDE_A, artifactType)).thenReturn(true);
      when(relTypes.getArtifactType(TYPE_1, SIDE_B)).thenReturn(artifactType2);

      when(node.getArtifactType()).thenReturn(artifactType);
      when(node.getExceptionString()).thenReturn("node message");

      thrown.expect(OseeArgumentException.class);
      thrown.expectMessage("Relation validity error for [node message] - ArtifactType [artType1] does not belong on side [SIDE_B] of relation [TYPE_1] - only items of type [artType2] are allowed");
      validity.checkRelationTypeValid(TYPE_1, node, SIDE_B);
   }

   @Test
   public void testCheckRelationTypeValidNoException() throws OseeCoreException {
      when(node.getArtifactType()).thenReturn(artifactType);
      when(relTypes.isArtifactTypeAllowed(TYPE_1, SIDE_B, artifactType)).thenReturn(true);
      when(relTypes.getMultiplicity(TYPE_1)).thenReturn(ONE_TO_ONE);

      ExpectedException.none();
      validity.checkRelationTypeValid(TYPE_1, node, SIDE_B);
   }

}

Back to the top