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;
27  
28  import java.util.Collection;
29  import java.util.List;
30  import java.util.Optional;
31  import it.tidalwave.util.As;
32  import it.tidalwave.role.spi.SystemRoleFactory;
33  import org.testng.annotations.BeforeClass;
34  import org.testng.annotations.Test;
35  import static it.tidalwave.util.test.MoreAnswers.CALLS_DEFAULT_METHODS;
36  import static it.tidalwave.ui.core.role.Presentable._Presentable_;
37  import static org.mockito.Mockito.*;
38  import static org.hamcrest.CoreMatchers.*;
39  import static org.hamcrest.MatcherAssert.assertThat;
40  
41  /***************************************************************************************************************************************************************
42   *
43   * @author  Fabrizio Giudici
44   *
45   **************************************************************************************************************************************************************/
46  public class PresentationModelTest
47    {
48      static interface MockRole1 {}
49  
50      static interface MockRole2 {}
51  
52      private final MockRole1 mockRole1 = mock(MockRole1.class);
53  
54      private final MockRole2 mockRole2 = mock(MockRole2.class);
55  
56      private final Collection<Object> roles = List.of(mockRole1, mockRole2);
57  
58      /***********************************************************************************************************************************************************
59       * 
60       **********************************************************************************************************************************************************/
61      // START SNIPPET: setup
62      @BeforeClass
63      public void setup()
64        {
65          SystemRoleFactory.reset();
66        }
67      // END SNIPPET: setup
68  
69      /***********************************************************************************************************************************************************
70       * 
71       **********************************************************************************************************************************************************/
72      @Test
73      public void test_ofMaybePresentable_without_Presentable()
74        {
75          // given
76          final var owner = mock(As.class);
77          // when
78          final var actualPm = PresentationModel.ofMaybePresentable(owner, roles);
79          // then
80          assertThat(actualPm.as(MockRole1.class), is(sameInstance(mockRole1)));
81          assertThat(actualPm.as(MockRole2.class), is(sameInstance(mockRole2)));
82        }
83  
84      /***********************************************************************************************************************************************************
85       * 
86       **********************************************************************************************************************************************************/
87      @Test
88      public void test_ofMaybePresentable_with_Presentable()
89        {
90          // given
91          final var owner = mock(As.class);
92          final var presentable = mock(Presentable.class, CALLS_DEFAULT_METHODS);
93          final var pm = mock(PresentationModel.class);
94          when(presentable.createPresentationModel(anyCollection())).thenReturn(pm);
95          when(owner.maybeAs(_Presentable_)).thenReturn(Optional.of(presentable));
96          // when
97          final var actualPm = PresentationModel.ofMaybePresentable(owner, roles);
98          // then
99          assertThat(actualPm, is(sameInstance(pm)));
100         verify(presentable).createPresentationModel(eq(roles));
101         verifyNoMoreInteractions(presentable);
102       }
103   }