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 jakarta.annotation.Nonnull;
29  import java.util.Collection;
30  import java.util.List;
31  import java.util.stream.IntStream;
32  import it.tidalwave.util.NotFoundException;
33  import lombok.RequiredArgsConstructor;
34  import org.testng.annotations.Test;
35  import org.hamcrest.BaseMatcher;
36  import org.hamcrest.Description;
37  import static java.util.Arrays.asList;
38  import static java.util.Collections.emptyList;
39  import static java.util.stream.Collectors.*;
40  import static org.testng.FileAssert.fail;
41  import static org.mockito.Mockito.*;
42  import static org.hamcrest.CoreMatchers.*;
43  import static org.hamcrest.MatcherAssert.assertThat;
44  
45  /***************************************************************************************************************************************************************
46   *
47   *
48   **************************************************************************************************************************************************************/
49  public class UserActionProviderTest
50    {
51      @RequiredArgsConstructor(staticName = "sameInstances")
52      static class InstanceMatcher extends BaseMatcher<Collection<? extends UserAction>>
53        {
54          private final List<UserAction> expected;
55  
56          @Override
57          public boolean matches (@Nonnull final Object object)
58            {
59              final var actual = (List<UserAction>)object;
60  
61               if (expected.size() != actual.size())
62                 {
63                   return false;
64                 }
65  
66               for (var i = 0; i < actual.size(); i++)
67                 {
68                   if (expected.get(i) != actual.get(i))
69                     {
70                       return false;
71                     }
72                 }
73  
74               return true;
75            }
76  
77          @Override
78          public void describeTo(final Description description)
79            {
80              description.appendValue(expected);
81            }
82        }
83  
84      private final UserAction[] ACTIONS = IntStream.range(0, 10)
85                                                    .mapToObj(i -> mock(UserAction.class))
86                                                    .collect(toList())
87                                                    .toArray(new UserAction[0]);
88  
89      private final UserAction[] NO_ACTIONS = new UserAction[0];
90  
91      @Test
92      public void works_with_actions()
93        throws NotFoundException
94        {
95          // when
96          final var underTest = UserActionProvider.of(ACTIONS);
97          // then
98          assertThat(underTest.getActions(), is(InstanceMatcher.sameInstances(asList(ACTIONS))));
99          assertThat(underTest.getDefaultAction(), sameInstance(ACTIONS[0]));
100         assertThat(underTest.getOptionalDefaultAction().orElseThrow(), sameInstance(ACTIONS[0]));
101       }
102 
103     @Test
104     public void works_with_no_actions()
105       {
106         // when
107         final var underTest = UserActionProvider.of(NO_ACTIONS);
108         // then
109         assertThat(underTest.getActions(), is(emptyList()));
110         assertThat(underTest.getOptionalDefaultAction().isPresent(), is(false));
111 
112         try
113           {
114             underTest.getDefaultAction();
115             fail("Expected NotFoundException");
116           }
117         catch (NotFoundException e)
118           {
119             // ok
120           }
121       }
122   }