Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ public class TransportManager {

private final Map<String, Transport> transportMap = new HashMap<>();

public Map<String, Transport> getTransportMap() {
return transportMap;
}

public void clearTransportBeforeInit() {
transports.clear();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

import com.google.common.annotations.VisibleForTesting;
import org.apache.http.client.utils.URIBuilder;
import org.apache.servicecomb.foundation.common.event.EnableExceptionPropagation;
import org.apache.servicecomb.foundation.common.event.EventManager;
Expand Down Expand Up @@ -79,6 +80,11 @@ private RegistrationManager() {
initPrimary();
}

@VisibleForTesting
public static void setINSTANCE(RegistrationManager INSTANCE) {
RegistrationManager.INSTANCE = INSTANCE;
}

public MicroserviceInstance getMicroserviceInstance() {
return primary.getMicroserviceInstance();
}
Expand Down
4 changes: 2 additions & 2 deletions inspector/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jmockit</groupId>
<artifactId>jmockit</artifactId>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response.Status;

import com.google.common.annotations.VisibleForTesting;
import org.apache.commons.lang3.StringUtils;
import org.apache.servicecomb.common.rest.resource.ClassPathStaticResourceHandler;
import org.apache.servicecomb.common.rest.resource.StaticResourceHandler;
Expand Down Expand Up @@ -108,6 +109,11 @@ public InspectorImpl setPropertyFactory(PriorityPropertyFactory propertyFactory)
return this;
}

@VisibleForTesting
public Map<String, String> getSchemas() {
return schemas;
}

public InspectorImpl setSchemas(Map<String, String> schemas) {
this.schemas = schemas;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,18 @@

import com.netflix.config.DynamicProperty;

import mockit.Deencapsulation;
import mockit.Expectations;
import mockit.Mocked;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.condition.EnabledForJreRange;
import org.junit.jupiter.api.condition.JRE;
import org.mockito.Mockito;

public class TestInspectorImpl {
static Map<String, String> schemas = new LinkedHashMap<>();

static InspectorImpl inspector;

static RegistrationManager originRegistrationManagerInstance;

@BeforeClass
public static void setup() throws IOException {
ConfigUtil.installDynamicConfig();
Expand All @@ -82,14 +84,15 @@ public static void setup() throws IOException {
.toString(TestInspectorImpl.class.getClassLoader().getResource("schema2.yaml"), StandardCharsets.UTF_8));

inspector = initInspector(null);
originRegistrationManagerInstance = RegistrationManager.INSTANCE;
}

private static InspectorImpl initInspector(String urlPrefix) {
SCBEngine scbEngine = SCBBootstrap.createSCBEngineForTest();
scbEngine.getTransportManager().clearTransportBeforeInit();

if (StringUtils.isNotEmpty(urlPrefix)) {
Map<String, Transport> transportMap = Deencapsulation.getField(scbEngine.getTransportManager(), "transportMap");
Map<String, Transport> transportMap = scbEngine.getTransportManager().getTransportMap();
transportMap.put(RESTFUL, new ServletRestTransport());
ClassLoaderScopeContext.setClassLoaderScopeProperty(DefinitionConst.URL_PREFIX, urlPrefix);
}
Expand Down Expand Up @@ -129,24 +132,22 @@ public void getSchemaIds() {
}

@Test
public void downloadSchemas_default_to_swagger(@Mocked Microservice microservice) throws IOException {
public void downloadSchemas_default_to_swagger() throws IOException {
Microservice microservice = Mockito.mock(Microservice.class);
testDownloadSchemasSwagger(microservice, null);
}

@Test
public void downloadSchemas_swagger(@Mocked Microservice microservice) throws IOException {
public void downloadSchemas_swagger() throws IOException {
Microservice microservice = Mockito.mock(Microservice.class);
testDownloadSchemasSwagger(microservice, SchemaFormat.SWAGGER);
}

private void testDownloadSchemasSwagger(Microservice microservice, SchemaFormat format) throws IOException {
new Expectations(RegistrationManager.INSTANCE) {
{
RegistrationManager.INSTANCE.getMicroservice();
result = microservice;
microservice.getServiceName();
result = "ms";
}
};
RegistrationManager spy = Mockito.spy(RegistrationManager.INSTANCE);
RegistrationManager.setINSTANCE(spy);
Mockito.when(spy.getMicroservice()).thenReturn(microservice);
Mockito.when(microservice.getServiceName()).thenReturn("ms");

Response response = inspector.downloadSchemas(format);
Part part = response.getResult();
Expand All @@ -162,15 +163,13 @@ private void testDownloadSchemasSwagger(Microservice microservice, SchemaFormat
}

@Test
public void downloadSchemas_html(@Mocked Microservice microservice) throws IOException {
new Expectations(RegistrationManager.INSTANCE) {
{
RegistrationManager.INSTANCE.getMicroservice();
result = microservice;
microservice.getServiceName();
result = "ms";
}
};
public void downloadSchemas_html() throws IOException {
Microservice microservice = Mockito.mock(Microservice.class);
RegistrationManager spy = Mockito.spy(RegistrationManager.INSTANCE);
RegistrationManager.setINSTANCE(spy);
Mockito.when(spy.getMicroservice()).thenReturn(microservice);
Mockito.when(microservice.getServiceName()).thenReturn("ms");

Response response = inspector.downloadSchemas(SchemaFormat.HTML);
Part part = response.getResult();
Assertions.assertEquals("ms.html.zip", part.getSubmittedFileName());
Expand All @@ -185,14 +184,10 @@ public void downloadSchemas_html(@Mocked Microservice microservice) throws IOExc
}

@Test
@EnabledForJreRange(min = JRE.JAVA_9)
public void downloadSchemas_failed() {
SchemaFormat format = SchemaFormat.SWAGGER;
new Expectations(format) {
{
format.getSuffix();
result = new RuntimeExceptionWithoutStackTrace("zip failed.");
}
};
SchemaFormat format = Mockito.spy(SchemaFormat.SWAGGER);
Mockito.doThrow(new RuntimeExceptionWithoutStackTrace("zip failed.")).when(format).getSuffix();

try (LogCollector logCollector = new LogCollector()) {
Response response = inspector.downloadSchemas(format);
Expand Down Expand Up @@ -364,9 +359,10 @@ public void priorityProperties() {

@Test
public void urlPrefix() {
RegistrationManager.setINSTANCE(originRegistrationManagerInstance);
InspectorImpl inspector = initInspector("/webroot/rest");

Map<String, String> schemas = Deencapsulation.getField(inspector, "schemas");
Map<String, String> schemas = inspector.getSchemas();
Assertions.assertTrue(schemas.get("schema1").indexOf("/webroot/rest/metrics") > 0);
}
}