Skip to main content
summaryrefslogtreecommitdiffstats
blob: 05034da954953d415e8edf3bcec8f58acc39a1b6 (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
/*******************************************************************************
 * Copyright (c) 2004, 2007 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.framework.core.dsl.integration.internal;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import org.junit.Assert;
import org.eclipse.osee.framework.core.data.IAccessContextId;
import org.eclipse.osee.framework.core.data.TokenFactory;
import org.eclipse.osee.framework.core.dsl.integration.internal.AccessModelInterpreterImpl;
import org.eclipse.osee.framework.core.dsl.integration.mocks.CheckAccessDetailCollectorNotCalled;
import org.eclipse.osee.framework.core.dsl.integration.mocks.MockArtifactDataProvider;
import org.eclipse.osee.framework.core.dsl.integration.mocks.MockArtifactProxy;
import org.eclipse.osee.framework.core.dsl.integration.mocks.MockModel;
import org.eclipse.osee.framework.core.dsl.integration.mocks.MockRestrictionHandler;
import org.eclipse.osee.framework.core.dsl.oseeDsl.AccessContext;
import org.eclipse.osee.framework.core.dsl.oseeDsl.ObjectRestriction;
import org.eclipse.osee.framework.core.exception.OseeArgumentException;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.model.access.AccessDetailCollector;
import org.eclipse.osee.framework.core.model.mocks.MockAccessDetailCollector;
import org.eclipse.osee.framework.jdk.core.util.GUID;
import org.junit.Before;
import org.junit.Test;

/**
 * Test Case for {@link AccessModelInterpreterImpl}
 * 
 * @author Roberto E. Escobar
 */
public class AccessModelInterpreterImplTest {
   private IAccessContextId contextId1;
   private IAccessContextId contextId2;

   private AccessContext expectedContext1;
   private AccessContext expectedContext2;

   private AccessModelInterpreterImpl interpreterNoArtData;

   @Before
   public void setup() {
      interpreterNoArtData = new AccessModelInterpreterImpl(null, null);

      contextId1 = TokenFactory.createAccessContextId(GUID.create(), "Context 1");
      contextId2 = TokenFactory.createAccessContextId(GUID.create(), "Context 2");

      expectedContext1 = MockModel.createAccessContext(contextId1.getGuid(), "c1");
      expectedContext2 = MockModel.createAccessContext(contextId2.getGuid(), "c2");
   }

   @Test
   public void testGetContext() throws OseeCoreException {

      Collection<AccessContext> contexts = Arrays.asList(expectedContext1, expectedContext2);
      AccessContext actualContext1 = interpreterNoArtData.getContext(contexts, contextId1);
      Assert.assertEquals(expectedContext1, actualContext1);

      AccessContext actualContext2 = interpreterNoArtData.getContext(contexts, contextId2);
      Assert.assertEquals(expectedContext2, actualContext2);
   }

   @Test(expected = OseeArgumentException.class)
   public void testGetContextNullCheck1() throws OseeCoreException {
      interpreterNoArtData.getContext(null, contextId1);
   }

   @Test(expected = OseeArgumentException.class)
   public void testGetContextNullCheck2() throws OseeCoreException {
      interpreterNoArtData.getContext(Collections.<AccessContext> emptyList(), null);
   }

   @Test(expected = OseeArgumentException.class)
   public void testComputeAccessNullCheck1() throws OseeCoreException {
      interpreterNoArtData.computeAccessDetails(null, expectedContext1, new Object());
   }

   @Test(expected = OseeArgumentException.class)
   public void testComputeAccessNullCheck2() throws OseeCoreException {
      interpreterNoArtData.computeAccessDetails(new MockAccessDetailCollector(), null, new Object());
   }

   @Test(expected = OseeArgumentException.class)
   public void testComputeAccessNullCheck3() throws OseeCoreException {
      interpreterNoArtData.computeAccessDetails(new MockAccessDetailCollector(), expectedContext1, null);
   }

   @Test
   public void testComputeAccessNotApplicableObject() throws OseeCoreException {
      final Object objectToCheck = new Object();
      MockArtifactDataProvider provider = new MockArtifactDataProvider(false, objectToCheck, null);
      AccessModelInterpreterImpl interpreter = new AccessModelInterpreterImpl(provider, null);
      interpreter.computeAccessDetails(new CheckAccessDetailCollectorNotCalled(), expectedContext1, objectToCheck);
      Assert.assertTrue("Provider isApplicableCalled failed", provider.wasIsApplicableCalled());
      Assert.assertFalse("Provider asCastedObjectCalled failed", provider.wasAsCastedObjectCalled());
   }

   @Test(expected = OseeArgumentException.class)
   public void testComputeAccessCastedObjectNull() throws OseeCoreException {
      final Object objectToCheck = new Object();
      MockArtifactDataProvider provider = new MockArtifactDataProvider(true, objectToCheck, null);
      AccessModelInterpreterImpl interpreter = new AccessModelInterpreterImpl(provider, null);
      try {
         interpreter.computeAccessDetails(new CheckAccessDetailCollectorNotCalled(), expectedContext1, objectToCheck);
      } finally {
         Assert.assertTrue("Provider isApplicableCalled failed", provider.wasIsApplicableCalled());
         Assert.assertTrue("Provider asCastedObjectCalled failed", provider.wasAsCastedObjectCalled());
      }
   }

   @Test
   public void testComputeAccessCheckRestriction() throws OseeCoreException {
      AccessContext accessContext = MockModel.createAccessContext(contextId2.getGuid(), "c2");

      MockArtifactProxy artifactData = new MockArtifactProxy("1234", null);

      ObjectRestriction objectRestriction = null;
      assertComputeDetails(accessContext, artifactData, objectRestriction, false);
   }

   private static void assertComputeDetails(AccessContext accessContext, MockArtifactProxy artifactData, ObjectRestriction objectRestriction, boolean expectedProcessCalled) throws OseeCoreException {
      final Object objectToCheck = new Object();
      MockArtifactDataProvider provider = new MockArtifactDataProvider(true, objectToCheck, artifactData);
      AccessDetailCollector collector = new CheckAccessDetailCollectorNotCalled();
      MockRestrictionHandler restrictionHandler =
         new MockRestrictionHandler(objectRestriction, artifactData, collector);
      AccessModelInterpreterImpl interpreter = new AccessModelInterpreterImpl(provider, null, restrictionHandler);
      interpreter.computeAccessDetails(collector, accessContext, objectToCheck);
      Assert.assertTrue("Provider isApplicableCalled failed", provider.wasIsApplicableCalled());
      Assert.assertTrue("Provider asCastedObjectCalled failed", provider.wasAsCastedObjectCalled());
      Assert.assertEquals("Restriction process called check failed", expectedProcessCalled,
         restrictionHandler.wasProcessCalled());
   }
}

Back to the top