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 @@ -16,30 +16,26 @@

package io.servicecomb.common.rest.definition;

import io.servicecomb.common.rest.codec.produce.ProduceProcessor;
import io.servicecomb.common.rest.codec.produce.ProduceProcessorManager;
import io.servicecomb.common.rest.definition.path.PathRegExp;
import io.servicecomb.common.rest.definition.path.URLPathBuilder;
import io.servicecomb.core.definition.OperationMeta;
import io.swagger.models.Operation;
import io.swagger.models.Swagger;
import io.swagger.models.parameters.Parameter;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.ws.rs.core.MediaType;

import io.servicecomb.common.rest.codec.produce.ProduceProcessor;
import io.servicecomb.common.rest.codec.produce.ProduceProcessorManager;
import io.servicecomb.common.rest.definition.path.URLPathBuilder;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.servicecomb.common.rest.definition.path.PathRegExp;
import io.servicecomb.core.definition.OperationMeta;

import io.swagger.models.Operation;
import io.swagger.models.Swagger;
import io.swagger.models.parameters.Parameter;

public class RestOperationMeta {
private static final Logger LOGGER = LoggerFactory.getLogger(RestOperationMeta.class);

Expand Down Expand Up @@ -109,31 +105,15 @@ public void setOperationMeta(OperationMeta operationMeta) {

// 输出b/c/形式的url
private String concatPath(String basePath, String operationPath) {
basePath = removeHeadTailSlash(basePath);
operationPath = removeHeadTailSlash(operationPath);

if (StringUtils.isEmpty(operationPath)) {
return basePath + "/";
}

return basePath + "/" + operationPath + "/";
return ("/" + nonNullify(basePath) + "/" + nonNullify(operationPath) + "/")
.replaceAll("/{2,}", "/");
}

protected String removeHeadTailSlash(String path) {
if (path == null) {
path = "";
}

if (path.startsWith("/")) {
path = path.substring(1);
}
if (path.endsWith("/")) {
path = path.substring(0, path.length() - 1);
}
return path;
private String nonNullify(String path) {
return path == null ? "" : path;
}

public String getAbsolutePath() {
public String getAbsolutePath() {
return this.absolutePath;
}

Expand Down Expand Up @@ -202,7 +182,7 @@ public List<RestParam> getParamList() {
return paramList;
}

public void addParam(RestParam param) {
private void addParam(RestParam param) {
paramList.add(param);
paramMap.put(param.getParamName(), param);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,12 @@

package io.servicecomb.common.rest.locator;

import io.servicecomb.common.rest.definition.RestOperationMeta;
import io.servicecomb.swagger.invocation.exception.InvocationException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import javax.ws.rs.core.Response.Status;

import io.servicecomb.common.rest.definition.RestOperationMeta;
import io.servicecomb.swagger.invocation.exception.InvocationException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -104,17 +101,11 @@ protected RestOperationMeta locateDynamicPathOperation(String path, Collection<R
}

protected boolean checkHttpMethod(RestOperationMeta operation, String httpMethod) {
if (operation.getHttpMethod().equals(httpMethod)) {
return true;
}
return false;
return operation.getHttpMethod().equals(httpMethod);
}

// Path: /a/b/c -> a/b/c/
public static String getStandardPath(String path) {
if (path.startsWith(SLASH)) {
path = path.substring(1);
}
// Path: /a/b/c -> /a/b/c/
static String getStandardPath(String path) {
if (path.length() > 0 && !path.endsWith(SLASH)) {
path += SLASH;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

package io.servicecomb.common.rest.locator;

import io.servicecomb.common.rest.definition.RestOperationComparator;
import io.servicecomb.common.rest.definition.RestOperationMeta;
import io.servicecomb.core.definition.MicroserviceMeta;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
Expand All @@ -24,14 +27,9 @@
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import io.servicecomb.common.rest.definition.RestOperationComparator;
import io.servicecomb.common.rest.definition.RestOperationMeta;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.servicecomb.core.definition.MicroserviceMeta;

/**
* 对静态路径和动态路径的operation进行预先处理,加速operation的查询定位
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,6 @@

package io.servicecomb.common.rest;

import java.util.ArrayList;
import java.util.List;

import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;
import org.springframework.context.ApplicationContext;

import io.servicecomb.common.rest.locator.ServicePathManager;
import io.servicecomb.core.definition.MicroserviceMeta;
import io.servicecomb.core.definition.SchemaMeta;
Expand All @@ -32,11 +24,17 @@
import io.servicecomb.swagger.generator.core.SwaggerGeneratorContext;
import io.servicecomb.swagger.generator.pojo.PojoSwaggerGeneratorContext;
import io.swagger.models.Swagger;
import java.util.ArrayList;
import java.util.List;
import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;
import org.springframework.context.ApplicationContext;

public class TestRestEngineSchemaListener {
SwaggerGeneratorContext context = new PojoSwaggerGeneratorContext();
private final SwaggerGeneratorContext context = new PojoSwaggerGeneratorContext();

static class Impl {
private static class Impl {
public int add(int x, int y) {
return 0;
}
Expand All @@ -63,6 +61,6 @@ public void test() {
ServicePathManager spm = ServicePathManager.getServicePathManager(mm);
Assert.assertEquals(mm, spm.getMicroserviceMeta());

Assert.assertNotNull(spm.getStaticPathOperationMap().get("Impl/add/"));
Assert.assertNotNull(spm.getStaticPathOperationMap().get("/Impl/add/"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,34 +16,37 @@

package io.servicecomb.common.rest.definition;

import javax.ws.rs.core.MediaType;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import io.servicecomb.common.rest.codec.produce.ProduceProcessorManager;
import org.junit.After;
import io.servicecomb.core.definition.OperationMeta;
import io.servicecomb.core.definition.SchemaMeta;
import io.swagger.models.Operation;
import io.swagger.models.Swagger;
import javax.ws.rs.core.MediaType;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import io.servicecomb.core.definition.OperationMeta;
import org.mockito.Mockito;

public class TestRestOperationMeta {

private RestOperationMeta operationMeta;
private final Swagger swagger = Mockito.mock(Swagger.class);
private final SchemaMeta schemaMeta = Mockito.mock(SchemaMeta.class);
private final Operation operation = Mockito.mock(Operation.class);
private final OperationMeta meta = mock(OperationMeta.class);
private final RestOperationMeta operationMeta = new RestOperationMeta();

@Before
public void beforeTest() {
operationMeta = new RestOperationMeta() {
@Override
public void init(OperationMeta operationMeta) {
}
};
}

@After
public void afterTest() {

operationMeta = null;
public void setUp() throws Exception {
when(meta.getSchemaMeta()).thenReturn(schemaMeta);
when(meta.getSwaggerOperation()).thenReturn(operation);
when(meta.getMethod()).thenReturn(SomeRestController.class.getMethod("sayHi"));

when(schemaMeta.getSwagger()).thenReturn(swagger);
}

@Test
Expand Down Expand Up @@ -78,4 +81,60 @@ public void testEnsureFindProduceProcessor() {
Assert.assertEquals(null, operationMeta.findProduceProcessor("test"));

}

@Test
public void generatesAbsolutePathWithRootBasePath() {
when(swagger.getBasePath()).thenReturn("/");
when(meta.getOperationPath()).thenReturn("/sayHi/");

operationMeta.init(meta);

assertThat(operationMeta.getAbsolutePath(), is("/sayHi/"));
}

@Test
public void generatesAbsolutePathWithNonRootBasePath() {
when(swagger.getBasePath()).thenReturn("/rest");
when(meta.getOperationPath()).thenReturn("/sayHi");

operationMeta.init(meta);

assertThat(operationMeta.getAbsolutePath(), is("/rest/sayHi/"));
}

@Test
public void generatesAbsolutePathWithNullPath() {
when(swagger.getBasePath()).thenReturn(null);
when(meta.getOperationPath()).thenReturn(null);

operationMeta.init(meta);

assertThat(operationMeta.getAbsolutePath(), is("/"));
}

@Test
public void generatesAbsolutePathWithEmptyPath() {
when(swagger.getBasePath()).thenReturn("");
when(meta.getOperationPath()).thenReturn("");

operationMeta.init(meta);

assertThat(operationMeta.getAbsolutePath(), is("/"));
}

@Test
public void consecutiveSlashesAreRemoved() {
when(swagger.getBasePath()).thenReturn("//rest//");
when(meta.getOperationPath()).thenReturn("//sayHi//");

operationMeta.init(meta);

assertThat(operationMeta.getAbsolutePath(), is("/rest/sayHi/"));
}

private static class SomeRestController {
public String sayHi() {
return "Hi";
}
}
}

This file was deleted.

Loading