View Javadoc
1   /*
2    * *************************************************************************************************************************************************************
3    *
4    * SteelBlue: DCI User Interfaces
5    * http://tidalwave.it/projects/steelblue
6    *
7    * Copyright (C) 2015 - 2025 by Tidalwave s.a.s. (http://tidalwave.it)
8    *
9    * *************************************************************************************************************************************************************
10   *
11   * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
12   * You may obtain a copy of the License at
13   *
14   *     http://www.apache.org/licenses/LICENSE-2.0
15   *
16   * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
17   * CONDITIONS OF ANY KIND, either express or implied.  See the License for the specific language governing permissions and limitations under the License.
18   *
19   * *************************************************************************************************************************************************************
20   *
21   * git clone https://bitbucket.org/tidalwave/steelblue-src
22   * git clone https://github.com/tidalwave-it/steelblue-src
23   *
24   * *************************************************************************************************************************************************************
25   */
26  package it.tidalwave.ui.core.role.impl;
27  
28  import it.tidalwave.util.As;
29  import it.tidalwave.util.AsException;
30  import it.tidalwave.util.mock.MockAsFactory;
31  import it.tidalwave.role.spi.SystemRoleFactory;
32  import org.testng.annotations.BeforeMethod;
33  import org.testng.annotations.Test;
34  import static it.tidalwave.util.Parameters.r;
35  import static org.mockito.Mockito.*;
36  import static org.hamcrest.CoreMatchers.*;
37  import static org.hamcrest.MatcherAssert.assertThat;
38  
39  /***************************************************************************************************************************************************************
40   *
41   * @author  Fabrizio Giudici
42   *
43   **************************************************************************************************************************************************************/
44  public class DefaultPresentationModelTest
45    {
46      public static interface Role1
47        {
48        }
49  
50      public static interface Role2
51        {
52        }
53  
54      public static interface Role3
55        {
56        }
57  
58      private Role1 localRole1;
59      private Role2 localRole2;
60      private Role2 role2InOwner;
61      private Object ownerNoAs;
62      private As ownerAsWithNoRoles;
63      private As ownerAsWithRole2;
64  
65      /***********************************************************************************************************************************************************
66       * 
67       **********************************************************************************************************************************************************/
68      @BeforeMethod
69      public void setup()
70        {
71          // Not called by tests, we only need it's there
72          SystemRoleFactory.reset();
73  
74          localRole1 = mock(Role1.class);
75          localRole2 = mock(Role2.class);
76          role2InOwner = mock(Role2.class);
77          ownerNoAs = new Object();
78  
79          ownerAsWithNoRoles = MockAsFactory.mockWithAs(As.class);
80          ownerAsWithRole2 = MockAsFactory.mockWithAs(As.class, r(role2InOwner));
81        }
82  
83      /***********************************************************************************************************************************************************
84       * 
85       **********************************************************************************************************************************************************/
86      @Test
87      public void must_find_local_roles()
88        {
89          // given
90          final var underTest1 = new DefaultPresentationModel(ownerNoAs, r(localRole1));
91          final var underTest2 = new DefaultPresentationModel(ownerNoAs, r(localRole1, localRole2));
92          // when
93          final var ut1Role1 = underTest1.as(Role1.class);
94          final var ut2Role1 = underTest2.as(Role1.class);
95          final var ut2Role2 = underTest2.as(Role2.class);
96          //then
97          assertThat(ut1Role1, is(sameInstance(localRole1)));
98          assertThat(ut2Role1, is(sameInstance(localRole1)));
99          assertThat(ut2Role2, is(sameInstance(localRole2)));
100       }
101 
102     /***********************************************************************************************************************************************************
103      * 
104      **********************************************************************************************************************************************************/
105     @Test(expectedExceptions = AsException.class)
106     public void must_not_find_inexistent_role()
107       {
108         // given
109         final var underTest = new DefaultPresentationModel(ownerNoAs, r(localRole1));
110         // when
111         underTest.as(Role2.class);
112       }
113 
114     /***********************************************************************************************************************************************************
115      * 
116      **********************************************************************************************************************************************************/
117     @Test(expectedExceptions = AsException.class)
118     public void must_not_find_inexistent_role_bis()
119       {
120         // given
121         final var underTest = new DefaultPresentationModel(ownerAsWithRole2, r(localRole2));
122         // when
123         underTest.as(Role1.class);
124       }
125 
126     /***********************************************************************************************************************************************************
127      * 
128      **********************************************************************************************************************************************************/
129     @Test
130     public void must_find_roles_in_owner()
131       {
132         // given
133         final var underTest = new DefaultPresentationModel(ownerAsWithRole2, r());
134         // when
135         final var role2 = underTest.as(Role2.class);
136         // then
137         assertThat(role2, is(sameInstance(role2InOwner)));
138       }
139 
140     /***********************************************************************************************************************************************************
141      * 
142      **********************************************************************************************************************************************************/
143     @Test
144     public void must_give_priority_to_local_roles()
145       {
146         // given
147         final var underTest = new DefaultPresentationModel(ownerAsWithRole2, r(localRole2));
148         // when
149         final var role2 = underTest.as(Role2.class);
150         // then
151         assertThat(role2, is(sameInstance(localRole2)));
152       }
153 
154     /***********************************************************************************************************************************************************
155      * 
156      **********************************************************************************************************************************************************/
157     @Test
158     public void test_TFT_248_regression()
159       {
160         // given
161         final var underTest = new DefaultPresentationModel(ownerAsWithRole2, r(localRole2));
162         // when
163         final var role3 = underTest.maybeAs(Role3.class);
164         // then
165         assertThat(role3.isPresent(), is(false));
166       }
167   }