This is an automated email from the ASF dual-hosted git repository.

shenlin pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/rocketmq-eventbridge.git


The following commit(s) were added to refs/heads/main by this push:
     new 83ca4d2  Add quota service (#55)
83ca4d2 is described below

commit 83ca4d2f62705784bdc251db893a271fc084b1de
Author: zhaohai <[email protected]>
AuthorDate: Thu Mar 23 11:15:51 2023 +0800

    Add quota service (#55)
    
    add quota service
---
 .../adapter/rpc/impl/quota/QuotaServiceImpl.java   | 38 ++++++++++++++++++++++
 .../domain/common/enums/TotalQuotaEnum.java        | 29 +++++++++++++++++
 .../apidestination/ApiDestinationService.java      |  9 +++--
 .../domain/model/connection/ConnectionService.java | 11 +++++--
 .../domain/model/quota/QuotaService.java           | 30 +++++++++++++++++
 .../domain/service/ApiDestinationServiceTest.java  |  4 +++
 .../domain/service/ConnectionServiceTest.java      |  8 +++--
 7 files changed, 122 insertions(+), 7 deletions(-)

diff --git 
a/adapter/rpc/src/main/java/org/apache/rocketmq/eventbridge/adapter/rpc/impl/quota/QuotaServiceImpl.java
 
b/adapter/rpc/src/main/java/org/apache/rocketmq/eventbridge/adapter/rpc/impl/quota/QuotaServiceImpl.java
new file mode 100644
index 0000000..8c77d57
--- /dev/null
+++ 
b/adapter/rpc/src/main/java/org/apache/rocketmq/eventbridge/adapter/rpc/impl/quota/QuotaServiceImpl.java
@@ -0,0 +1,38 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.rocketmq.eventbridge.adapter.rpc.impl.quota;
+
+import org.apache.rocketmq.eventbridge.domain.common.EventBridgeConstants;
+import org.apache.rocketmq.eventbridge.domain.common.enums.TotalQuotaEnum;
+import org.apache.rocketmq.eventbridge.domain.model.quota.QuotaService;
+import org.springframework.stereotype.Service;
+
+@Service
+public class QuotaServiceImpl implements QuotaService {
+
+    @Override
+    public Integer getTotalQuota(String accountId, TotalQuotaEnum 
totalQuotaEnum) {
+        if 
(TotalQuotaEnum.API_DESTINATION_COUNT.name().equals(totalQuotaEnum.name())) {
+            return EventBridgeConstants.API_DESTINATION_COUNT_LIMIT;
+        }
+        if 
(TotalQuotaEnum.CONNECTION_COUNT.name().equals(totalQuotaEnum.name())) {
+            return EventBridgeConstants.CONNECTION_COUNT_LIMIT;
+        }
+        return null;
+    }
+}
diff --git 
a/domain/src/main/java/org/apache/rocketmq/eventbridge/domain/common/enums/TotalQuotaEnum.java
 
b/domain/src/main/java/org/apache/rocketmq/eventbridge/domain/common/enums/TotalQuotaEnum.java
new file mode 100644
index 0000000..2760724
--- /dev/null
+++ 
b/domain/src/main/java/org/apache/rocketmq/eventbridge/domain/common/enums/TotalQuotaEnum.java
@@ -0,0 +1,29 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.rocketmq.eventbridge.domain.common.enums;
+
+public enum TotalQuotaEnum {
+    /**
+     * API DESTINATION COUNT
+     */
+    API_DESTINATION_COUNT,
+    /**
+     * CONNECTION COUNT
+     */
+    CONNECTION_COUNT;
+}
diff --git 
a/domain/src/main/java/org/apache/rocketmq/eventbridge/domain/model/apidestination/ApiDestinationService.java
 
b/domain/src/main/java/org/apache/rocketmq/eventbridge/domain/model/apidestination/ApiDestinationService.java
index 874546f..19fad7f 100644
--- 
a/domain/src/main/java/org/apache/rocketmq/eventbridge/domain/model/apidestination/ApiDestinationService.java
+++ 
b/domain/src/main/java/org/apache/rocketmq/eventbridge/domain/model/apidestination/ApiDestinationService.java
@@ -18,11 +18,13 @@
 package org.apache.rocketmq.eventbridge.domain.model.apidestination;
 
 import lombok.extern.slf4j.Slf4j;
+import org.apache.rocketmq.eventbridge.domain.common.enums.TotalQuotaEnum;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.rocketmq.eventbridge.domain.common.EventBridgeConstants;
 import 
org.apache.rocketmq.eventbridge.domain.common.exception.EventBridgeErrorCode;
 import org.apache.rocketmq.eventbridge.domain.model.AbstractResourceService;
 import org.apache.rocketmq.eventbridge.domain.model.PaginationResult;
+import org.apache.rocketmq.eventbridge.domain.model.quota.QuotaService;
 import 
org.apache.rocketmq.eventbridge.domain.model.apidestination.parameter.HttpApiParameters;
 import 
org.apache.rocketmq.eventbridge.domain.repository.ApiDestinationRepository;
 import org.apache.rocketmq.eventbridge.exception.EventBridgeException;
@@ -40,8 +42,11 @@ public class ApiDestinationService extends 
AbstractResourceService {
 
     private final ApiDestinationRepository apiDestinationRepository;
 
-    public ApiDestinationService(ApiDestinationRepository 
apiDestinationRepository) {
+    private final QuotaService quotaService;
+
+    public ApiDestinationService(ApiDestinationRepository 
apiDestinationRepository, QuotaService quotaService) {
         this.apiDestinationRepository = apiDestinationRepository;
+        this.quotaService = quotaService;
     }
 
     @Transactional(rollbackFor = Exception.class, propagation = 
Propagation.REQUIRED)
@@ -49,7 +54,7 @@ public class ApiDestinationService extends 
AbstractResourceService {
         if (checkApiDestination(eventApiDestinationDTO.getAccountId(), 
eventApiDestinationDTO.getName()) != null) {
             throw new 
EventBridgeException(EventBridgeErrorCode.ApiDestinationAlreadyExist, 
eventApiDestinationDTO.getName());
         }
-        
super.checkQuota(this.getApiDestinationCount(eventApiDestinationDTO.getAccountId()),
 EventBridgeConstants.API_DESTINATION_COUNT_LIMIT,
+        
super.checkQuota(this.getApiDestinationCount(eventApiDestinationDTO.getAccountId()),
 quotaService.getTotalQuota(eventApiDestinationDTO.getAccountId(), 
TotalQuotaEnum.API_DESTINATION_COUNT),
                 ApiDestinationCountExceedLimit);
         checkHttpApiParameters(eventApiDestinationDTO.getApiParams());
         final Boolean apiDestination = 
apiDestinationRepository.createApiDestination(eventApiDestinationDTO);
diff --git 
a/domain/src/main/java/org/apache/rocketmq/eventbridge/domain/model/connection/ConnectionService.java
 
b/domain/src/main/java/org/apache/rocketmq/eventbridge/domain/model/connection/ConnectionService.java
index 369694c..54aae47 100644
--- 
a/domain/src/main/java/org/apache/rocketmq/eventbridge/domain/model/connection/ConnectionService.java
+++ 
b/domain/src/main/java/org/apache/rocketmq/eventbridge/domain/model/connection/ConnectionService.java
@@ -26,6 +26,7 @@ import org.apache.commons.lang3.StringUtils;
 import org.apache.rocketmq.eventbridge.domain.common.EventBridgeConstants;
 import 
org.apache.rocketmq.eventbridge.domain.common.enums.AuthorizationTypeEnum;
 import org.apache.rocketmq.eventbridge.domain.common.enums.NetworkTypeEnum;
+import org.apache.rocketmq.eventbridge.domain.common.enums.TotalQuotaEnum;
 import 
org.apache.rocketmq.eventbridge.domain.common.exception.EventBridgeErrorCode;
 import org.apache.rocketmq.eventbridge.domain.model.AbstractResourceService;
 import org.apache.rocketmq.eventbridge.domain.model.PaginationResult;
@@ -35,6 +36,7 @@ import 
org.apache.rocketmq.eventbridge.domain.model.connection.parameter.BasicAu
 import 
org.apache.rocketmq.eventbridge.domain.model.connection.parameter.NetworkParameters;
 import 
org.apache.rocketmq.eventbridge.domain.model.connection.parameter.OAuthHttpParameters;
 import 
org.apache.rocketmq.eventbridge.domain.model.connection.parameter.OAuthParameters;
+import org.apache.rocketmq.eventbridge.domain.model.quota.QuotaService;
 import 
org.apache.rocketmq.eventbridge.domain.repository.ApiDestinationRepository;
 import org.apache.rocketmq.eventbridge.domain.repository.ConnectionRepository;
 import org.apache.rocketmq.eventbridge.domain.rpc.NetworkServiceAPI;
@@ -57,13 +59,16 @@ public class ConnectionService extends 
AbstractResourceService {
 
     protected ApiDestinationRepository apiDestinationRepository;
 
+    protected QuotaService quotaService;
+
     public ConnectionService(ConnectionRepository connectionRepository,
-                             SecretManagerAPI secretManagerAPI, 
NetworkServiceAPI networkServiceAPI,
-                             ApiDestinationRepository 
apiDestinationRepository) {
+        SecretManagerAPI secretManagerAPI, NetworkServiceAPI networkServiceAPI,
+        ApiDestinationRepository apiDestinationRepository, QuotaService 
quotaService) {
         this.connectionRepository = connectionRepository;
         this.secretManagerAPI = secretManagerAPI;
         this.networkServiceAPI = networkServiceAPI;
         this.apiDestinationRepository = apiDestinationRepository;
+        this.quotaService = quotaService;
     }
 
     @Transactional(rollbackFor = Exception.class, propagation = 
Propagation.REQUIRED)
@@ -71,7 +76,7 @@ public class ConnectionService extends 
AbstractResourceService {
         if 
(!CollectionUtils.isEmpty(checkConnection(connectionDTO.getAccountId(), 
connectionDTO.getConnectionName()))) {
             throw new 
EventBridgeException(EventBridgeErrorCode.ConnectionAlreadyExist, 
connectionDTO.getConnectionName());
         }
-        
super.checkQuota(this.getConnectionCount(connectionDTO.getAccountId()), 
EventBridgeConstants.CONNECTION_COUNT_LIMIT, ConnectionCountExceedLimit);
+        
super.checkQuota(this.getConnectionCount(connectionDTO.getAccountId()), 
quotaService.getTotalQuota(connectionDTO.getAccountId(), 
TotalQuotaEnum.CONNECTION_COUNT), ConnectionCountExceedLimit);
         checkNetworkType(connectionDTO.getNetworkParameters());
         if (connectionDTO.getAuthParameters() != null) {
             checkAuthParameters(connectionDTO.getAuthParameters());
diff --git 
a/domain/src/main/java/org/apache/rocketmq/eventbridge/domain/model/quota/QuotaService.java
 
b/domain/src/main/java/org/apache/rocketmq/eventbridge/domain/model/quota/QuotaService.java
new file mode 100644
index 0000000..8f5fcf5
--- /dev/null
+++ 
b/domain/src/main/java/org/apache/rocketmq/eventbridge/domain/model/quota/QuotaService.java
@@ -0,0 +1,30 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.rocketmq.eventbridge.domain.model.quota;
+
+import org.apache.rocketmq.eventbridge.domain.common.enums.TotalQuotaEnum;
+
+public interface QuotaService {
+
+    /**
+     * Return the totalQuotaEnum match total quota.
+     * @param accountId
+     * @return
+     */
+    Integer getTotalQuota(String accountId, TotalQuotaEnum totalQuotaEnum);
+}
diff --git 
a/domain/src/test/java/org/apache/rocketmq/eventbridge/domain/service/ApiDestinationServiceTest.java
 
b/domain/src/test/java/org/apache/rocketmq/eventbridge/domain/service/ApiDestinationServiceTest.java
index ce9cc21..22918cc 100644
--- 
a/domain/src/test/java/org/apache/rocketmq/eventbridge/domain/service/ApiDestinationServiceTest.java
+++ 
b/domain/src/test/java/org/apache/rocketmq/eventbridge/domain/service/ApiDestinationServiceTest.java
@@ -23,6 +23,7 @@ import java.util.UUID;
 import org.apache.rocketmq.eventbridge.domain.model.PaginationResult;
 import 
org.apache.rocketmq.eventbridge.domain.model.apidestination.ApiDestinationDTO;
 import 
org.apache.rocketmq.eventbridge.domain.model.apidestination.ApiDestinationService;
+import org.apache.rocketmq.eventbridge.domain.model.quota.QuotaService;
 import 
org.apache.rocketmq.eventbridge.domain.repository.ApiDestinationRepository;
 import org.junit.Assert;
 import org.junit.Before;
@@ -43,6 +44,8 @@ public class ApiDestinationServiceTest {
     private ApiDestinationService apiDestinationService;
     @Mock
     private ApiDestinationRepository apiDestinationRepository;
+    @Mock
+    private QuotaService quotaService;
 
     @Before
     public void testBefore() {
@@ -53,6 +56,7 @@ public class ApiDestinationServiceTest {
         apiDestinationDTO.setName(UUID.randomUUID().toString());
         
Mockito.when(apiDestinationRepository.getApiDestinationCount(any())).thenReturn(8);
         Mockito.when(apiDestinationRepository.listApiDestinations(any(), 
any(), any(), anyInt())).thenReturn(new ArrayList<>());
+        Mockito.when(quotaService.getTotalQuota(any(), any())).thenReturn(10);
     }
 
     @Test
diff --git 
a/domain/src/test/java/org/apache/rocketmq/eventbridge/domain/service/ConnectionServiceTest.java
 
b/domain/src/test/java/org/apache/rocketmq/eventbridge/domain/service/ConnectionServiceTest.java
index 57ffa39..f28017f 100644
--- 
a/domain/src/test/java/org/apache/rocketmq/eventbridge/domain/service/ConnectionServiceTest.java
+++ 
b/domain/src/test/java/org/apache/rocketmq/eventbridge/domain/service/ConnectionServiceTest.java
@@ -29,6 +29,7 @@ import 
org.apache.rocketmq.eventbridge.domain.model.connection.ConnectionService
 import 
org.apache.rocketmq.eventbridge.domain.model.connection.parameter.AuthParameters;
 import 
org.apache.rocketmq.eventbridge.domain.model.connection.parameter.BasicAuthParameters;
 import 
org.apache.rocketmq.eventbridge.domain.model.connection.parameter.NetworkParameters;
+import org.apache.rocketmq.eventbridge.domain.model.quota.QuotaService;
 import 
org.apache.rocketmq.eventbridge.domain.repository.ApiDestinationRepository;
 import org.apache.rocketmq.eventbridge.domain.repository.ConnectionRepository;
 import org.apache.rocketmq.eventbridge.domain.rpc.NetworkServiceAPI;
@@ -59,6 +60,8 @@ public class ConnectionServiceTest {
     private NetworkServiceAPI networkServiceAPI;
     @Mock
     private ApiDestinationRepository apiDestinationRepository;
+    @Mock
+    private QuotaService quotaService;
 
     @Before
     public void testBefore() throws Exception {
@@ -73,6 +76,7 @@ public class ConnectionServiceTest {
         List<ConnectionDTO> connectionDTOS = Lists.newArrayList();
         connectionDTOS.add(connectionDTO);
         Mockito.when(connectionRepository.getConnection(any(), 
any())).thenReturn(connectionDTOS);
+        Mockito.when(quotaService.getTotalQuota(any(), any())).thenReturn(10);
     }
 
     @Test
@@ -107,8 +111,6 @@ public class ConnectionServiceTest {
 
     @Test
     public void testUpdateConnection() {
-        
Mockito.when(connectionRepository.updateConnection(any())).thenReturn(Boolean.TRUE);
-        
Mockito.when(connectionRepository.getConnectionByName(anyString())).thenReturn(new
 ConnectionDTO());
         ConnectionDTO connectionDTO = new ConnectionDTO();
         connectionDTO.setConnectionName(UUID.randomUUID().toString());
         NetworkParameters networkParameters = new NetworkParameters();
@@ -124,6 +126,8 @@ public class ConnectionServiceTest {
         authParameters.setBasicAuthParameters(basicAuthParameters);
         
authParameters.setAuthorizationType(AuthorizationTypeEnum.BASIC_AUTH.getType());
         connectionDTO.setAuthParameters(authParameters);
+        
Mockito.when(connectionRepository.updateConnection(any())).thenReturn(Boolean.TRUE);
+        
Mockito.when(connectionRepository.getConnectionByNameAccountId(anyString(), 
anyString())).thenReturn(connectionDTO);
         connectionService.updateConnection(connectionDTO, 
UUID.randomUUID().toString());
     }
 

Reply via email to