Skip to main content
summaryrefslogtreecommitdiffstats
blob: 28fda8ea464a54709a35048de6630dd32a87c9df (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
 *
 * This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License 2.0
 * which is available at https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * 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.eclipse.osee.framework.core.data.IAccessContextId;
import org.eclipse.osee.framework.core.dsl.integration.AccessDataCollector;
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.model.access.AccessDetailCollector;
import org.eclipse.osee.framework.jdk.core.type.OseeArgumentException;
import org.eclipse.osee.framework.jdk.core.util.Lib;
import org.junit.Assert;
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 = IAccessContextId.valueOf(Lib.generateArtifactIdAsInt(), "Context 1");
      contextId2 = IAccessContextId.valueOf(Lib.generateArtifactIdAsInt(), "Context 2");

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

   @Test
   public void testGetContext() {

      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() {
      interpreterNoArtData.getContext(null, contextId1);
   }

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

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

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

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

   @Test
   public void testComputeAccessNotApplicableObject() {
      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() {
      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() {
      AccessContext accessContext = MockModel.createAccessContext(contextId2.getId(), "c2");

      MockArtifactProxy artifactData = new MockArtifactProxy();

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

   private static void assertComputeDetails(AccessContext accessContext, MockArtifactProxy artifactData, ObjectRestriction objectRestriction, boolean expectedProcessCalled) {
      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