Skip to main content
summaryrefslogtreecommitdiffstats
blob: d518d4e6c6bb4b4636f6521d2196ffc7d7c41b45 (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
/*******************************************************************************
 * 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.framework.core.model.access;

import java.util.ArrayList;
import java.util.Collection;
import org.eclipse.osee.framework.core.enums.PermissionEnum;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.model.access.AccessDetail;
import org.eclipse.osee.framework.core.model.mocks.MockDataFactory;
import org.eclipse.osee.framework.core.model.access.Scope;
import org.eclipse.osee.framework.jdk.core.util.Strings;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

/**
 * Test Case for {@link AccessDetail}
 * 
 * @author Roberto E. Escobar
 */
@RunWith(Parameterized.class)
public class AccessDetailTest {

   private final Object expAccessObject;
   private final PermissionEnum expPermission;
   private final String expReason;
   private final AccessDetail<?> target;
   private final Scope scope;

   public AccessDetailTest(AccessDetail<?> target, Object expAccessObject, PermissionEnum expPermission, String expReason, Scope scope) {
      this.target = target;
      this.expAccessObject = expAccessObject;
      this.expPermission = expPermission;
      this.expReason = expReason;
      this.scope = scope;
   }

   @Test
   public void testGetReason() {
      Assert.assertEquals(Strings.isValid(expReason) ? expReason : scope.getPath(), target.getReason());
   }

   @Test
   public void testGetPermission() {
      Assert.assertEquals(expPermission, target.getPermission());
   }

   @Test
   public void testGetAccessObject() {
      Assert.assertEquals(expAccessObject, target.getAccessObject());
   }

   @Test
   public void testSetPermission() {
      Assert.assertEquals(expPermission, target.getPermission());
      PermissionEnum anotherPermission = PermissionEnum.NONE;

      target.setPermission(anotherPermission);
      Assert.assertEquals(anotherPermission, target.getPermission());

      target.setPermission(expPermission);
      Assert.assertEquals(expPermission, target.getPermission());
   }

   @Test
   public void testHashCodeAndEquals() {
      Assert.assertTrue(target.equals(target));
      Assert.assertTrue(target.hashCode() == target.hashCode());

      AccessDetail<?> other =
         MockDataFactory.createAccessDetails(expAccessObject, PermissionEnum.NONE, null, new Scope().add("other"));
      Assert.assertTrue(target.equals(other));
      Assert.assertTrue(target.hashCode() == other.hashCode());

      AccessDetail<?> nulled =
         MockDataFactory.createAccessDetails(null, PermissionEnum.NONE, null, new Scope().add("nulled"));
      Assert.assertFalse(target.equals(nulled));
      Assert.assertTrue(target.hashCode() != nulled.hashCode());

      Collection<AccessDetail<?>> collect = new ArrayList<AccessDetail<?>>();
      collect.add(target);
      Assert.assertEquals(1, collect.size());
      Assert.assertTrue(collect.contains(target));
      Assert.assertTrue(collect.contains(other));
      Assert.assertFalse(collect.contains(nulled));
   }

   @Test
   public void testToString() {
      String expected =
         "AccessDetail [permission=" + expPermission + ", scope=" + scope + ", accessObject=" + expAccessObject + ", reason=" + (Strings.isValid(expReason) ? expReason : scope) + "]";
      Assert.assertEquals(expected, target.toString());
   }

   @Parameters
   public static Collection<Object[]> getData() throws OseeCoreException {
      Collection<Object[]> data = new ArrayList<Object[]>();
      addTest(data, "Hello", PermissionEnum.DENY, "A reason", new Scope().add("hello_scope"));
      addTest(data, 456, PermissionEnum.WRITE, null, new Scope().add("456_scope"));
      addTest(data, MockDataFactory.createArtifactType(4), PermissionEnum.FULLACCESS, "reason3", new Scope());
      addTest(data, MockDataFactory.createAttributeType(), PermissionEnum.READ, "xx", new Scope().add("xx"));
      return data;
   }

   private static <T> void addTest(Collection<Object[]> data, T expAccessObject, PermissionEnum expPermission, String expReason, Scope scope) {
      String reasonToCheck = expReason;
      if (expReason == null) {
         reasonToCheck = Strings.emptyString();
      }
      AccessDetail<T> target = MockDataFactory.createAccessDetails(expAccessObject, expPermission, expReason, scope);
      data.add(new Object[] {target, expAccessObject, expPermission, reasonToCheck, scope});
   }

}

Back to the top