1+ package com .jaspersoft .jasperserver .jaxrs .client .apiadapters .authority .organizations ;
2+
3+ import com .jaspersoft .jasperserver .dto .authority .ClientTenant ;
4+ import com .jaspersoft .jasperserver .jaxrs .client .core .Callback ;
5+ import com .jaspersoft .jasperserver .jaxrs .client .core .JerseyRequest ;
6+ import com .jaspersoft .jasperserver .jaxrs .client .core .RequestExecution ;
7+ import com .jaspersoft .jasperserver .jaxrs .client .core .SessionStorage ;
8+ import com .jaspersoft .jasperserver .jaxrs .client .core .exceptions .handling .DefaultErrorHandler ;
9+ import com .jaspersoft .jasperserver .jaxrs .client .core .operationresult .OperationResult ;
10+ import java .util .concurrent .atomic .AtomicInteger ;
11+ import org .mockito .Mock ;
12+ import org .mockito .internal .util .reflection .Whitebox ;
13+ import org .powermock .core .classloader .annotations .PrepareForTest ;
14+ import org .powermock .modules .testng .PowerMockTestCase ;
15+ import org .testng .annotations .BeforeMethod ;
16+ import org .testng .annotations .Test ;
17+
18+ import static com .jaspersoft .jasperserver .jaxrs .client .core .JerseyRequest .buildRequest ;
19+ import static org .mockito .Matchers .any ;
20+ import static org .mockito .Matchers .anyString ;
21+ import static org .mockito .Matchers .eq ;
22+ import static org .mockito .Mockito .times ;
23+ import static org .mockito .Mockito .verify ;
24+ import static org .mockito .MockitoAnnotations .initMocks ;
25+ import static org .powermock .api .mockito .PowerMockito .doReturn ;
26+ import static org .powermock .api .mockito .PowerMockito .mockStatic ;
27+ import static org .powermock .api .mockito .PowerMockito .spy ;
28+ import static org .powermock .api .mockito .PowerMockito .verifyPrivate ;
29+ import static org .powermock .api .mockito .PowerMockito .verifyStatic ;
30+ import static org .powermock .api .mockito .PowerMockito .when ;
31+ import static org .testng .Assert .assertNotSame ;
32+ import static org .testng .AssertJUnit .assertEquals ;
33+ import static org .testng .AssertJUnit .assertNotNull ;
34+
35+ /**
36+ * @author Tetiana Iefimenko
37+ */
38+ @ PrepareForTest ({OrganizationsService .class , JerseyRequest .class })
39+ public class SingleOrganizationAdapterTest extends PowerMockTestCase {
40+
41+ @ Mock
42+ private SessionStorage sessionStorageMock ;
43+ @ Mock
44+ private SingleOrganizationAdapter singleOrganizationAdapterMock ;
45+ @ Mock
46+ private JerseyRequest <ClientTenant > jerseyRequestMock ;
47+ @ Mock
48+ private OperationResult <ClientTenant > operationResultMock ;
49+
50+ @ BeforeMethod
51+ public void before () {
52+ initMocks (this );
53+ }
54+
55+ @ Test
56+ public void should_return_proper_instance_of_SingleOrganizationAdapter () throws Exception {
57+
58+ // When
59+ SingleOrganizationAdapter adapter = new SingleOrganizationAdapter (sessionStorageMock , "orgId" );
60+
61+ // Then
62+ assertEquals (Whitebox .getInternalState (adapter , "sessionStorage" ), sessionStorageMock );
63+ assertEquals (Whitebox .getInternalState (adapter , "organizationId" ), "orgId" );
64+ }
65+
66+ @ Test
67+ public void should_run_get_method_asynchronously () throws Exception {
68+
69+ // Given
70+ mockStatic (JerseyRequest .class );
71+ when (buildRequest (eq (sessionStorageMock ), eq (ClientTenant .class ), eq (new String []{"/organizations" , "orgId" }), any (DefaultErrorHandler .class ))).thenReturn (jerseyRequestMock );
72+ doReturn (operationResultMock ).when (jerseyRequestMock ).get ();
73+ SingleOrganizationAdapter adapterSpy = spy (new SingleOrganizationAdapter (sessionStorageMock , "orgId" ));
74+
75+ final AtomicInteger newThreadId = new AtomicInteger ();
76+ final int currentThreadId = (int ) Thread .currentThread ().getId ();
77+
78+ final Callback <OperationResult <ClientTenant >, Void > callback = spy (new Callback <OperationResult <ClientTenant >, Void >() {
79+ @ Override
80+ public Void execute (OperationResult <ClientTenant > data ) {
81+ newThreadId .set ((int ) Thread .currentThread ().getId ());
82+ synchronized (this ) {
83+ this .notifyAll ();
84+ }
85+ return null ;
86+ }
87+ });
88+
89+ doReturn (null ).when (callback ).execute (operationResultMock );
90+
91+ // When
92+ RequestExecution retrieved = adapterSpy .asyncGet (callback );
93+
94+ synchronized (callback ) {
95+ callback .wait (1000 );
96+ }
97+
98+ // Then
99+ verify (jerseyRequestMock ).get ();
100+ verify (callback ).execute (operationResultMock );
101+ assertNotNull (retrieved );
102+ assertNotSame (currentThreadId , newThreadId .get ());
103+ }
104+
105+ @ Test
106+ public void should_run_get_method_synchronously () throws Exception {
107+
108+ // Given
109+ mockStatic (JerseyRequest .class );
110+ when (buildRequest (eq (sessionStorageMock ), eq (ClientTenant .class ), eq (new String []{"/organizations" , "orgId" }), any (DefaultErrorHandler .class ))).thenReturn (jerseyRequestMock );
111+ doReturn (operationResultMock ).when (jerseyRequestMock ).get ();
112+ SingleOrganizationAdapter adapterSpy = spy (new SingleOrganizationAdapter (sessionStorageMock , "orgId" ));
113+ // When
114+ OperationResult <ClientTenant > retrieved = adapterSpy .get ();
115+
116+ // Then
117+ verifyStatic (times (1 ));
118+ JerseyRequest .buildRequest (eq (sessionStorageMock ), eq (ClientTenant .class ), eq (new String []{"/organizations" , "orgId" }), any (DefaultErrorHandler .class ));
119+ verify (jerseyRequestMock ).get ();
120+ verifyPrivate (adapterSpy ).invoke ("buildRequest" );
121+ assertNotNull (retrieved );
122+
123+ }
124+
125+
126+ @ Test
127+ public void should_run_delete_method () throws Exception {
128+
129+ // Given
130+ mockStatic (JerseyRequest .class );
131+ when (buildRequest (eq (sessionStorageMock ), eq (ClientTenant .class ), eq (new String []{"/organizations" , "orgId" }), any (DefaultErrorHandler .class ))).thenReturn (jerseyRequestMock );
132+ doReturn (operationResultMock ).when (jerseyRequestMock ).delete ();
133+ SingleOrganizationAdapter adapterSpy = spy (new SingleOrganizationAdapter (sessionStorageMock , "orgId" ));
134+ // When
135+ OperationResult <ClientTenant > retrieved = adapterSpy .delete ();
136+
137+ // Then
138+ verifyStatic (times (1 ));
139+ JerseyRequest .buildRequest (eq (sessionStorageMock ), eq (ClientTenant .class ), eq (new String []{"/organizations" , "orgId" }), any (DefaultErrorHandler .class ));
140+ verify (jerseyRequestMock ).delete ();
141+ verifyPrivate (adapterSpy ).invoke ("buildRequest" );
142+ assertNotNull (retrieved );
143+
144+ }
145+
146+ @ Test
147+ public void should_run_createOrUpdate_method () throws Exception {
148+
149+ // Given
150+ ClientTenant clientTenantMock = new ClientTenant ();
151+ mockStatic (JerseyRequest .class );
152+ when (buildRequest (eq (sessionStorageMock ), eq (ClientTenant .class ), eq (new String []{"/organizations" , "orgId" }), any (DefaultErrorHandler .class ))).thenReturn (jerseyRequestMock );
153+ doReturn (operationResultMock ).when (jerseyRequestMock ).put (anyString ());
154+ SingleOrganizationAdapter adapterSpy = spy (new SingleOrganizationAdapter (sessionStorageMock , "orgId" ));
155+ // When
156+ OperationResult <ClientTenant > retrieved = adapterSpy .createOrUpdate (clientTenantMock );
157+
158+ // Then
159+ verifyStatic (times (1 ));
160+ JerseyRequest .buildRequest (eq (sessionStorageMock ), eq (ClientTenant .class ), eq (new String []{"/organizations" , "orgId" }), any (DefaultErrorHandler .class ));
161+ verify (jerseyRequestMock ).put (anyString ());
162+ verifyPrivate (adapterSpy ).invoke ("buildRequest" );
163+ assertNotNull (retrieved );
164+
165+ }
166+ }
0 commit comments