Skip to content

Commit

Permalink
[SCB-2813]refactoring configuration extension because can not using C…
Browse files Browse the repository at this point in the history
…onfigurationProperties before configuration setup (#3976)
  • Loading branch information
liubao68 authored Oct 18, 2023
1 parent 793b1c1 commit 06b2b5f
Show file tree
Hide file tree
Showing 67 changed files with 472 additions and 757 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.apache.servicecomb.common.rest.locator.OperationLocator;
import org.apache.servicecomb.common.rest.locator.ServicePathManager;
import org.apache.servicecomb.common.rest.locator.TestPathSchema;
import org.apache.servicecomb.config.BootStrapProperties;
import org.apache.servicecomb.core.BootListener;
import org.apache.servicecomb.core.SCBEngine;
import org.apache.servicecomb.core.bootstrap.SCBBootstrap;
Expand Down Expand Up @@ -59,6 +60,13 @@ public static void setup() {
.thenReturn(false);
Mockito.when(environment.getProperty(CFG_KEY_TURN_DOWN_STATUS_WAIT_SEC,
long.class, DEFAULT_TURN_DOWN_STATUS_WAIT_SEC)).thenReturn(DEFAULT_TURN_DOWN_STATUS_WAIT_SEC);
Mockito.when(environment.getProperty(BootStrapProperties.CONFIG_SERVICE_APPLICATION))
.thenReturn(BootStrapProperties.DEFAULT_APPLICATION);
Mockito.when(environment.getProperty(BootStrapProperties.CONFIG_SERVICE_NAME))
.thenReturn(BootStrapProperties.DEFAULT_MICROSERVICE_NAME);
Mockito.when(environment.getProperty(BootStrapProperties.CONFIG_SERVICE_ENVIRONMENT))
.thenReturn(BootStrapProperties.DEFAULT_MICROSERVICE_ENVIRONMENT);

List<BootListener> listeners = new ArrayList<>();
listeners.add(new RestEngineSchemaListener());
scbEngine.setBootListeners(listeners);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.List;

import org.apache.servicecomb.common.rest.RestEngineSchemaListener;
import org.apache.servicecomb.config.BootStrapProperties;
import org.apache.servicecomb.core.BootListener;
import org.apache.servicecomb.core.SCBEngine;
import org.apache.servicecomb.core.bootstrap.SCBBootstrap;
Expand All @@ -35,8 +36,8 @@
import org.apache.servicecomb.core.executor.ExecutorManager;
import org.apache.servicecomb.core.transport.TransportManager;
import org.hamcrest.MatcherAssert;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.core.env.Environment;
Expand Down Expand Up @@ -156,25 +157,33 @@ public void formWithView(@FormParam("form") String form) {
}
}

static SCBEngine scbEngine;
SCBEngine scbEngine;

static OpenAPI swagger;
OpenAPI swagger;

OperationMeta meta;

RestOperationMeta operationMeta;

@BeforeAll
public static void classSetup() {
@BeforeEach
public void setUp() {
Environment environment = Mockito.mock(Environment.class);
scbEngine = SCBBootstrap.createSCBEngineForTest(environment);
ExecutorManager executorManager = Mockito.mock(ExecutorManager.class);
TransportManager transportManager = Mockito.mock(TransportManager.class);
scbEngine.setTransportManager(transportManager);
scbEngine.setExecutorManager(executorManager);
scbEngine.setEnvironment(environment);

Mockito.when(environment.getProperty("servicecomb.rest.parameter.decodeAsObject", boolean.class, false))
.thenReturn(false);
Mockito.when(environment.getProperty(CFG_KEY_TURN_DOWN_STATUS_WAIT_SEC,
long.class, DEFAULT_TURN_DOWN_STATUS_WAIT_SEC)).thenReturn(DEFAULT_TURN_DOWN_STATUS_WAIT_SEC);
Mockito.when(environment.getProperty(BootStrapProperties.CONFIG_SERVICE_APPLICATION))
.thenReturn(BootStrapProperties.DEFAULT_APPLICATION);
Mockito.when(environment.getProperty(BootStrapProperties.CONFIG_SERVICE_NAME))
.thenReturn("test");
Mockito.when(environment.getProperty(BootStrapProperties.CONFIG_SERVICE_ENVIRONMENT))
.thenReturn(BootStrapProperties.DEFAULT_MICROSERVICE_ENVIRONMENT);

List<BootListener> listeners = new ArrayList<>();
listeners.add(new RestEngineSchemaListener());
Expand All @@ -185,8 +194,8 @@ public static void classSetup() {
swagger = Mockito.spy(scbEngine.getProducerMicroserviceMeta().ensureFindSchemaMeta("sid1").getSwagger());
}

@AfterAll
public static void classTeardown() {
@AfterEach
public void teardown() {
scbEngine.destroy();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,31 +25,36 @@

import org.apache.servicecomb.common.rest.RestEngineSchemaListener;
import org.apache.servicecomb.common.rest.definition.RestOperationMeta;
import org.apache.servicecomb.config.BootStrapProperties;
import org.apache.servicecomb.core.BootListener;
import org.apache.servicecomb.core.SCBEngine;
import org.apache.servicecomb.core.bootstrap.SCBBootstrap;
import org.apache.servicecomb.core.executor.ExecutorManager;
import org.apache.servicecomb.core.transport.TransportManager;
import org.apache.servicecomb.foundation.common.LegacyPropertyFactory;
import org.apache.servicecomb.foundation.common.exceptions.ServiceCombException;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.core.env.Environment;

public class TestMicroservicePaths {
static SCBEngine scbEngine;
SCBEngine scbEngine;

static MicroservicePaths paths;
MicroservicePaths paths;

@BeforeAll
public static void setup() {
@BeforeEach
public void setup() {
Environment environment = Mockito.mock(Environment.class);
scbEngine = SCBBootstrap.createSCBEngineForTest(environment);
LegacyPropertyFactory.setEnvironment(environment);
scbEngine.setEnvironment(environment);

Mockito.when(environment.getProperty(BootStrapProperties.CONFIG_SERVICE_APPLICATION))
.thenReturn(BootStrapProperties.DEFAULT_APPLICATION);
Mockito.when(environment.getProperty(BootStrapProperties.CONFIG_SERVICE_NAME))
.thenReturn(BootStrapProperties.DEFAULT_MICROSERVICE_NAME);
Mockito.when(environment.getProperty(BootStrapProperties.CONFIG_SERVICE_ENVIRONMENT))
.thenReturn(BootStrapProperties.DEFAULT_MICROSERVICE_ENVIRONMENT);
Mockito.when(environment.getProperty(CFG_KEY_TURN_DOWN_STATUS_WAIT_SEC,
long.class, DEFAULT_TURN_DOWN_STATUS_WAIT_SEC)).thenReturn(DEFAULT_TURN_DOWN_STATUS_WAIT_SEC);
Mockito.when(environment.getProperty("servicecomb.rest.parameter.decodeAsObject", boolean.class, false))
Expand All @@ -68,8 +73,8 @@ public static void setup() {
paths = spm.producerPaths;
}

@AfterAll
public static void teardown() {
@AfterEach
public void teardown() {
scbEngine.destroy();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,37 +24,43 @@
import java.util.List;

import org.apache.servicecomb.common.rest.RestEngineSchemaListener;
import org.apache.servicecomb.config.BootStrapProperties;
import org.apache.servicecomb.core.BootListener;
import org.apache.servicecomb.core.SCBEngine;
import org.apache.servicecomb.core.bootstrap.SCBBootstrap;
import org.apache.servicecomb.core.executor.ExecutorManager;
import org.apache.servicecomb.core.transport.TransportManager;
import org.apache.servicecomb.foundation.common.LegacyPropertyFactory;
import org.apache.servicecomb.foundation.common.utils.ClassLoaderScopeContext;
import org.apache.servicecomb.registry.definition.DefinitionConst;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.core.env.Environment;

public class TestServicePathManager {
static Environment environment = Mockito.mock(Environment.class);

@BeforeAll
public static void beforeClass() {
LegacyPropertyFactory.setEnvironment(environment);
Mockito.when(environment.getProperty("servicecomb.rest.parameter.decodeAsObject", boolean.class, false))
.thenReturn(false);
}

SCBEngine scbEngine;

Environment environment;

@BeforeEach
public void setUp() {
environment = Mockito.mock(Environment.class);
scbEngine = SCBBootstrap.createSCBEngineForTest(environment);

Mockito.when(environment.getProperty("servicecomb.rest.parameter.decodeAsObject", boolean.class, false))
.thenReturn(false);
Mockito.when(environment.getProperty("servicecomb.rest.parameter.decodeAsObject", boolean.class, false))
.thenReturn(false);
Mockito.when(environment.getProperty(CFG_KEY_TURN_DOWN_STATUS_WAIT_SEC,
long.class, DEFAULT_TURN_DOWN_STATUS_WAIT_SEC)).thenReturn(DEFAULT_TURN_DOWN_STATUS_WAIT_SEC);
Mockito.when(environment.getProperty(BootStrapProperties.CONFIG_SERVICE_APPLICATION))
.thenReturn(BootStrapProperties.DEFAULT_APPLICATION);
Mockito.when(environment.getProperty(BootStrapProperties.CONFIG_SERVICE_NAME))
.thenReturn(BootStrapProperties.DEFAULT_MICROSERVICE_NAME);
Mockito.when(environment.getProperty(BootStrapProperties.CONFIG_SERVICE_ENVIRONMENT))
.thenReturn(BootStrapProperties.DEFAULT_MICROSERVICE_ENVIRONMENT);
}

@AfterEach
Expand All @@ -65,11 +71,6 @@ public void tearDown() {

@Test
public void testBuildProducerPathsNoPrefix() {
Environment environment = Mockito.mock(Environment.class);
scbEngine = SCBBootstrap.createSCBEngineForTest(environment);
scbEngine.setEnvironment(environment);
Mockito.when(environment.getProperty(CFG_KEY_TURN_DOWN_STATUS_WAIT_SEC,
long.class, DEFAULT_TURN_DOWN_STATUS_WAIT_SEC)).thenReturn(DEFAULT_TURN_DOWN_STATUS_WAIT_SEC);
ExecutorManager executorManager = Mockito.mock(ExecutorManager.class);
TransportManager transportManager = Mockito.mock(TransportManager.class);
scbEngine.setTransportManager(transportManager);
Expand All @@ -90,9 +91,7 @@ public void testBuildProducerPathsNoPrefix() {
@Test
public void testBuildProducerPathsHasPrefix() {
ClassLoaderScopeContext.setClassLoaderScopeProperty(DefinitionConst.URL_PREFIX, "/root/rest");
Environment environment = Mockito.mock(Environment.class);
scbEngine = SCBBootstrap.createSCBEngineForTest(environment);
scbEngine.setEnvironment(environment);

Mockito.when(environment.getProperty(CFG_KEY_TURN_DOWN_STATUS_WAIT_SEC,
long.class, DEFAULT_TURN_DOWN_STATUS_WAIT_SEC)).thenReturn(DEFAULT_TURN_DOWN_STATUS_WAIT_SEC);
Mockito.when(environment.getProperty(DefinitionConst.REGISTER_URL_PREFIX, boolean.class, false)).thenReturn(false);
Expand Down

This file was deleted.

21 changes: 5 additions & 16 deletions core/src/main/java/org/apache/servicecomb/core/SCBEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import java.util.concurrent.atomic.AtomicLong;

import org.apache.commons.lang3.StringUtils;
import org.apache.servicecomb.config.MicroserviceProperties;
import org.apache.servicecomb.config.BootStrapProperties;
import org.apache.servicecomb.config.priority.PriorityPropertyManager;
import org.apache.servicecomb.core.BootListener.BootEvent;
import org.apache.servicecomb.core.BootListener.EventType;
Expand Down Expand Up @@ -112,8 +112,6 @@ public class SCBEngine {

private RegistrationManager registrationManager;

private MicroserviceProperties microserviceProperties;

private DiscoveryManager discoveryManager;

private Environment environment;
Expand Down Expand Up @@ -182,11 +180,6 @@ public void setSwaggerLoader(SwaggerLoader swaggerLoader) {
this.swaggerLoader = swaggerLoader;
}

@Autowired
public void setMicroserviceProperties(MicroserviceProperties microserviceProperties) {
this.microserviceProperties = microserviceProperties;
}

@Autowired
public void setExecutorManager(ExecutorManager executorManager) {
this.executorManager = executorManager;
Expand All @@ -206,11 +199,7 @@ public ApplicationContext getApplicationContext() {
}

public String getAppId() {
return this.microserviceProperties.getApplication();
}

public MicroserviceProperties getMicroserviceProperties() {
return this.microserviceProperties;
return BootStrapProperties.readApplication(environment);
}

public void setStatus(SCBStatus status) {
Expand Down Expand Up @@ -398,11 +387,11 @@ private void doRun() throws Exception {
}

private void createProducerMicroserviceMeta() {
String microserviceName = this.microserviceProperties.getName();
String microserviceName = BootStrapProperties.readServiceName(environment);
producerMicroserviceMeta = new MicroserviceMeta(this,
this.microserviceProperties.getApplication(), microserviceName, false);
BootStrapProperties.readApplication(environment), microserviceName, false);
producerMicroserviceMeta.setFilterChain(filterChainsManager.findProducerChain(
this.microserviceProperties.getApplication(), microserviceName));
BootStrapProperties.readApplication(environment), microserviceName));
producerMicroserviceMeta.setMicroserviceVersionsMeta(new MicroserviceVersionsMeta(this));
}

Expand Down
Loading

0 comments on commit 06b2b5f

Please sign in to comment.