use of com.alibaba.nacos.config.server.model.capacity.Capacity in project nacos by alibaba.
the class CapacityServiceTest method testGetCapacity.
@Test
public void testGetCapacity() {
GroupCapacity groupCapacity = new GroupCapacity();
groupCapacity.setId(1L);
when(groupCapacityPersistService.getGroupCapacity(eq("testGroup"))).thenReturn(groupCapacity);
TenantCapacity tenantCapacity = new TenantCapacity();
tenantCapacity.setId(2L);
when(tenantCapacityPersistService.getTenantCapacity(eq("testTenant"))).thenReturn(tenantCapacity);
Capacity resCapacity1 = service.getCapacity("testGroup", null);
Assert.assertEquals(1L, resCapacity1.getId().longValue());
Capacity resCapacity2 = service.getCapacity(null, "testTenant");
Assert.assertEquals(2L, resCapacity2.getId().longValue());
}
use of com.alibaba.nacos.config.server.model.capacity.Capacity in project nacos by alibaba.
the class CapacityControllerTest method testGetCapacity.
@Test
public void testGetCapacity() throws Exception {
Capacity capacity = new Capacity();
capacity.setId(1L);
capacity.setMaxAggrCount(1);
capacity.setMaxSize(1);
capacity.setMaxAggrSize(1);
capacity.setGmtCreate(new Timestamp(1));
capacity.setGmtModified(new Timestamp(2));
when(capacityService.getCapacityWithDefault(eq("test"), eq("test"))).thenReturn(capacity);
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get(Constants.CAPACITY_CONTROLLER_PATH).param("group", "test").param("tenant", "test");
String actualValue = mockMvc.perform(builder).andReturn().getResponse().getContentAsString();
Capacity result = JacksonUtils.toObj(JacksonUtils.toObj(actualValue).get("data").toString(), Capacity.class);
Assert.assertNotNull(result);
Assert.assertEquals(capacity.getId(), result.getId());
Assert.assertEquals(capacity.getMaxAggrCount(), result.getMaxAggrCount());
Assert.assertEquals(capacity.getMaxSize(), result.getMaxSize());
Assert.assertEquals(capacity.getMaxAggrSize(), result.getMaxAggrSize());
Assert.assertEquals(capacity.getGmtCreate(), result.getGmtCreate());
Assert.assertEquals(capacity.getGmtModified(), result.getGmtModified());
}
use of com.alibaba.nacos.config.server.model.capacity.Capacity in project nacos by alibaba.
the class GroupCapacityPersistService method getClusterUsage.
public int getClusterUsage() {
Capacity clusterCapacity = getClusterCapacity();
if (clusterCapacity != null) {
return clusterCapacity.getUsage();
}
String sql = "SELECT count(*) FROM config_info";
Integer result = jdbcTemplate.queryForObject(sql, Integer.class);
if (result == null) {
throw new IllegalArgumentException("configInfoCount error");
}
return result.intValue();
}
use of com.alibaba.nacos.config.server.model.capacity.Capacity in project nacos by alibaba.
the class CapacityController method getCapacity.
@GetMapping
public RestResult<Capacity> getCapacity(HttpServletResponse response, @RequestParam(required = false) String group, @RequestParam(required = false) String tenant) {
if (group == null && tenant == null) {
RestResult<Capacity> restResult = new RestResult<>();
response.setStatus(STATUS400);
restResult.setCode(STATUS400);
restResult.setMessage("The parameter group and tenant cannot be empty at the same time");
return restResult;
}
if (group == null && StringUtils.isBlank(tenant)) {
RestResult<Capacity> restResult = new RestResult<>();
response.setStatus(STATUS400);
restResult.setCode(STATUS400);
restResult.setMessage("tenant cannot be an empty string");
return restResult;
}
RestResult<Capacity> restResult = new RestResult<>();
try {
response.setStatus(STATUS200);
restResult.setCode(STATUS200);
Capacity capacity = capacityService.getCapacityWithDefault(group, tenant);
if (capacity == null) {
LOGGER.warn("[getCapacity] capacity not exist,need init group: {}, tenant: {}", group, tenant);
capacityService.initCapacity(group, tenant);
capacity = capacityService.getCapacityWithDefault(group, tenant);
}
if (capacity != null) {
restResult.setData(capacity);
}
} catch (Exception e) {
LOGGER.error("[getCapacity] ", e);
response.setStatus(STATUS500);
restResult.setCode(STATUS500);
restResult.setMessage(e.getMessage());
}
return restResult;
}
use of com.alibaba.nacos.config.server.model.capacity.Capacity in project nacos by alibaba.
the class CapacityServiceTest method testGetCapacityWithDefault.
@Test
public void testGetCapacityWithDefault() {
TenantCapacity tenantCapacity = new TenantCapacity();
tenantCapacity.setQuota(0);
tenantCapacity.setMaxSize(0);
tenantCapacity.setMaxAggrCount(0);
tenantCapacity.setMaxAggrSize(0);
when(tenantCapacityPersistService.getTenantCapacity(anyString())).thenReturn(tenantCapacity);
GroupCapacity groupCapacity1 = new GroupCapacity();
groupCapacity1.setQuota(0);
groupCapacity1.setMaxSize(0);
groupCapacity1.setMaxAggrCount(0);
groupCapacity1.setMaxAggrSize(0);
when(groupCapacityPersistService.getGroupCapacity(anyString())).thenReturn(groupCapacity1);
// group is null
Capacity resCapacity1 = service.getCapacityWithDefault(null, "testTenant");
Assert.assertEquals(PropertyUtil.getDefaultGroupQuota(), resCapacity1.getQuota().intValue());
Assert.assertEquals(PropertyUtil.getDefaultMaxSize(), resCapacity1.getMaxSize().intValue());
Assert.assertEquals(PropertyUtil.getDefaultMaxAggrCount(), resCapacity1.getMaxAggrCount().intValue());
Assert.assertEquals(PropertyUtil.getDefaultMaxAggrSize(), resCapacity1.getMaxAggrSize().intValue());
// group is GroupCapacityPersistService.CLUSTER
Capacity resCapacity2 = service.getCapacityWithDefault(GroupCapacityPersistService.CLUSTER, null);
Assert.assertEquals(PropertyUtil.getDefaultClusterQuota(), resCapacity2.getQuota().intValue());
Assert.assertEquals(PropertyUtil.getDefaultMaxSize(), resCapacity2.getMaxSize().intValue());
Assert.assertEquals(PropertyUtil.getDefaultMaxAggrCount(), resCapacity2.getMaxAggrCount().intValue());
Assert.assertEquals(PropertyUtil.getDefaultMaxAggrSize(), resCapacity2.getMaxAggrSize().intValue());
GroupCapacity groupCapacity2 = new GroupCapacity();
groupCapacity2.setQuota(0);
groupCapacity2.setMaxSize(0);
groupCapacity2.setMaxAggrCount(0);
groupCapacity2.setMaxAggrSize(0);
when(groupCapacityPersistService.getGroupCapacity(anyString())).thenReturn(groupCapacity2);
// tenant is null
Capacity resCapacity3 = service.getCapacityWithDefault("testGroup", null);
Assert.assertEquals(PropertyUtil.getDefaultGroupQuota(), resCapacity3.getQuota().intValue());
Assert.assertEquals(PropertyUtil.getDefaultMaxSize(), resCapacity3.getMaxSize().intValue());
Assert.assertEquals(PropertyUtil.getDefaultMaxAggrCount(), resCapacity3.getMaxAggrCount().intValue());
Assert.assertEquals(PropertyUtil.getDefaultMaxAggrSize(), resCapacity3.getMaxAggrSize().intValue());
}
Aggregations