Skip to content

Commit

Permalink
fixes #1880 pagination not work (#1891)
Browse files Browse the repository at this point in the history
fixes #1880 pagination not work
  • Loading branch information
kezhenxu94 authored and nobodyiam committed Jan 20, 2019
1 parent f99d54a commit db54e50
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 5 deletions.
Expand Up @@ -35,13 +35,13 @@ List<InstanceConfig> findByConfigAppIdAndConfigClusterNameAndConfigNamespaceName
value = "select b.Id from `InstanceConfig` a inner join `Instance` b on b.Id =" +
" a.`InstanceId` where a.`ConfigAppId` = :configAppId and a.`ConfigClusterName` = " +
":clusterName and a.`ConfigNamespaceName` = :namespaceName and a.`DataChange_LastTime` " +
"> :validDate and b.`AppId` = :instanceAppId and ?#{#pageable.pageSize} > 0",
"> :validDate and b.`AppId` = :instanceAppId",
countQuery = "select count(1) from `InstanceConfig` a inner join `Instance` b on b.id =" +
" a.`InstanceId` where a.`ConfigAppId` = :configAppId and a.`ConfigClusterName` = " +
":clusterName and a.`ConfigNamespaceName` = :namespaceName and a.`DataChange_LastTime` " +
"> :validDate and b.`AppId` = :instanceAppId",
nativeQuery = true)
Page<Object[]> findInstanceIdsByNamespaceAndInstanceAppId(
Page<Object> findInstanceIdsByNamespaceAndInstanceAppId(
@Param("instanceAppId") String instanceAppId, @Param("configAppId") String configAppId,
@Param("clusterName") String clusterName, @Param("namespaceName") String namespaceName,
@Param("validDate") Date validDate, Pageable pageable);
Expand Down
Expand Up @@ -6,6 +6,7 @@
import com.ctrip.framework.apollo.biz.repository.InstanceRepository;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import java.util.Objects;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
Expand Down Expand Up @@ -91,7 +92,7 @@ public Page<Instance> findInstancesByNamespaceAndInstanceAppId(String instanceAp
appId, String clusterName, String
namespaceName, Pageable
pageable) {
Page<Object[]> instanceIdResult = instanceConfigRepository
Page<Object> instanceIdResult = instanceConfigRepository
.findInstanceIdsByNamespaceAndInstanceAppId(instanceAppId, appId, clusterName,
namespaceName, getValidInstanceConfigDate(), pageable);

Expand All @@ -116,7 +117,7 @@ public Page<Instance> findInstancesByNamespaceAndInstanceAppId(String instanceAp
}

return null;
}).filter((Long value) -> value != null).collect(Collectors.toSet());
}).filter(Objects::nonNull).collect(Collectors.toSet());
instances = findInstancesByIds(instanceIds);
}

Expand Down
@@ -0,0 +1,57 @@
package com.ctrip.framework.apollo.biz.repository;

import com.ctrip.framework.apollo.biz.AbstractIntegrationTest;
import com.ctrip.framework.apollo.biz.entity.Instance;
import com.ctrip.framework.apollo.biz.entity.InstanceConfig;
import java.util.Date;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.test.annotation.Rollback;

import static org.hamcrest.Matchers.hasSize;
import static org.junit.Assert.assertThat;

/**
* Created by kezhenxu94 at 2019/1/18 15:33.
*
* @author kezhenxu94 (kezhenxu94 at 163 dot com)
*/
public class InstanceConfigRepositoryTest extends AbstractIntegrationTest {
@Autowired
private InstanceConfigRepository instanceConfigRepository;
@Autowired
private InstanceRepository instanceRepository;

@Rollback
@Test
public void shouldPaginated() {
for (int i = 0; i < 25; i++) {
Instance instance = new Instance();
instance.setAppId("appId");
instanceRepository.save(instance);

final InstanceConfig instanceConfig = new InstanceConfig();
instanceConfig.setConfigAppId("appId");
instanceConfig.setInstanceId(instance.getId());
instanceConfig.setConfigClusterName("cluster");
instanceConfig.setConfigNamespaceName("namespace");
instanceConfigRepository.save(instanceConfig);
}
Page<Object> ids = instanceConfigRepository.findInstanceIdsByNamespaceAndInstanceAppId(
"appId", "appId", "cluster", "namespace", new Date(0), PageRequest.of(0, 10)
);
assertThat(ids.getContent(), hasSize(10));

ids = instanceConfigRepository.findInstanceIdsByNamespaceAndInstanceAppId(
"appId", "appId", "cluster", "namespace", new Date(0), PageRequest.of(1, 10)
);
assertThat(ids.getContent(), hasSize(10));

ids = instanceConfigRepository.findInstanceIdsByNamespaceAndInstanceAppId(
"appId", "appId", "cluster", "namespace", new Date(0), PageRequest.of(2, 10)
);
assertThat(ids.getContent(), hasSize(5));
}
}
1 change: 0 additions & 1 deletion apollo-biz/src/test/resources/application.properties
Expand Up @@ -3,4 +3,3 @@ spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.Ph
spring.jpa.properties.hibernate.show_sql=false
spring.h2.console.enabled = true
spring.h2.console.settings.web-allow-others=true

0 comments on commit db54e50

Please sign in to comment.