Skip to main content
summaryrefslogtreecommitdiffstats
blob: 7f36046a33e4d4692c87491fa44d3085bf6bb3b7 (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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*******************************************************************************
 * Copyright (c) 2010 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.client.integration.tests.integration.ui.skynet;

import static org.eclipse.osee.client.demo.DemoChoice.OSEE_CLIENT_DEMO;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import org.eclipse.osee.client.test.framework.OseeClientIntegrationRule;
import org.eclipse.osee.client.test.framework.OseeLogMonitorRule;
import org.eclipse.osee.framework.core.data.IAttributeType;
import org.eclipse.osee.framework.core.enums.CoreArtifactTypes;
import org.eclipse.osee.framework.core.enums.CoreAttributeTypes;
import org.eclipse.osee.framework.core.enums.CoreBranches;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.framework.jdk.core.type.Pair;
import org.eclipse.osee.framework.jdk.core.util.Compare;
import org.eclipse.osee.framework.skynet.core.artifact.Artifact;
import org.eclipse.osee.framework.skynet.core.artifact.ArtifactTypeManager;
import org.eclipse.osee.framework.ui.skynet.artifact.editor.sections.AttributeTypeEditPresenter;
import org.eclipse.osee.framework.ui.skynet.artifact.editor.sections.AttributeTypeEditPresenter.Display.OperationType;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

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

   @Rule
   public OseeClientIntegrationRule integration = new OseeClientIntegrationRule(OSEE_CLIENT_DEMO);

   @Rule
   public OseeLogMonitorRule monitorRule = new OseeLogMonitorRule();

   private static final IAttributeType[] selectableTypes = new IAttributeType[] {
      CoreAttributeTypes.RelationOrder,
      CoreAttributeTypes.ContentUrl,
      CoreAttributeTypes.Annotation,
      CoreAttributeTypes.StaticId};

   private AttributeTypeEditPresenter controller;
   private MockDisplay display;
   private Artifact artifact;
   private MockEditor editor;

   @Before
   public void setUp() throws OseeCoreException {
      display = new MockDisplay();
      editor = new MockEditor();
      controller = new AttributeTypeEditPresenter(editor, display);

      artifact =
         ArtifactTypeManager.addArtifact(CoreArtifactTypes.Artifact, CoreBranches.COMMON, "test attribute types");
      editor.setArtifact(artifact);
   }

   @After
   public void tearDown() throws OseeCoreException {
      if (artifact != null) {
         artifact.purgeFromBranch();
      }
   }

   @Test
   public void testAddRemoveItems() throws OseeCoreException {
      String expectedTitle = "Add Attribute Types";
      String expectedOpMessage = "Select items to add.";
      String expectedNoneMessage = "No attribute types available to add.";
      OperationType expectedType = OperationType.ADD_ITEM;

      testOperation(expectedType, expectedTitle, expectedOpMessage, expectedNoneMessage);

      expectedTitle = "Delete Attribute Types";
      expectedOpMessage = "Select items to remove.";
      expectedNoneMessage = "No attribute types available to remove.";
      expectedType = OperationType.REMOVE_ITEM;

      testOperation(expectedType, expectedTitle, expectedOpMessage, expectedNoneMessage);
   }

   private static void performOp(AttributeTypeEditPresenter controller, OperationType operationType) throws OseeCoreException {
      switch (operationType) {
         case ADD_ITEM:
            controller.onAddAttributeType();
            break;
         case REMOVE_ITEM:
            controller.onRemoveAttributeType();
            break;
         default:
            throw new UnsupportedOperationException();
      }
   }

   private void testOperation(OperationType operationType, String expectedTitle, String expectedOpMessage, String expectedNoneMessage) throws OseeCoreException {
      editor.setWasDirtyStateCalled(false);
      display.setAddWidgetsAttributeTypes(null);
      display.setRemoveWidgetsAttributeTypes(null);

      // None Selected
      display.setSelected();
      List<IAttributeType> selectable = new ArrayList<>(Arrays.asList(selectableTypes));
      performOp(controller, operationType);

      Assert.assertNull(display.getAddWidgetsAttributeTypes());
      Assert.assertNull(display.getRemoveWidgetsAttributeTypes());

      // Editor not saved unless artifact change
      Assert.assertFalse(editor.wasDirtyStateCalled());
      checkDisplay(display, operationType, expectedTitle, expectedOpMessage, selectable);

      // Add one at a time
      for (IAttributeType itemToSelect : selectableTypes) {
         editor.setWasDirtyStateCalled(false);
         Assert.assertFalse(editor.wasDirtyStateCalled());

         display.setAddWidgetsAttributeTypes(null);
         display.setRemoveWidgetsAttributeTypes(null);

         display.setSelected(itemToSelect);

         if (OperationType.ADD_ITEM == operationType) {
            Assert.assertTrue(artifact.getAttributeCount(itemToSelect) == 0);
         } else if (OperationType.REMOVE_ITEM == operationType) {
            Assert.assertFalse(artifact.getAttributeCount(itemToSelect) == 0);
         }

         Assert.assertNull(display.getAddWidgetsAttributeTypes());
         Assert.assertNull(display.getRemoveWidgetsAttributeTypes());
         performOp(controller, operationType);

         // Check Dirty State
         Assert.assertTrue(editor.wasDirtyStateCalled());

         checkDisplay(display, operationType, expectedTitle, expectedOpMessage, selectable);

         if (OperationType.ADD_ITEM == operationType) {
            Assert.assertFalse(artifact.getAttributeCount(itemToSelect) == 0);
            Assert.assertNull(display.getRemoveWidgetsAttributeTypes());

            Collection<IAttributeType> addedTypes = display.getAddWidgetsAttributeTypes();
            Assert.assertEquals(1, addedTypes.size());
            Assert.assertEquals(itemToSelect, addedTypes.iterator().next());

         } else if (OperationType.REMOVE_ITEM == operationType) {
            Assert.assertTrue(artifact.getAttributeCount(itemToSelect) == 0);
            Assert.assertNull(display.getAddWidgetsAttributeTypes());

            Collection<IAttributeType> removedTypes = display.getRemoveWidgetsAttributeTypes();
            Assert.assertEquals(1, removedTypes.size());
            Assert.assertEquals(itemToSelect, removedTypes.iterator().next());
         }
         selectable.remove(itemToSelect);
      }

      editor.setWasDirtyStateCalled(false);
      Assert.assertFalse(editor.wasDirtyStateCalled());

      display.setAddWidgetsAttributeTypes(null);
      display.setRemoveWidgetsAttributeTypes(null);

      //      artifact.deleteAttributes(CoreAttributeTypes.Name);

      // None Selectable
      display.setSelected();
      performOp(controller, operationType);
      Assert.assertNull(display.getAddWidgetsAttributeTypes());
      Assert.assertNull(display.getRemoveWidgetsAttributeTypes());

      Assert.assertFalse(editor.wasDirtyStateCalled());

      Pair<String, String> info = display.getShowInfo();
      Assert.assertEquals(expectedTitle, info.getFirst());
      Assert.assertEquals(expectedNoneMessage, info.getSecond());
   }

   private static void checkDisplay(MockDisplay display, OperationType expectedType, String title, String message, List<IAttributeType> expectedSelectable) {
      List<IAttributeType> selectableItems = display.getInput();

      Assert.assertFalse(
         String.format("Selectable Types - expected:[%s] actual:[%s]", expectedSelectable, selectableItems),
         Compare.isDifferent(expectedSelectable, selectableItems));
      Assert.assertEquals(expectedType, display.getOperationType());

      Pair<String, String> selectionInfo = display.getSelectionInfo();
      Assert.assertEquals(title, selectionInfo.getFirst());
      Assert.assertEquals(message, selectionInfo.getSecond());
      Assert.assertNull(display.getShowInfo());

      //      display.getSelections(operationType, title, message, input)
   }

   private final static class MockEditor implements AttributeTypeEditPresenter.Model {
      private boolean wasDirtyStateCalled;
      private Artifact artifact;

      public void setWasDirtyStateCalled(boolean wasDirtyStateCalled) {
         this.wasDirtyStateCalled = wasDirtyStateCalled;
      }

      public boolean wasDirtyStateCalled() {
         return wasDirtyStateCalled;
      }

      @Override
      public Artifact getArtifact() {
         return artifact;
      }

      public void setArtifact(Artifact artifact) {
         this.artifact = artifact;
      }

      @Override
      public void refreshDirtyArtifact() {
         // do nothing
      }

      @Override
      public void dirtyStateChanged() {
         this.wasDirtyStateCalled = true;
      }

   }

   private final static class MockDisplay implements AttributeTypeEditPresenter.Display {

      private Pair<String, String> showInfo;
      private Pair<String, String> selectionInfo;
      private OperationType operationType;
      private List<IAttributeType> input;
      private Collection<IAttributeType> selected;
      private Collection<IAttributeType> addWidgetsAttributeTypes;
      private Collection<IAttributeType> removeWidgetsAttributeTypes;

      private MockDisplay() {
         this.selected = Collections.emptyList();
      }

      @Override
      public Collection<IAttributeType> getSelections(OperationType operationType, String title, String message, List<IAttributeType> input) {
         setSelectionInfo(new Pair<String, String>(title, message));
         setInput(input);
         setOperationType(operationType);
         setShowInfo(null);
         return selected;
      }

      @Override
      public void showInformation(String title, String message) {
         showInfo = new Pair<>(title, message);
      }

      public Pair<String, String> getShowInfo() {
         return showInfo;
      }

      public Pair<String, String> getSelectionInfo() {
         return selectionInfo;
      }

      public OperationType getOperationType() {
         return operationType;
      }

      public List<IAttributeType> getInput() {
         return input;
      }

      public void setSelected(IAttributeType... selected) {
         this.selected = Arrays.asList(selected);
      }

      public void setShowInfo(Pair<String, String> showInfo) {
         this.showInfo = showInfo;
      }

      public void setSelectionInfo(Pair<String, String> selectionInfo) {
         this.selectionInfo = selectionInfo;
      }

      public void setOperationType(OperationType operationType) {
         this.operationType = operationType;
      }

      public void setInput(List<IAttributeType> input) {
         this.input = input;
      }

      public Collection<IAttributeType> getAddWidgetsAttributeTypes() {
         return addWidgetsAttributeTypes;
      }

      public Collection<IAttributeType> getRemoveWidgetsAttributeTypes() {
         return removeWidgetsAttributeTypes;
      }

      public void setAddWidgetsAttributeTypes(Collection<IAttributeType> addWidgetsAttributeTypes) {
         this.addWidgetsAttributeTypes = addWidgetsAttributeTypes;
      }

      public void setRemoveWidgetsAttributeTypes(Collection<IAttributeType> removeWidgetsAttributeTypes) {
         this.removeWidgetsAttributeTypes = removeWidgetsAttributeTypes;
      }

      @Override
      public void addWidgetFor(Collection<IAttributeType> attributeTypes) {
         addWidgetsAttributeTypes = attributeTypes;
      }

      @Override
      public void removeWidgetFor(Collection<IAttributeType> attributeTypes) {
         removeWidgetsAttributeTypes = attributeTypes;
      }
   }
}

Back to the top