3535import org .apache .shenyu .common .constant .AdminConstants ;
3636import org .apache .shenyu .common .dto .MetaData ;
3737import org .apache .shenyu .register .common .dto .MetaDataRegisterDTO ;
38+ import org .apache .shiro .authz .UnauthorizedException ;
39+ import org .apache .shiro .subject .Subject ;
40+ import org .apache .shiro .util .ThreadContext ;
3841import org .junit .jupiter .api .AfterAll ;
42+ import org .junit .jupiter .api .AfterEach ;
3943import org .junit .jupiter .api .Assertions ;
4044import org .junit .jupiter .api .BeforeAll ;
45+ import org .junit .jupiter .api .BeforeEach ;
4146import org .junit .jupiter .api .Test ;
4247import org .junit .jupiter .api .extension .ExtendWith ;
4348import org .mockito .ArgumentCaptor ;
5863
5964import static org .apache .shenyu .common .constant .Constants .SYS_DEFAULT_NAMESPACE_ID ;
6065import static org .junit .jupiter .api .Assertions .assertNotNull ;
66+ import static org .junit .jupiter .api .Assertions .assertThrows ;
6167import static org .mockito .ArgumentMatchers .any ;
6268import static org .mockito .ArgumentMatchers .anyString ;
6369import static org .mockito .BDDMockito .given ;
6470import static org .mockito .Mockito .CALLS_REAL_METHODS ;
6571import static org .mockito .Mockito .doNothing ;
72+ import static org .mockito .Mockito .doThrow ;
6673import static org .mockito .Mockito .mockStatic ;
6774import static org .mockito .Mockito .never ;
6875import static org .mockito .Mockito .spy ;
@@ -102,6 +109,9 @@ public final class MetaDataServiceTest {
102109 @ Mock
103110 private NamespaceMapper namespaceMapper ;
104111
112+ @ Mock
113+ private Subject subject ;
114+
105115 @ BeforeAll
106116 public static void beforeClass () {
107117 loggerSpy = spy (LoggerFactory .getLogger (MetaDataServiceImpl .class ));
@@ -114,6 +124,16 @@ public static void afterClass() {
114124 loggerFactoryMockedStatic .close ();
115125 }
116126
127+ @ BeforeEach
128+ public void setUp () {
129+ ThreadContext .bind (subject );
130+ }
131+
132+ @ AfterEach
133+ public void tearDown () {
134+ ThreadContext .unbindSubject ();
135+ }
136+
117137 /**
118138 * Test case for saveOrUpdateMetaData().
119139 */
@@ -133,6 +153,89 @@ public void testCreateOrUpdate() {
133153 testCreateOrUpdateForUpdate ();
134154 }
135155
156+ /**
157+ * Test case for an add-only subject creating metadata with a blank id.
158+ */
159+ @ Test
160+ public void testAddOnlyPermissionAllowsCreateWithBlankId () {
161+ MetaDataDTO dto = new MetaDataDTO ();
162+ dto .setId (" " );
163+ dto .setPath ("path" );
164+ dto .setNamespaceId (SYS_DEFAULT_NAMESPACE_ID );
165+ when (metaDataMapper .pathExisted ("path" , SYS_DEFAULT_NAMESPACE_ID )).thenReturn (null );
166+ when (metaDataMapper .insert (any ())).thenReturn (1 );
167+
168+ assertEquals (ShenyuResultMessage .CREATE_SUCCESS , metaDataService .createOrUpdate (dto ));
169+
170+ verify (subject ).checkPermission ("system:meta:add" );
171+ verify (subject , never ()).checkPermission ("system:meta:edit" );
172+ verify (metaDataMapper ).insert (any ());
173+ }
174+
175+ /**
176+ * Test case for an edit-only subject updating existing metadata.
177+ */
178+ @ Test
179+ public void testEditOnlyPermissionAllowsUpdate () {
180+ MetaDataDTO dto = new MetaDataDTO ();
181+ dto .setId ("id" );
182+ dto .setPath ("path" );
183+ dto .setNamespaceId (SYS_DEFAULT_NAMESPACE_ID );
184+ MetaDataDO current = MetaDataDO .builder ().id ("id" ).enabled (true ).build ();
185+ when (metaDataMapper .pathExistedExclude ("path" , SYS_DEFAULT_NAMESPACE_ID , Collections .singletonList ("id" ))).thenReturn (null );
186+ when (metaDataMapper .selectById ("id" )).thenReturn (current );
187+ when (metaDataMapper .update (any ())).thenReturn (1 );
188+
189+ assertEquals (ShenyuResultMessage .UPDATE_SUCCESS , metaDataService .createOrUpdate (dto ));
190+
191+ verify (subject ).checkPermission ("system:meta:edit" );
192+ verify (subject , never ()).checkPermission ("system:meta:add" );
193+ verify (metaDataMapper ).update (any ());
194+ }
195+
196+ /**
197+ * Test case for an edit-only subject attempting to create metadata.
198+ */
199+ @ Test
200+ public void testEditOnlyPermissionCannotCreate () {
201+ MetaDataDTO dto = new MetaDataDTO ();
202+ doThrow (new UnauthorizedException ()).when (subject ).checkPermission ("system:meta:add" );
203+
204+ assertThrows (UnauthorizedException .class , () -> metaDataService .createOrUpdate (dto ));
205+
206+ verifyNoInteractions (metaDataMapper , eventPublisher , publisher );
207+ }
208+
209+ /**
210+ * Test case for an add-only subject attempting to update a missing metadata id.
211+ */
212+ @ Test
213+ public void testAddOnlyPermissionCannotUpdateMissingMetadata () {
214+ MetaDataDTO dto = new MetaDataDTO ();
215+ dto .setId ("missing" );
216+ doThrow (new UnauthorizedException ()).when (subject ).checkPermission ("system:meta:edit" );
217+
218+ assertThrows (UnauthorizedException .class , () -> metaDataService .createOrUpdate (dto ));
219+
220+ verifyNoInteractions (metaDataMapper , eventPublisher , publisher );
221+ }
222+
223+ /**
224+ * Test case for a subject without either metadata write permission.
225+ */
226+ @ Test
227+ public void testNoPermissionRejectsCreateAndUpdate () {
228+ doThrow (new UnauthorizedException ()).when (subject ).checkPermission (anyString ());
229+ MetaDataDTO create = new MetaDataDTO ();
230+ MetaDataDTO update = new MetaDataDTO ();
231+ update .setId ("id" );
232+
233+ assertThrows (UnauthorizedException .class , () -> metaDataService .createOrUpdate (create ));
234+ assertThrows (UnauthorizedException .class , () -> metaDataService .createOrUpdate (update ));
235+
236+ verifyNoInteractions (metaDataMapper , eventPublisher , publisher );
237+ }
238+
136239 /**
137240 * Test case for update publishes sibling methods whose rpc_ext changed.
138241 */
0 commit comments