Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shenyu.admin.exception.ShenyuAdminException;
import org.apache.shenyu.admin.utils.Assert;
import org.apache.shenyu.admin.mapper.NamespacePluginRelMapper;
import org.apache.shenyu.admin.mapper.PluginHandleMapper;
import org.apache.shenyu.admin.mapper.PluginMapper;
Expand Down Expand Up @@ -113,6 +114,9 @@ public NamespacePluginVO create(final String namespaceId, final String pluginId)
@Transactional(rollbackFor = Exception.class)
public String update(final NamespacePluginDTO namespacePluginDTO) {
final NamespacePluginVO before = namespacePluginRelMapper.selectById(namespacePluginDTO.getId());
Assert.notNull(before, "Namespace plugin relation does not exist: " + namespacePluginDTO.getId());
Assert.isTrue(Objects.equals(before.getNamespaceId(), namespacePluginDTO.getNamespaceId()),
"Namespace plugin relation does not belong to namespace: " + namespacePluginDTO.getNamespaceId());
NamespacePluginRelDO namespacePluginRelDO = NamespacePluginRelDO.buildNamespacePluginRelDO(namespacePluginDTO);
if (namespacePluginRelMapper.updateSelective(namespacePluginRelDO) > 0) {
final NamespacePluginVO now = namespacePluginRelMapper.selectById(namespacePluginDTO.getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
import org.apache.shenyu.admin.model.entity.PluginHandleDO;
import org.apache.shenyu.admin.model.entity.SelectorDO;
import org.apache.shenyu.admin.model.vo.NamespacePluginVO;
import org.apache.shenyu.admin.utils.ShenyuResultMessage;
import org.apache.shenyu.admin.model.dto.NamespacePluginDTO;
import org.apache.shenyu.admin.exception.ShenyuAdminException;
import org.apache.shenyu.admin.model.vo.PluginSnapshotVO;
import org.apache.shenyu.admin.service.impl.NamespacePluginServiceImpl;
import org.apache.shenyu.admin.service.publish.NamespacePluginEventPublisher;
Expand Down Expand Up @@ -168,4 +171,46 @@ public void testActivePluginSnapshotEmptyEnabled() {
// verify
Assertions.assertEquals(0, result.size());
}

@Test
public void testUpdateRejectsRelationFromAnotherNamespace() {
NamespacePluginDTO dto = new NamespacePluginDTO();
dto.setId("rel-1");
dto.setNamespaceId("namespace-a");
dto.setPluginId("pluginId");
NamespacePluginVO before = Mockito.mock(NamespacePluginVO.class);
Mockito.when(before.getNamespaceId()).thenReturn("namespace-b");
Mockito.when(namespacePluginRelMapper.selectById("rel-1")).thenReturn(before);

Assertions.assertThrows(ShenyuAdminException.class, () -> namespacePluginService.update(dto));
Mockito.verify(namespacePluginRelMapper, Mockito.never()).updateSelective(Mockito.any());
}

@Test
public void testUpdateRejectsUnknownRelation() {
NamespacePluginDTO dto = new NamespacePluginDTO();
dto.setId("missing-rel");
dto.setNamespaceId("namespace-a");
dto.setPluginId("pluginId");
Mockito.when(namespacePluginRelMapper.selectById("missing-rel")).thenReturn(null);

Assertions.assertThrows(ShenyuAdminException.class, () -> namespacePluginService.update(dto));
Mockito.verify(namespacePluginRelMapper, Mockito.never()).updateSelective(Mockito.any());
}

@Test
public void testUpdateAcceptsRelationFromSameNamespace() {
NamespacePluginDTO dto = new NamespacePluginDTO();
dto.setId("rel-1");
dto.setNamespaceId("namespace-a");
dto.setPluginId("pluginId");
NamespacePluginVO before = Mockito.mock(NamespacePluginVO.class);
Mockito.when(before.getNamespaceId()).thenReturn("namespace-a");
NamespacePluginVO after = Mockito.mock(NamespacePluginVO.class);
Mockito.when(namespacePluginRelMapper.selectById("rel-1")).thenReturn(before, after);
Mockito.when(namespacePluginRelMapper.updateSelective(Mockito.any())).thenReturn(1);

Assertions.assertEquals(ShenyuResultMessage.UPDATE_SUCCESS, namespacePluginService.update(dto));
Mockito.verify(namespacePluginEventPublisher).onUpdated(after, before);
}
}
Loading