Skip to main content
summaryrefslogtreecommitdiffstats
blob: 0bfcd83fcd2af12a15bfca739a072c939f17fa4f (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
/*******************************************************************************
 * 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.impl;

import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.anyCollectionOf;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import org.eclipse.osee.framework.core.data.ResultSet;
import org.eclipse.osee.framework.core.enums.LoadLevel;
import org.eclipse.osee.framework.core.enums.RelationSide;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.framework.jdk.core.util.GUID;
import org.eclipse.osee.orcs.OrcsSession;
import org.eclipse.osee.orcs.core.internal.graph.GraphData;
import org.eclipse.osee.orcs.core.internal.relation.Relation;
import org.eclipse.osee.orcs.core.internal.relation.RelationNode;
import org.eclipse.osee.orcs.core.internal.relation.RelationNodeLoader;
import org.eclipse.osee.orcs.core.internal.relation.RelationResolver;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

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

   // @formatter:off
   @Mock private RelationNodeLoader loader;
   @Mock private OrcsSession session;
   @Mock private GraphData graphData;
   
   @Mock private Relation relation1;
   @Mock private Relation relation2;
   @Mock private Relation relation3;
   @Mock private Relation relation4;
   
   @Mock private RelationNode node1;
   @Mock private RelationNode node2;
   @Mock private RelationNode node3;
   @Mock private RelationNode node4;
   @Mock private RelationNode node5;
   @Mock private RelationNode node6;
   
   @Mock private ResultSet<RelationNode> resultSet;
   @Captor private ArgumentCaptor<Collection<Integer>> captor;
   // @formatter:on

   private RelationResolver resolver;
   private List<Relation> links;

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

      resolver = new RelationResolverImpl(loader);

      links = Arrays.asList(relation1, relation2, relation3, relation4);

      String sessionId = GUID.create();
      when(session.getGuid()).thenReturn(sessionId);

      when(node1.getLocalId()).thenReturn(11);
      when(node2.getLocalId()).thenReturn(22);
      when(node3.getLocalId()).thenReturn(33);
      when(node4.getLocalId()).thenReturn(44);
      when(node5.getLocalId()).thenReturn(55);
      when(node6.getLocalId()).thenReturn(66);

      when(relation1.getLocalIdForSide(RelationSide.SIDE_A)).thenReturn(11);
      when(relation1.getLocalIdForSide(RelationSide.SIDE_B)).thenReturn(22);

      when(relation2.getLocalIdForSide(RelationSide.SIDE_A)).thenReturn(33);
      when(relation2.getLocalIdForSide(RelationSide.SIDE_B)).thenReturn(44);

      when(relation3.getLocalIdForSide(RelationSide.SIDE_A)).thenReturn(55);
      when(relation3.getLocalIdForSide(RelationSide.SIDE_B)).thenReturn(66);

      when(relation4.getLocalIdForSide(RelationSide.SIDE_A)).thenReturn(11);
      when(relation4.getLocalIdForSide(RelationSide.SIDE_B)).thenReturn(66);

      when(loader.loadNodes(eq(session), eq(graphData), anyCollectionOf(Integer.class), eq(LoadLevel.ALL))).thenReturn(
         resultSet);
   }

   @Test
   public void testLoadAll() throws OseeCoreException {
      List<RelationNode> loaded = Arrays.asList(node1, node2, node3, node4, node5, node6);

      when(resultSet.iterator()).thenReturn(loaded.iterator());

      List<RelationNode> arts = resolver.resolve(session, graphData, links, RelationSide.SIDE_A, RelationSide.SIDE_B);

      verify(loader).loadNodes(eq(session), eq(graphData), captor.capture(), eq(LoadLevel.ALL));
      assertCollection(captor.getValue(), 11, 22, 33, 44, 55, 66);
      assertCollection(arts, node1, node2, node3, node4, node5, node6);
   }

   @Test
   public void testLoadSideAOnly() throws OseeCoreException {
      List<RelationNode> loaded = Arrays.asList(node1, node3, node5);

      when(resultSet.iterator()).thenReturn(loaded.iterator());

      List<RelationNode> arts = resolver.resolve(session, graphData, links, RelationSide.SIDE_A);

      verify(loader).loadNodes(eq(session), eq(graphData), captor.capture(), eq(LoadLevel.ALL));

      assertCollection(captor.getValue(), 11, 33, 55);
      assertCollection(arts, node1, node3, node5);
   }

   @Test
   public void testLoadSideBOnly() throws OseeCoreException {
      List<RelationNode> loaded = Arrays.asList(node2, node4, node6);

      when(resultSet.iterator()).thenReturn(loaded.iterator());

      List<RelationNode> arts = resolver.resolve(session, graphData, links, RelationSide.SIDE_B);

      verify(loader).loadNodes(eq(session), eq(graphData), captor.capture(), eq(LoadLevel.ALL));

      assertCollection(captor.getValue(), 22, 44, 66);
      assertCollection(arts, node2, node4, node6);
   }

   @Test
   public void testLoadSideAFromCacheAndSideBFromLoader() throws OseeCoreException {
      List<RelationNode> loaded = Arrays.asList(node2, node4, node6);

      when(graphData.getNode(11)).thenReturn(node1);
      when(graphData.getNode(33)).thenReturn(node3);
      when(graphData.getNode(55)).thenReturn(node5);

      when(resultSet.iterator()).thenReturn(loaded.iterator());

      List<RelationNode> arts = resolver.resolve(session, graphData, links, RelationSide.SIDE_A, RelationSide.SIDE_B);

      verify(graphData, times(2)).getNode(11);
      verify(graphData).getNode(33);
      verify(graphData).getNode(55);

      verify(loader).loadNodes(eq(session), eq(graphData), captor.capture(), eq(LoadLevel.ALL));

      assertCollection(captor.getValue(), 22, 44, 66);
      assertCollection(arts, node1, node2, node3, node4, node5, node6);
   }

   private static <T> void assertCollection(Collection<T> actual, T... expecteds) {
      assertEquals(expecteds.length, actual.size());
      int index = 0;
      for (Iterator<T> iterator = actual.iterator(); iterator.hasNext();) {
         T value = iterator.next();
         assertEquals(expecteds[index++], value);
      }
   }
}

Back to the top