Skip to content
Open
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 @@ -112,18 +112,20 @@ private String update(final ApiDTO apiDTO) {
ApiDO apiDO = ApiDO.buildApiDO(apiDTO);
final int updateRows = apiMapper.updateByPrimaryKeySelective(apiDO);
if (updateRows > 0) {
if (CollectionUtils.isNotEmpty(apiDTO.getTagIds())) {
if (Objects.nonNull(apiDTO.getTagIds())) {
List<String> tagIds = apiDTO.getTagIds();
Timestamp currentTime = new Timestamp(System.currentTimeMillis());
List<TagRelationDO> tags = tagIds.stream().map(tagId -> TagRelationDO.builder()
.id(UUIDUtils.getInstance().generateShortUuid())
.apiId(apiDO.getId())
.tagId(tagId)
.dateCreated(currentTime)
.dateUpdated(currentTime)
.build()).collect(Collectors.toList());
tagRelationMapper.deleteByApiId(apiDO.getId());
tagRelationMapper.batchInsert(tags);
if (CollectionUtils.isNotEmpty(tagIds)) {
Timestamp currentTime = new Timestamp(System.currentTimeMillis());
List<TagRelationDO> tags = tagIds.stream().map(tagId -> TagRelationDO.builder()
.id(UUIDUtils.getInstance().generateShortUuid())
.apiId(apiDO.getId())
.tagId(tagId)
.dateCreated(currentTime)
.dateUpdated(currentTime)
.build()).collect(Collectors.toList());
tagRelationMapper.batchInsert(tags);
}
}
}
return ShenyuResultMessage.UPDATE_SUCCESS;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,12 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

/**
Expand Down Expand Up @@ -91,6 +95,27 @@ public void testCreateOrUpdate() {
testUpdate("123");
}

@Test
public void testUpdateWithEmptyTagIdsClearsRelations() {
ApiDTO apiDTO = buildApiDTO("123");
apiDTO.setTagIds(Collections.emptyList());
when(apiMapper.updateByPrimaryKeySelective(any(ApiDO.class))).thenReturn(1);

assertEquals(ShenyuResultMessage.UPDATE_SUCCESS, apiService.createOrUpdate(apiDTO));
verify(tagRelationMapper).deleteByApiId("123");
verify(tagRelationMapper, never()).batchInsert(anyList());
}

@Test
public void testUpdateWithNullTagIdsKeepsRelations() {
ApiDTO apiDTO = buildApiDTO("123");
when(apiMapper.updateByPrimaryKeySelective(any(ApiDO.class))).thenReturn(1);

assertEquals(ShenyuResultMessage.UPDATE_SUCCESS, apiService.createOrUpdate(apiDTO));
verify(tagRelationMapper, never()).deleteByApiId("123");
verify(tagRelationMapper, never()).batchInsert(anyList());
}

@Test
public void testDelete() {
List<ApiDO> apis = Collections.singletonList(buildApiDO("123"));
Expand Down
Loading