11
11
import org .springframework .beans .factory .annotation .Autowired ;
12
12
import org .springframework .boot .test .context .SpringBootTest ;
13
13
import org .springframework .test .context .ActiveProfiles ;
14
+ import reactor .core .publisher .Flux ;
14
15
import reactor .core .publisher .Mono ;
15
16
import reactor .test .StepVerifier ;
16
17
18
+ import java .util .List ;
19
+ import java .util .UUID ;
20
+
17
21
@ SpringBootTest
18
22
//@RunWith(SpringRunner.class)
19
23
@ ActiveProfiles ("OrganizationApiServiceTest" )
@@ -28,6 +32,7 @@ public class OrganizationServiceTest {
28
32
private Mono <Organization > createOrganization (String name ) {
29
33
Organization organization = Organization .builder ()
30
34
.name (name )
35
+ .gid ("gid-" + UUID .randomUUID ())
31
36
.build ();
32
37
return organizationService .create (organization , "user01" , false );
33
38
}
@@ -51,4 +56,133 @@ public void testUpdateSlug() {
51
56
})
52
57
.verifyComplete ();
53
58
}
59
+
60
+ @ Test
61
+ @ WithMockUser
62
+ public void testGetAllActive () {
63
+ // Create a single organization to ensure there is at least one active one in the database
64
+ Mono <Organization > orgMono = createOrganization ("ActiveOrganization" );
65
+
66
+ // Ensure the organization is created and then verify getAllActive
67
+ Flux <Organization > allActiveOrganizations = orgMono .thenMany (organizationService .getAllActive ());
68
+
69
+ StepVerifier .create (allActiveOrganizations .collectList ())
70
+ .assertNext (orgList -> {
71
+ Assertions .assertFalse (orgList .isEmpty (), "The organization list should not be empty" );
72
+ Organization lastOrg = orgList .get (orgList .size () - 1 );
73
+
74
+ // Check the last element in the list
75
+ Assertions .assertNotNull (lastOrg .getId (), "Last organization ID should not be null" );
76
+ Assertions .assertEquals ("ActiveOrganization" , lastOrg .getName (), "Last organization name should match 'ActiveOrganization'" );
77
+ })
78
+ .verifyComplete ();
79
+ }
80
+
81
+ @ Test
82
+ @ WithMockUser
83
+ public void testGetByIds () {
84
+ // Create the first organization and update its gid
85
+ Mono <Organization > org1Mono = createOrganization ("Organization1" )
86
+ .flatMap (org -> {
87
+ org .setGid ("gid-1" );
88
+ return organizationService .update (org .getId (), org )
89
+ .thenReturn (org );
90
+ });
91
+
92
+ // Create the second organization and update its gid
93
+ Mono <Organization > org2Mono = createOrganization ("Organization2" )
94
+ .flatMap (org -> {
95
+ org .setGid ("gid-2" );
96
+ return organizationService .update (org .getId (), org )
97
+ .thenReturn (org );
98
+ });
99
+
100
+ // Use these updated organizations in further operations or assertions
101
+ Flux <Organization > savedOrgs = org1Mono .then (org2Mono )
102
+ .thenMany (organizationService .getByIds (List .of ("gid-1" , "gid-2" )));
103
+
104
+ StepVerifier .create (savedOrgs .collectList ())
105
+ .assertNext (orgList -> {
106
+ Assertions .assertTrue (orgList .size () >= 2 , "Organization list should have at least two elements" );
107
+
108
+ // Check the last two elements
109
+ Organization org1 = orgList .get (orgList .size () - 2 );
110
+ Organization org2 = orgList .get (orgList .size () - 1 );
111
+
112
+ Assertions .assertNotNull (org1 .getId (), "Organization ID should not be null" );
113
+ Assertions .assertEquals ("gid-1" , org1 .getGid (), "First organization GID should match 'gid-1'" );
114
+ Assertions .assertEquals ("Organization1" , org1 .getName (), "First organization name should match 'Organization1'" );
115
+
116
+ Assertions .assertNotNull (org2 .getId (), "Organization ID should not be null" );
117
+ Assertions .assertEquals ("gid-2" , org2 .getGid (), "Second organization GID should match 'gid-2'" );
118
+ Assertions .assertEquals ("Organization2" , org2 .getName (), "Second organization name should match 'Organization2'" );
119
+ })
120
+ .verifyComplete ();
121
+ }
122
+
123
+ @ Test
124
+ @ WithMockUser
125
+ public void testGetOrgCommonSettings () {
126
+
127
+ Organization .OrganizationCommonSettings testSettings = new Organization .OrganizationCommonSettings ();
128
+ testSettings .put ("settingKey1" , "settingValue1" );
129
+ testSettings .put ("settingKey2" , "settingValue2" );
130
+
131
+ // Create the first organization and update its common setting
132
+ Mono <Organization > orgMono = createOrganization ("Organization" )
133
+ .flatMap (org -> {
134
+ org .setCommonSettings (testSettings );
135
+ return organizationService .update (org .getId (), org )
136
+ .thenReturn (org );
137
+ });
138
+
139
+ // Retrieve the common settings from the organization
140
+ Mono <Organization .OrganizationCommonSettings > commonSettingsMono = orgMono
141
+ .flatMap (org -> organizationService .getOrgCommonSettings (org .getId ()));
142
+
143
+ // Verify that the settings match the expected test settings
144
+ StepVerifier .create (commonSettingsMono )
145
+ .assertNext (retrievedSettings -> {
146
+ Assertions .assertEquals ("settingValue1" , retrievedSettings .get ("settingKey1" ), "The setting value for 'settingKey1' should match" );
147
+ Assertions .assertEquals ("settingValue2" , retrievedSettings .get ("settingKey2" ), "The setting value for 'settingKey2' should match" );
148
+ })
149
+ .verifyComplete ();
150
+ }
151
+
152
+ @ Test
153
+ @ WithMockUser
154
+ public void testUpdateCommonSettings () {
155
+ // Initial settings for the organization
156
+ Organization .OrganizationCommonSettings initialSettings = new Organization .OrganizationCommonSettings ();
157
+ initialSettings .put ("settingKey1" , "initialValue1" );
158
+
159
+ // Create an organization with initial settings
160
+ Mono <Organization > orgMono = createOrganization ("CommonSettingsTestOrganization" )
161
+ .flatMap (org -> {
162
+ org .setCommonSettings (initialSettings );
163
+ return organizationService .update (org .getId (), org )
164
+ .thenReturn (org );
165
+ });
166
+
167
+ // Define key and new value to update
168
+ String keyToUpdate = "settingKey1" ;
169
+ String newValue = "updatedValue1" ;
170
+
171
+ // Chain the update and verification operations
172
+ orgMono .flatMap (org ->
173
+ // Update the common settings
174
+ organizationService .updateCommonSettings (org .getId (), keyToUpdate , newValue )
175
+ .flatMap (updateSuccess -> {
176
+ Assertions .assertTrue (updateSuccess , "Update should be successful" );
177
+ // Retrieve updated common settings
178
+ return organizationService .getOrgCommonSettings (org .getId ());
179
+ })
180
+ )
181
+ .as (StepVerifier ::create )
182
+ .assertNext (updatedSettings -> {
183
+ // Verify updated settings
184
+ Assertions .assertEquals (newValue , updatedSettings .get (keyToUpdate ), "The setting value should be updated to 'updatedValue1'" );
185
+ })
186
+ .verifyComplete ();
187
+ }
54
188
}
0 commit comments