Skip to content

Commit

Permalink
fix derby usage filed (#12659)
Browse files Browse the repository at this point in the history
* fix derby usage filed

* code style

* code style

* code style

* code style
  • Loading branch information
shiyiyue1102 authored Sep 18, 2024
1 parent 12f474e commit 91987b0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@

package com.alibaba.nacos.config.server.service;

import com.alibaba.nacos.config.server.model.capacity.TenantCapacity;
import com.alibaba.nacos.config.server.service.capacity.TenantCapacityPersistService;
import com.alibaba.nacos.config.server.constant.PropertiesConstant;
import com.alibaba.nacos.config.server.service.repository.ConfigInfoPersistService;
import com.alibaba.nacos.config.server.utils.PropertyUtil;
import com.alibaba.nacos.core.namespace.injector.AbstractNamespaceDetailInjector;
import com.alibaba.nacos.core.namespace.model.Namespace;
import com.alibaba.nacos.sys.env.EnvUtil;
import org.springframework.stereotype.Service;

/**
Expand All @@ -34,23 +33,17 @@ public class NamespaceConfigInfoService extends AbstractNamespaceDetailInjector

private final ConfigInfoPersistService configInfoPersistService;

private final TenantCapacityPersistService tenantCapacityPersistService;

public NamespaceConfigInfoService(ConfigInfoPersistService configInfoPersistService,
TenantCapacityPersistService tenantCapacityPersistService) {
public NamespaceConfigInfoService(ConfigInfoPersistService configInfoPersistService) {
this.configInfoPersistService = configInfoPersistService;
this.tenantCapacityPersistService = tenantCapacityPersistService;
}

@Override
public void injectDetail(Namespace namespace) {
// set tenant quota
TenantCapacity tenantCapacity = tenantCapacityPersistService.getTenantCapacity(namespace.getNamespace());
if (tenantCapacity != null && tenantCapacity.getQuota() != null && tenantCapacity.getQuota() > 0) {
namespace.setQuota(tenantCapacity.getQuota());
} else {
namespace.setQuota(PropertyUtil.getDefaultTenantQuota());

if (EnvUtil.getProperty(PropertiesConstant.DEFAULT_TENANT_QUOTA, Integer.class) != null) {
namespace.setQuota(EnvUtil.getProperty(PropertiesConstant.DEFAULT_TENANT_QUOTA, Integer.class));
}

// set config count.
int configCount = configInfoPersistService.configInfoCount(namespace.getNamespace());
namespace.setConfigCount(configCount);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@

package com.alibaba.nacos.config.server.service;

import com.alibaba.nacos.config.server.constant.PropertiesConstant;
import com.alibaba.nacos.config.server.model.capacity.TenantCapacity;
import com.alibaba.nacos.config.server.service.capacity.TenantCapacityPersistService;
import com.alibaba.nacos.config.server.service.repository.ConfigInfoPersistService;
import com.alibaba.nacos.config.server.utils.PropertyUtil;
import com.alibaba.nacos.core.namespace.model.Namespace;
import com.alibaba.nacos.sys.env.EnvUtil;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -31,6 +31,7 @@
import org.springframework.test.context.junit.jupiter.SpringExtension;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when;

@ExtendWith(SpringExtension.class)
Expand All @@ -39,14 +40,11 @@ public class NamespaceConfigInfoServiceTest {
@Mock
private ConfigInfoPersistService configInfoPersistService;

@Mock
private TenantCapacityPersistService tenantCapacityPersistService;

MockedStatic<PropertyUtil> propertyUtilMockedStatic;
MockedStatic<EnvUtil> propertyUtilMockedStatic;

@BeforeEach
void setUp() throws Exception {
propertyUtilMockedStatic = Mockito.mockStatic(PropertyUtil.class);
propertyUtilMockedStatic = Mockito.mockStatic(EnvUtil.class);

}

Expand All @@ -59,14 +57,12 @@ void after() throws Exception {
public void testInjectDetailNotDefault() {

String namespaceId = "test1234";
TenantCapacity tenantCapacity = new TenantCapacity();
tenantCapacity.setQuota(1023);

when(tenantCapacityPersistService.getTenantCapacity(namespaceId)).thenReturn(tenantCapacity);
when(EnvUtil.getProperty(eq(PropertiesConstant.DEFAULT_TENANT_QUOTA), eq(Integer.class))).thenReturn(1023);
when(configInfoPersistService.configInfoCount(namespaceId)).thenReturn(101);
Namespace namespace = new Namespace(namespaceId, "test123ShowName");
NamespaceConfigInfoService namespaceConfigInfoService = new NamespaceConfigInfoService(configInfoPersistService,
tenantCapacityPersistService);
namespace.setQuota(200);
NamespaceConfigInfoService namespaceConfigInfoService = new NamespaceConfigInfoService(
configInfoPersistService);
namespaceConfigInfoService.injectDetail(namespace);
assertEquals(101, namespace.getConfigCount());
assertEquals(1023, namespace.getQuota());
Expand All @@ -79,17 +75,17 @@ public void testInjectDetailDefaultQuota() {
String namespaceId = "test1234";
TenantCapacity tenantCapacity = new TenantCapacity();
tenantCapacity.setQuota(0);
when(tenantCapacityPersistService.getTenantCapacity(namespaceId)).thenReturn(tenantCapacity);
when(configInfoPersistService.configInfoCount(namespaceId)).thenReturn(105);

when(PropertyUtil.getDefaultTenantQuota()).thenReturn(1025);
when(EnvUtil.getProperty(eq(PropertiesConstant.DEFAULT_TENANT_QUOTA), eq(Integer.class))).thenReturn(null);
Namespace namespace = new Namespace(namespaceId, "test123ShowName");
NamespaceConfigInfoService namespaceConfigInfoService = new NamespaceConfigInfoService(configInfoPersistService,
tenantCapacityPersistService);
namespace.setQuota(200);
NamespaceConfigInfoService namespaceConfigInfoService = new NamespaceConfigInfoService(
configInfoPersistService);
namespaceConfigInfoService.injectDetail(namespace);

assertEquals(105, namespace.getConfigCount());
assertEquals(1025, namespace.getQuota());
assertEquals(200, namespace.getQuota());
}

}

0 comments on commit 91987b0

Please sign in to comment.