Skip to content

Commit

Permalink
feat: Enhance OpenAPI for cosid-proxy (#399)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahoo-Wang authored Aug 3, 2023
1 parent 4580cf1 commit bea04c0
Show file tree
Hide file tree
Showing 24 changed files with 261 additions and 84 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright [2021-present] [ahoo wang <ahoowang@qq.com> (https://github.com/Ahoo-Wang)].
* Licensed 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 me.ahoo.cosid.accessor;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;

import org.junit.jupiter.api.Test;

class NotFoundCosIdAccessorTest {
@Test
public void getIdDefinition() {
assertThat(CosIdAccessor.NOT_FOUND.getIdDefinition(), nullValue());
}

@Test
public void getIdGenerator() {
assertThat(CosIdAccessor.NOT_FOUND.getIdGenerator(), nullValue());
}

@Test
public void getIdField() {
assertThat(CosIdAccessor.NOT_FOUND.getIdField(), nullValue());
}

@Test
public void getId() {
assertThat(CosIdAccessor.NOT_FOUND.getId(new Object()), nullValue());
}

@Test
public void setId() {
CosIdAccessor.NOT_FOUND.setId(new Object(), new Object());
}

@Test
public void ensureId() {
assertThat(CosIdAccessor.NOT_FOUND.ensureId(new Object()), equalTo(false));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@

package me.ahoo.cosid.accessor.registry;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;

import me.ahoo.cosid.accessor.parser.DefaultAccessorParser;
import me.ahoo.cosid.annotation.AnnotationDefinitionParser;
import me.ahoo.cosid.annotation.entity.IntIdEntity;
import me.ahoo.cosid.annotation.entity.LongIdEntity;
import me.ahoo.cosid.annotation.entity.MissingIdGenEntity;
import me.ahoo.cosid.annotation.entity.StringIdEntity;
Expand All @@ -30,9 +34,9 @@
* @author ahoo wang
*/
class DefaultAccessorRegistryTest {

CosIdAccessorRegistry accessorRegistry = new DefaultAccessorRegistry(new DefaultAccessorParser(AnnotationDefinitionParser.INSTANCE));

@Test
void ensureIdExistsByAnnotation() {
DefaultIdGeneratorProvider.INSTANCE.setShare(AtomicLongGenerator.INSTANCE);
Expand All @@ -41,21 +45,28 @@ void ensureIdExistsByAnnotation() {
accessorRegistry.ensureId(entity);
Assertions.assertEquals(888, entity.getId());
}

@Test
void ensureIdNotFindByAnnotation() {
MissingIdGenEntity entity = new MissingIdGenEntity();
Assertions.assertThrows(NotFoundIdGeneratorException.class, () -> {
accessorRegistry.ensureId(entity);
});
}

@Test
void ensureStringId() {
DefaultIdGeneratorProvider.INSTANCE.setShare(AtomicLongGenerator.INSTANCE);
StringIdEntity entity = new StringIdEntity();
accessorRegistry.ensureId(entity);
Assertions.assertFalse(Strings.isNullOrEmpty(entity.getId()));
}


@Test
void ensureIntId() {
DefaultIdGeneratorProvider.INSTANCE.setShare(AtomicLongGenerator.INSTANCE);
IntIdEntity entity = new IntIdEntity();
accessorRegistry.ensureId(entity);
assertThat(entity.getId(), not(0));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
package me.ahoo.cosid.accessor.scanner;

import me.ahoo.cosid.accessor.CosIdAccessor;
import me.ahoo.cosid.accessor.parser.CosIdAccessorParser;
import me.ahoo.cosid.accessor.parser.DefaultAccessorParser;
import me.ahoo.cosid.accessor.parser.NamedDefinitionParser;
import me.ahoo.cosid.accessor.registry.CosIdAccessorRegistry;
Expand All @@ -23,6 +22,7 @@
import me.ahoo.cosid.accessor.scanner.entity.OrderItemEntity;
import me.ahoo.cosid.annotation.AnnotationDefinitionParser;
import me.ahoo.cosid.annotation.entity.ChildEntity;
import me.ahoo.cosid.annotation.entity.IntIdEntity;
import me.ahoo.cosid.annotation.entity.LongIdEntity;
import me.ahoo.cosid.annotation.entity.MissingIdGenEntity;
import me.ahoo.cosid.annotation.entity.PrimitiveLongIdEntity;
Expand All @@ -35,56 +35,62 @@
* @author ahoo wang
*/
class DefaultCosIdScannerTest {

@Test
void scanAnnotationDefinitionParser() {
CosIdAccessorRegistry registry = new DefaultAccessorRegistry(new DefaultAccessorParser(AnnotationDefinitionParser.INSTANCE));
DefaultCosIdScanner scanner =
new DefaultCosIdScanner(new String[] {"me.ahoo.cosid.accessor.annotation.entity"}, AnnotationDefinitionParser.INSTANCE, registry);
scanner.scan();

CosIdAccessor cosIdAccessor = registry.get(LongIdEntity.class);
Assertions.assertNotNull(cosIdAccessor);
Assertions.assertNotEquals(CosIdAccessor.NOT_FOUND, cosIdAccessor);
Assertions.assertEquals(LongIdEntity.class, cosIdAccessor.getIdDeclaringClass());

cosIdAccessor = registry.get(MissingIdGenEntity.class);
Assertions.assertNotNull(cosIdAccessor);
Assertions.assertNotEquals(CosIdAccessor.NOT_FOUND, cosIdAccessor);
Assertions.assertEquals(MissingIdGenEntity.class, cosIdAccessor.getIdDeclaringClass());

cosIdAccessor = registry.get(PrimitiveLongIdEntity.class);
Assertions.assertNotNull(cosIdAccessor);
Assertions.assertNotEquals(CosIdAccessor.NOT_FOUND, cosIdAccessor);
Assertions.assertEquals(PrimitiveLongIdEntity.class, cosIdAccessor.getIdDeclaringClass());


cosIdAccessor = registry.get(IntIdEntity.class);
Assertions.assertNotNull(cosIdAccessor);
Assertions.assertNotEquals(CosIdAccessor.NOT_FOUND, cosIdAccessor);
Assertions.assertEquals(IntIdEntity.class, cosIdAccessor.getIdDeclaringClass());

cosIdAccessor = registry.get(StringIdEntity.class);
Assertions.assertNotNull(cosIdAccessor);
Assertions.assertNotEquals(CosIdAccessor.NOT_FOUND, cosIdAccessor);
Assertions.assertEquals(StringIdEntity.class, cosIdAccessor.getIdDeclaringClass());

cosIdAccessor = registry.get(ChildEntity.class);
Assertions.assertNotNull(cosIdAccessor);
Assertions.assertNotEquals(CosIdAccessor.NOT_FOUND, cosIdAccessor);
Assertions.assertEquals(LongIdEntity.class, cosIdAccessor.getIdDeclaringClass());
}

@Test
void scanNamedDefinitionParser() {
CosIdAccessorRegistry registry = new DefaultAccessorRegistry(new DefaultAccessorParser(AnnotationDefinitionParser.INSTANCE));
DefaultCosIdScanner scanner =
new DefaultCosIdScanner(new String[] {"me.ahoo.cosid.accessor.scanner.entity"}, new NamedDefinitionParser("id"), registry);
scanner.scan();

CosIdAccessor cosIdAccessor = registry.get(OrderEntity.class);
Assertions.assertNotNull(cosIdAccessor);
Assertions.assertNotEquals(CosIdAccessor.NOT_FOUND, cosIdAccessor);
Assertions.assertEquals(OrderEntity.class, cosIdAccessor.getIdDeclaringClass());

cosIdAccessor = registry.get(OrderItemEntity.class);
Assertions.assertNotNull(cosIdAccessor);
Assertions.assertNotEquals(CosIdAccessor.NOT_FOUND, cosIdAccessor);
Assertions.assertEquals(OrderItemEntity.class, cosIdAccessor.getIdDeclaringClass());

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright [2021-present] [ahoo wang <ahoowang@qq.com> (https://github.com/Ahoo-Wang)].
* Licensed 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 me.ahoo.cosid.annotation.entity;

import me.ahoo.cosid.annotation.CosId;

/**
* @author ahoo wang
*/
public class IntIdEntity {
@CosId
private int id;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}
}
1 change: 1 addition & 0 deletions cosid-dependencies/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ dependencies {
api(libs.guava)
api(libs.mybatis)
api(libs.mybatisSpringBoot)
api(libs.springDocStarterWebfluxUi)
api(libs.junitPioneer)
api(libs.hamcrest)
api(libs.jmhCore)
Expand Down
11 changes: 6 additions & 5 deletions cosid-proxy-server/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ application {
mainClass.set("me.ahoo.cosid.proxy.server.ProxyServer")

applicationDefaultJvmArgs = listOf(
"-Xms512M",
"-Xmx512M",
"-XX:MaxMetaspaceSize=128M",
"-XX:MaxDirectMemorySize=256M",
"-Xms1280M",
"-Xmx1280M",
"-XX:MaxMetaspaceSize=256M",
"-XX:MaxDirectMemorySize=512M",
"-Xss1m",
"-server",
"-XX:+UseG1GC",
"-XX:+UseZGC",
"-Xlog:gc*:file=logs/${applicationName}-gc.log:time,tags:filecount=10,filesize=32M",
"-XX:+HeapDumpOnOutOfMemoryError",
"-XX:HeapDumpPath=data",
Expand Down Expand Up @@ -79,6 +79,7 @@ dependencies {
implementation("com.google.guava:guava")
implementation("io.netty:netty-all")
implementation("org.springframework.boot:spring-boot-starter-webflux")
implementation("org.springdoc:springdoc-openapi-starter-webflux-ui")
compileOnly("org.projectlombok:lombok")
annotationProcessor("org.projectlombok:lombok")
annotationProcessor("org.springframework.boot:spring-boot-configuration-processor")
Expand Down
22 changes: 15 additions & 7 deletions cosid-proxy-server/src/dist/config/application.yaml
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
management:
endpoints:
web:
exposure:
include:
- cosid
- cosidGenerator
- cosidStringGenerator
springdoc:
show-actuator: true
server:
port: 8688
spring:
application:
name: ${service.name:cosid-proxy}
# redis:
# url: redis://localhost:6379
# redis:
# url: redis://localhost:6379
autoconfigure:
exclude:
# - org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration
- org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
# - org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration
- org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
# datasource:
# url: jdbc:mysql://localhost:3306/cosid_db
# username: root
Expand All @@ -19,15 +29,13 @@ cosid:
enabled: true
distributor:
type: redis
guarder:
enabled: true
snowflake:
enabled: true
segment:
enabled: true
mode: chain
distributor:
type: redis
logging:
level:
me.ahoo.cosid: debug

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright [2021-present] [ahoo wang <ahoowang@qq.com> (https://github.com/Ahoo-Wang)].
* Licensed 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 me.ahoo.cosid.proxy.server.configuration;

import com.google.common.base.MoreObjects;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import org.springdoc.core.customizers.OpenApiCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class SwaggerConfiguration {

@Bean
public OpenApiCustomizer openApiCustomizer() {
var version = MoreObjects.firstNonNull(getClass().getPackage().getImplementationVersion(), "2.3.0");
return openApi -> {
var info = new Info()
.title("CosId Proxy Server")
.description("Universal, flexible, high-performance distributed ID generator.")
.contact(new Contact().name("Ahoo Wang").url("https://github.com/Ahoo-Wang/CosId"))
.license(new License().url("https://github.com/Ahoo-Wang/CosId/blob/main/LICENSE").name("Apache 2.0"))
.version(version);
openApi.info(info);
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import me.ahoo.cosid.provider.IdGeneratorProvider;

import io.swagger.v3.oas.annotations.Operation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -38,27 +39,31 @@ public IdController(IdGeneratorProvider provider) {
this.provider = provider;
}

@Operation(summary = "Generate a ID by share.")
@GetMapping
public long generate() {
return provider
.getShare()
.generate();
}

@Operation(summary = "Generate a ID by id name.")
@GetMapping("{name}")
public long generate(@PathVariable String name) {
return provider
.getRequired(name)
.generate();
}

@Operation(summary = "Generate a ID as String by share.")
@GetMapping("/as-string")
public String generateAsString() {
return provider
.getShare()
.generateAsString();
}

@Operation(summary = "Generate a ID as String by id name.")
@GetMapping("/as-string/{name}")
public String generateAsString(@PathVariable String name) {
return provider
Expand Down
Loading

0 comments on commit bea04c0

Please sign in to comment.