1+ package com .jaspersoft .jasperserver .jaxrs .client .apiadapters .attributes ;
2+
3+ import com .jaspersoft .jasperserver .dto .authority .ClientTenant ;
4+ import com .jaspersoft .jasperserver .dto .authority .ClientUser ;
5+ import com .jaspersoft .jasperserver .jaxrs .client .core .SessionStorage ;
6+ import java .util .List ;
7+ import org .powermock .core .classloader .annotations .PrepareForTest ;
8+ import org .powermock .modules .testng .PowerMockTestCase ;
9+ import org .testng .Assert ;
10+ import org .testng .annotations .AfterMethod ;
11+ import org .testng .annotations .BeforeMethod ;
12+ import org .testng .annotations .Test ;
13+
14+ import static java .util .Arrays .asList ;
15+ import static org .mockito .Mockito .mock ;
16+ import static org .mockito .Mockito .times ;
17+ import static org .powermock .api .mockito .PowerMockito .verifyNew ;
18+ import static org .powermock .api .mockito .PowerMockito .whenNew ;
19+ import static org .testng .Assert .assertSame ;
20+
21+ /**
22+ * Unit tests for {@link com.jaspersoft.jasperserver.jaxrs.client.apiadapters.attributes.AttributesService}
23+ */
24+ @ PrepareForTest ({AttributesService .class })
25+ public class AttributesServiceTest extends PowerMockTestCase {
26+
27+ private SessionStorage sessionStorageMock ;
28+ private SingleAttributeAdapter singleAttributeAdapterMock ;
29+ private BatchAttributeAdapter batchAttributeAdapterMock ;
30+
31+ @ BeforeMethod
32+ public void before () {
33+ sessionStorageMock = mock (SessionStorage .class );
34+ singleAttributeAdapterMock = mock (SingleAttributeAdapter .class );
35+ batchAttributeAdapterMock = mock (BatchAttributeAdapter .class );
36+ }
37+
38+ @ Test
39+ /**
40+ * for {@link com.jaspersoft.jasperserver.jaxrs.client.apiadapters.attributes.AttributesService#ServerAttributesService(com.jaspersoft.jasperserver.jaxrs.client.core.SessionStorage)}
41+ */
42+ public void should_pass_session_storage_to_parent_adapter () {
43+ AttributesService attributesService = new AttributesService (sessionStorageMock );
44+ SessionStorage retrieved = attributesService .getSessionStorage ();
45+ assertSame (retrieved , sessionStorageMock );
46+ }
47+
48+ @ Test
49+ /**
50+ * for {@link com.jaspersoft.jasperserver.jaxrs.client.apiadapters.attributes.AttributesService#attribute()}
51+ */
52+ public void should_return_proper_ServerSingleAttributeAdapter_instance () throws Exception {
53+
54+ // Given
55+ whenNew (SingleAttributeAdapter .class )
56+ .withArguments ("/" , sessionStorageMock , "attrName" )
57+ .thenReturn (singleAttributeAdapterMock );
58+
59+ AttributesService attributesService = new AttributesService (sessionStorageMock );
60+
61+ // When
62+ SingleAttributeAdapter retrieved = attributesService .attribute ("attrName" );
63+
64+ // Then
65+ assertSame (retrieved , singleAttributeAdapterMock );
66+ verifyNew (SingleAttributeAdapter .class , times (1 ))
67+ .withArguments ("/" , sessionStorageMock , "attrName" );
68+ }
69+
70+ @ Test
71+ /**
72+ * for {@link com.jaspersoft.jasperserver.jaxrs.client.apiadapters.attributes.AttributesService#attribute()}
73+ */
74+ public void should_return_proper_ServerSingleAttributeAdapter_instance_for_organization () throws Exception {
75+
76+ // Given
77+ whenNew (SingleAttributeAdapter .class )
78+ .withArguments ("/organizations/orgName/" , sessionStorageMock , "attrName" )
79+ .thenReturn (singleAttributeAdapterMock );
80+
81+ AttributesService attributesService = new AttributesService (sessionStorageMock );
82+
83+ // When
84+ SingleAttributeAdapter retrieved = attributesService .forOrganization ("orgName" ).attribute ("attrName" );
85+
86+ // Then
87+ assertSame (retrieved , singleAttributeAdapterMock );
88+ verifyNew (SingleAttributeAdapter .class , times (1 ))
89+ .withArguments ("/organizations/orgName/" , sessionStorageMock , "attrName" );
90+ }
91+
92+ @ Test
93+ /**
94+ * for {@link com.jaspersoft.jasperserver.jaxrs.client.apiadapters.attributes.AttributesService#attribute()}
95+ */
96+ public void should_return_proper_ServerSingleAttributeAdapter_instance_for_organization_as_object () throws Exception {
97+
98+ // Given
99+ whenNew (SingleAttributeAdapter .class )
100+ .withArguments ("/organizations/orgName/" , sessionStorageMock , "attrName" )
101+ .thenReturn (singleAttributeAdapterMock );
102+
103+ AttributesService attributesService = new AttributesService (sessionStorageMock );
104+
105+ // When
106+ SingleAttributeAdapter retrieved = attributesService
107+ .forOrganization (new ClientTenant ()
108+ .setId ("orgName" ))
109+ .attribute ("attrName" );
110+
111+ // Then
112+ assertSame (retrieved , singleAttributeAdapterMock );
113+ verifyNew (SingleAttributeAdapter .class , times (1 ))
114+ .withArguments ("/organizations/orgName/" , sessionStorageMock , "attrName" );
115+ }
116+
117+ @ Test
118+ /**
119+ * for {@link com.jaspersoft.jasperserver.jaxrs.client.apiadapters.attributes.AttributesService#attribute()}
120+ */
121+ public void should_return_proper_ServerSingleAttributeAdapter_instance_for_user () throws Exception {
122+
123+ // Given
124+ whenNew (SingleAttributeAdapter .class )
125+ .withArguments ("/users/userName/" , sessionStorageMock , "attrName" )
126+ .thenReturn (singleAttributeAdapterMock );
127+
128+ AttributesService attributesService = new AttributesService (sessionStorageMock );
129+
130+ // When
131+ SingleAttributeAdapter retrieved = attributesService .forUser ("userName" ).attribute ("attrName" );
132+
133+ // Then
134+ assertSame (retrieved , singleAttributeAdapterMock );
135+ verifyNew (SingleAttributeAdapter .class , times (1 ))
136+ .withArguments ("/users/userName/" , sessionStorageMock , "attrName" );
137+ }
138+
139+
140+ @ Test
141+ /**
142+ * for {@link com.jaspersoft.jasperserver.jaxrs.client.apiadapters.attributes.AttributesService#attribute()}
143+ */
144+ public void should_return_proper_ServerSingleAttributeAdapter_instance_for_user_as_object () throws Exception {
145+
146+ // Given
147+ whenNew (SingleAttributeAdapter .class )
148+ .withArguments ("/users/userName/" , sessionStorageMock , "attrName" )
149+ .thenReturn (singleAttributeAdapterMock );
150+
151+ AttributesService attributesService = new AttributesService (sessionStorageMock );
152+
153+ // When
154+ SingleAttributeAdapter retrieved = attributesService
155+ .forUser (new ClientUser ()
156+ .setUsername ("userName" ))
157+ .attribute ("attrName" );
158+
159+ // Then
160+ assertSame (retrieved , singleAttributeAdapterMock );
161+ verifyNew (SingleAttributeAdapter .class , times (1 ))
162+ .withArguments ("/users/userName/" , sessionStorageMock , "attrName" );
163+ }
164+
165+ @ Test
166+ /**
167+ * for {@link com.jaspersoft.jasperserver.jaxrs.client.apiadapters.attributes.AttributesService#attribute()}
168+ */
169+ public void should_return_proper_ServerSingleAttributeAdapter_instance_for_organization_for_user () throws Exception {
170+
171+ // Given
172+ whenNew (SingleAttributeAdapter .class )
173+ .withArguments ("/organizations/orgName/users/userName/" , sessionStorageMock , "attrName" )
174+ .thenReturn (singleAttributeAdapterMock );
175+
176+ AttributesService attributesService = new AttributesService (sessionStorageMock );
177+
178+ // When
179+ SingleAttributeAdapter retrieved = attributesService .forOrganization ("orgName" ).forUser ("userName" ).attribute ("attrName" );
180+
181+ // Then
182+ assertSame (retrieved , singleAttributeAdapterMock );
183+ verifyNew (SingleAttributeAdapter .class , times (1 ))
184+ .withArguments ("/organizations/orgName/users/userName/" , sessionStorageMock , "attrName" );
185+ }
186+
187+
188+ @ Test
189+ /**
190+ * for {@link com.jaspersoft.jasperserver.jaxrs.client.apiadapters.attributes.AttributesService#attribute(String)}
191+ */
192+ public void should_construct_an_object_with_proper_params () throws Exception {
193+
194+ // Given /
195+ whenNew (SingleAttributeAdapter .class )
196+ .withArguments ("/" , sessionStorageMock , "status" )
197+ .thenReturn (singleAttributeAdapterMock );
198+
199+ AttributesService attributesService = new AttributesService (sessionStorageMock );
200+
201+ // When
202+ SingleAttributeAdapter retrieved = attributesService .attribute ("status" );
203+
204+ // Then
205+ Assert .assertSame (retrieved , singleAttributeAdapterMock );
206+ verifyNew (SingleAttributeAdapter .class , times (1 ))
207+ .withArguments ("/" , sessionStorageMock , "status" );
208+ }
209+
210+ @ Test
211+ /**
212+ * for {@link com.jaspersoft.jasperserver.jaxrs.client.apiadapters.attributes.AttributesService#attribute()}
213+ */
214+ public void should_return_proper_BatchAttributeAdapter_instance () throws Exception {
215+
216+ // Given
217+ whenNew (BatchAttributeAdapter .class )
218+ .withArguments ("/" , sessionStorageMock )
219+ .thenReturn (batchAttributeAdapterMock );
220+
221+ AttributesService attributesService = new AttributesService (sessionStorageMock );
222+
223+ // When
224+ BatchAttributeAdapter retrieved = attributesService .allAttributes ();
225+
226+ // Then
227+ assertSame (retrieved , batchAttributeAdapterMock );
228+ verifyNew (BatchAttributeAdapter .class , times (1 ))
229+ .withArguments ("/" , sessionStorageMock );
230+ }
231+
232+ @ Test
233+ /**
234+ * for {@link com.jaspersoft.jasperserver.jaxrs.client.apiadapters.attributes.AttributesService#attributes(java.util.Collection)}
235+ */
236+ public void should_instantiate_proper_BatchAttributeAdapter_instance () throws Exception {
237+
238+ // Given
239+ List <String > list = asList ("attr1" , "attr2" , "attr3" );
240+ whenNew (BatchAttributeAdapter .class )
241+ .withArguments ("/" , sessionStorageMock , list )
242+ .thenReturn (batchAttributeAdapterMock );
243+
244+ AttributesService attributesService = new AttributesService (sessionStorageMock );
245+
246+ // When
247+ BatchAttributeAdapter retrieved = attributesService .attributes (list );
248+
249+ // Then
250+ assertSame (retrieved , batchAttributeAdapterMock );
251+ verifyNew (BatchAttributeAdapter .class , times (1 ))
252+ .withArguments ("/" , sessionStorageMock , list );
253+ }
254+
255+ @ Test
256+ /**
257+ * for {@link com.jaspersoft.jasperserver.jaxrs.client.apiadapters.attributes.AttributesService#attributes(java.util.Collection)}
258+ */
259+ public void should_instantiate_proper_BatchAttributeAdapter_instance_when_pass_vararg () throws Exception {
260+
261+ // Given
262+ whenNew (BatchAttributeAdapter .class )
263+ .withArguments ("/" , sessionStorageMock , "attr1" , "attr2" , "attr3" )
264+ .thenReturn (batchAttributeAdapterMock );
265+
266+ AttributesService attributesService = new AttributesService (sessionStorageMock );
267+
268+ // When
269+ BatchAttributeAdapter retrieved = attributesService .attributes ("attr1" , "attr2" , "attr3" );
270+
271+ // Then
272+ assertSame (retrieved , batchAttributeAdapterMock );
273+ verifyNew (BatchAttributeAdapter .class , times (1 ))
274+ .withArguments ("/" , sessionStorageMock , "attr1" , "attr2" , "attr3" );
275+ }
276+
277+ @ AfterMethod
278+ public void after () {
279+ sessionStorageMock = null ;
280+ singleAttributeAdapterMock = null ;
281+ batchAttributeAdapterMock = null ;
282+ }
283+ }
0 commit comments