Skip to contentMethod: hasUserActionProvider()
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.test;
27:
28: import jakarta.annotation.Nonnull;
29: import org.springframework.util.function.ThrowingSupplier;
30: import it.tidalwave.ui.core.BoundProperty;
31: import org.apiguardian.api.API;
32: import org.assertj.core.api.Assertions;
33: import org.assertj.core.api.ObjectAssert;
34: import it.tidalwave.util.As;
35: import lombok.RequiredArgsConstructor;
36: import lombok.ToString;
37: import org.mockito.ArgumentCaptor;
38: import static it.tidalwave.ui.core.role.Displayable._Displayable_;
39: import static it.tidalwave.ui.core.role.Styleable._Styleable_;
40: import static it.tidalwave.ui.core.role.UserActionProvider._UserActionProvider_;
41: import static it.tidalwave.role.Aggregate._Aggregate_;
42: import static it.tidalwave.role.SimpleComposite._SimpleComposite_;
43: import static lombok.AccessLevel.PROTECTED;
44:
45: /***************************************************************************************************************************************************************
46: *
47: * Assertions for UI core.
48: * @since 3.0-ALPHA-3
49: * @author Fabrizio Giudici
50: *
51: **************************************************************************************************************************************************************/
52: @API(status = API.Status.EXPERIMENTAL)
53: @RequiredArgsConstructor(staticName = "of", access = PROTECTED) @ToString
54: public class AsAssert<T extends As>
55: {
56: /** The object under inspection. */
57: @Nonnull
58: protected final T object;
59:
60: /***********************************************************************************************************************************************************
61: * Starts a sequence of assertions on the given object.
62: * @param object the object
63: * @return an object for fluent assertions
64: **********************************************************************************************************************************************************/
65: @Nonnull
66: public static AsAssert<As> assertThat (@Nonnull final As object)
67: {
68: return new AsAssert<>(object);
69: }
70:
71: /***********************************************************************************************************************************************************
72: * Starts a sequence of assertions on the given object.
73: * @param captor the object, provided as a mockito {@link ArgumentCaptor}
74: * @return an object for fluent assertions
75: **********************************************************************************************************************************************************/
76: @Nonnull
77: public static AsAssert<As> assertThat (@Nonnull final ArgumentCaptor<? extends As> captor)
78: {
79: return new AsAssert<>(captor.getValue());
80: }
81:
82: /***********************************************************************************************************************************************************
83: * Starts a sequence of assertions on the given property.
84: * @param <T> the static type of the property
85: * @param property the property
86: * @return an object for fluent assertions
87: **********************************************************************************************************************************************************/
88: @Nonnull
89: public static <T> ObjectAssert<T> assertThat (@Nonnull final BoundProperty<T> property)
90: {
91: Assertions.assertThat(property).isNotNull();
92: return Assertions.assertThat(property.get());
93: }
94:
95: /***********************************************************************************************************************************************************
96: * Asserts that the object under inspection has a {@link it.tidalwave.ui.core.role.Displayable} role.
97: * @return an object for fluent further assertions
98: **********************************************************************************************************************************************************/
99: @Nonnull
100: public DisplayableAssert hasDisplayable()
101: {
102: return DisplayableAssert.of(getRole(As.type(_Displayable_)));
103: }
104:
105: /***********************************************************************************************************************************************************
106: * Asserts that the object under inspection has the given display name.
107: * @param displayName the display name
108: * @return an object for fluent further assertions
109: **********************************************************************************************************************************************************/
110: @Nonnull
111: public AsAssert<T> hasDisplayName (@Nonnull final String displayName)
112: {
113: hasDisplayable().withDisplayName(displayName);
114: return this;
115: }
116:
117: /***********************************************************************************************************************************************************
118: * Asserts that the object under inspection has the given display name.
119: * @param displayName the display name
120: * @return an object for fluent further assertions
121: **********************************************************************************************************************************************************/
122:
123: @Nonnull
124: public AsAssert<T> hasDisplayName (@Nonnull final ThrowingSupplier<String> displayName)
125: {
126: hasDisplayable().withDisplayName(displayName);
127: return this;
128: }
129:
130: /***********************************************************************************************************************************************************
131: * Asserts that the object under inspection has a {@link it.tidalwave.ui.core.role.Styleable} role.
132: * @return an object for fluent further assertions
133: **********************************************************************************************************************************************************/
134: @Nonnull
135: public StyleableAssert hasStyleable()
136: {
137: return StyleableAssert.of(getRole(As.type(_Styleable_)));
138: }
139:
140: /***********************************************************************************************************************************************************
141: * Asserts that the object under inspection has a {@link it.tidalwave.role.SimpleComposite} role of the given type
142: * @param <C> the static type of the composite
143: * @param compositeType the type of the composite
144: * @return an object for fluent further assertions
145: **********************************************************************************************************************************************************/
146: @Nonnull
147: public <C extends As> CompositeAssert<C> hasCompositeOf (@Nonnull final Class<C> compositeType)
148: {
149: return CompositeAssert.of(compositeType, getRole(As.type(_SimpleComposite_)));
150: }
151:
152: /***********************************************************************************************************************************************************
153: * Asserts that the object under inspection has a {@link it.tidalwave.role.Aggregate} role of the given type
154: * @param <C> the static type of the aggregate
155: * @param aggregateType the type of the aggregate
156: * @return an object for fluent further assertions
157: **********************************************************************************************************************************************************/
158: @Nonnull
159: public <C extends As> AggregateAssert<C> hasAggregateOf (@Nonnull final Class<C> aggregateType)
160: {
161: return AggregateAssert.of(aggregateType, getRole(As.type(_Aggregate_)));
162: }
163:
164: /***********************************************************************************************************************************************************
165: * Asserts that the object under inspection has a {@link it.tidalwave.ui.core.role.UserActionProvider} role.
166: * @return an object for fluent further assertions
167: **********************************************************************************************************************************************************/
168: @Nonnull
169: public UserActionProviderAssert hasUserActionProvider()
170: {
171: return UserActionProviderAssert.of(getRole(As.type(_UserActionProvider_)));
172: }
173:
174: /***********************************************************************************************************************************************************
175: * Asserts that the object under inspection has the given role.
176: * @param <R> the static type of the role
177: * @param roleType the type of the role
178: * @return an object for fluent further assertions
179: **********************************************************************************************************************************************************/
180: @Nonnull
181: public <R> ObjectAssert<R> hasRole (@Nonnull final Class<R> roleType)
182: {
183: return hasRole(As.type(roleType));
184: }
185:
186: /***********************************************************************************************************************************************************
187: * Asserts that the object under inspection has the given role.
188: * @param <R> the static type of the role
189: * @param roleType the type of the role
190: * @return an object for fluent further assertions
191: **********************************************************************************************************************************************************/
192: @Nonnull
193: public <R> ObjectAssert<R> hasRole (@Nonnull final As.Type<R> roleType)
194: {
195: return Assertions.assertThat(getRole(roleType));
196: }
197:
198: /***********************************************************************************************************************************************************
199: *
200: **********************************************************************************************************************************************************/
201: @Nonnull
202: private <R> R getRole (@Nonnull As.Type<R> roleType)
203: {
204: return object.maybeAs(roleType).orElseThrow(() -> new AssertionError(String.format("%s doesn't have role %s", object, roleType)));
205: }
206: }