Skip to content

Commit

Permalink
[SCB-2687]removed producer swagger override: fix demo error and remov…
Browse files Browse the repository at this point in the history
…e api
  • Loading branch information
liubao68 committed Jul 21, 2023
1 parent 9834af3 commit 6fff729
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 193 deletions.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,16 @@

package org.apache.servicecomb.demo.crossapp;

import org.apache.servicecomb.provider.pojo.RpcSchema;
import org.apache.servicecomb.provider.rest.common.RestSchema;

@RpcSchema(schemaId = "helloworld")
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;

@RestSchema(schemaId = "helloworld")
@Path("helloworld")
public class HelloWorldImpl {
@GET
@Path("hello")
public String sayHello() {
return "hello world";
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -60,26 +60,6 @@ public static String calcSchemaSummary(String schemaContent) {
return Hashing.sha256().newHasher().putString(schemaContent, Charsets.UTF_8).hash().toString();
}

/**
* <pre>
* register swaggers in the location to current microservice
* Scenes for contract first mode:
* 1.consumer
* manager manage some product, can only know product microservice names after deploy
* and can only register swagger after product registered
* in fact, consumers can load swagger from ServiceCenter
* so for consumer, this logic is not necessary, just keep it for compatible
* 2.producer
* deploy to different microservice name in different product
* can register swaggers in BootListener.onBeforeProducerProvider
* </pre>
* @param swaggersLocation eg. "test/schemas", will load all test/schemas/*.yaml
*/
public void registerSwaggersInLocation(String swaggersLocation) {
String microserviceName = RegistrationManager.INSTANCE.getMicroservice().getServiceName();
registerSwaggersInLocation(microserviceName, swaggersLocation);
}

public void registerSwaggersInLocation(String microserviceName, String swaggersLocation) {
LOGGER.info("register schemas in location [{}], microserviceName=[{}]", swaggersLocation, microserviceName);
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,15 @@

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.net.URI;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;

import org.apache.commons.io.IOUtils;
import org.apache.servicecomb.foundation.common.Holder;
import org.apache.servicecomb.foundation.common.exceptions.ServiceCombException;
import org.apache.servicecomb.foundation.common.utils.JvmUtils;
import org.apache.servicecomb.foundation.common.utils.ResourceUtil;
import org.apache.servicecomb.registry.RegistrationManager;
import org.apache.servicecomb.registry.api.registry.Microservice;
import org.apache.servicecomb.serviceregistry.TestRegistryBase;
Expand Down Expand Up @@ -206,67 +199,4 @@ String toString(URL url, Charset encoding) {
}
};
}

@Test
public void should_ignore_not_exist_location_when_register_swagger_in_location() {
Map<String, Object> apps = Deencapsulation.getField(RegistrationManager.INSTANCE.getSwaggerLoader(), "apps");
apps.clear();
RegistrationManager.INSTANCE.getSwaggerLoader().registerSwaggersInLocation("notExistPath");
assertThat(apps).isEmpty();
}

@Test
public void should_ignore_non_yaml_file_when_register_swagger_in_location() {
RegistrationManager.INSTANCE.getSwaggerLoader().registerSwaggersInLocation("swagger-del");
assertThat(RegistrationManager.INSTANCE.getSwaggerLoader().loadFromMemory(appId, serviceName, "other")).isNull();
}

@Test
public void should_throw_exception_when_register_invalid_swagger_in_location() {
IllegalStateException exception = Assertions.assertThrows(IllegalStateException.class, () -> {
URL url = new MockUp<URL>() {

private final String path = "location/invalid.yaml";

@Mock
String getPath() {
return path;
}

@Mock
String toExternalForm() {
return path;
}
}.getMockInstance();
URI uri = new MockUp<URI>() {
@Mock
URL toURL() {
return url;
}
}.getMockInstance();
new MockUp<ResourceUtil>() {
@Mock
List<URI> findResources(String directory, Predicate<Path> filter) {
return Collections.singletonList(uri);
}
};
new MockUp<IOUtils>() {
@Mock
String toString(final URL url, final Charset encoding) {
return "invalid yaml content";
}
};

RegistrationManager.INSTANCE.getSwaggerLoader().registerSwaggersInLocation("location");
});
Assertions.assertEquals("failed to register swaggers, microserviceName=default, location=location.",
exception.getMessage());
Assertions.assertTrue(exception.getCause() instanceof ServiceCombException);
}

@Test
public void should_correct_register_swagger_in_location() {
RegistrationManager.INSTANCE.getSwaggerLoader().registerSwaggersInLocation("swagger-del");
assertThat(RegistrationManager.INSTANCE.getSwaggerLoader().loadFromMemory(appId, serviceName, "hello")).isNotNull();
}
}

0 comments on commit 6fff729

Please sign in to comment.