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 javax.annotation.Nullable;
30  import javax.annotation.concurrent.NotThreadSafe;
31  import java.util.ArrayList;
32  import java.util.Collection;
33  import java.util.List;
34  import it.tidalwave.util.AsException;
35  import lombok.NoArgsConstructor;
36  import lombok.extern.slf4j.Slf4j;
37  import org.mockito.ArgumentMatcher;
38  import org.hamcrest.Matcher;
39  import static java.util.stream.Collectors.*;
40  import static it.tidalwave.util.ShortNames.*;
41  
42  /***************************************************************************************************************************************************************
43   *
44   * A {@link Matcher} for {@link PresentationModel}.
45   *
46   * @stereotype  mockito matcher
47   *
48   * @author  Fabrizio Giudici
49   *
50   **************************************************************************************************************************************************************/
51  @NotThreadSafe @NoArgsConstructor(staticName = "presentationModel") @Slf4j
52  public class PresentationModelMatcher implements ArgumentMatcher<PresentationModel>
53    {
54      private final StringBuilder pmDescription = new StringBuilder("PresentationModel");
55  
56      private String separator = "";
57  
58      private final List<Class<?>> expectedRoleTypes = new ArrayList<>();
59  
60      /***********************************************************************************************************************************************************
61       * {@inheritDoc}
62       **********************************************************************************************************************************************************/
63      @Nonnull
64      public PresentationModelMatcher withRole (@Nonnull final Class<?> roleType)
65        {
66          expectedRoleTypes.add(roleType);
67          pmDescription.append(separator).append(" with role ").append(shortName(roleType));
68          separator = ", ";
69          return this;
70        }
71  
72      /***********************************************************************************************************************************************************
73       * {@inheritDoc}
74       **********************************************************************************************************************************************************/
75      @Override
76      public boolean matches (@Nullable final PresentationModel pm)
77        {
78          if (pm == null)
79            {
80              return false;
81            }
82  
83          for (final var roleType : expectedRoleTypes)
84            {
85              try
86                {
87                  pm.as(roleType);
88                }
89              catch (AsException e)
90                {
91                  final var actualRoles = pm.asMany(Object.class);
92                  final Collection<Class<?>> actualRoleTypes =
93                          actualRoles.stream().map(Object::getClass).collect(toList());
94  
95                  log.error("Failed matching: expected roles types:");
96                  expectedRoleTypes.forEach(ert -> log.error("        {}", shortName(ert)));
97                  log.error("Failed matching: actual roles types:");
98                  actualRoleTypes.forEach(art -> log.error("        {}", shortName(art, true)));
99                  log.error("Failed matching: actual roles:");
100                 actualRoles.forEach(ar -> log.error("        {}", shortId(ar)));
101 
102                 return false;
103               }
104           }
105 
106         return true;
107       }
108 
109     /***********************************************************************************************************************************************************
110      * {@inheritDoc}
111      **********************************************************************************************************************************************************/
112     @Override @Nonnull
113     public String toString()
114       {
115         return pmDescription.toString();
116       }
117   }