Search in sources :

Example 1 with ApiResultObject

use of com.terran4j.commons.api2doc.domain.ApiResultObject in project commons by terran4j.

the class ApiResultObjectTest method testParseResultTypeWithNotOrder.

@Test
public void testParseResultTypeWithNotOrder() throws Exception {
    log.info("testParseResultTypeWithNotOrder");
    Method method = ReflectionUtils.findMethod(getClass(), "getNotOrderBean");
    Assert.assertNotNull(method);
    ApiResultObject object = ApiResultObject.parseResultType(method, null);
    Assert.assertNotNull(object);
    Assert.assertEquals(4, object.getChildren().size());
    Assert.assertEquals("abc", object.getChildren().get(0).getId());
    Assert.assertEquals("abd", object.getChildren().get(1).getId());
    Assert.assertEquals("b1", object.getChildren().get(2).getId());
    Assert.assertEquals("b2", object.getChildren().get(3).getId());
}
Also used : Method(java.lang.reflect.Method) ApiResultObject(com.terran4j.commons.api2doc.domain.ApiResultObject) Test(org.junit.Test)

Example 2 with ApiResultObject

use of com.terran4j.commons.api2doc.domain.ApiResultObject in project commons by terran4j.

the class ApiResultObjectTest method testParseResultType.

@Test
public void testParseResultType() throws Exception {
    log.info("testParseResultType");
    Method method = ReflectionUtils.findMethod(getClass(), "getUsers");
    Assert.assertNotNull(method);
    KeyedList<String, ApiResultObject> totalResults = new KeyedList<>();
    ApiResultObject object = ApiResultObject.parseResultType(method, totalResults);
    Assert.assertNotNull(object);
    Assert.assertEquals(1, totalResults.size());
    Assert.assertTrue(object == totalResults.get(0));
    Assert.assertTrue(ApiDataType.ARRAY == object.getDataType());
    Assert.assertTrue(User.class == object.getSourceType());
}
Also used : KeyedList(com.terran4j.commons.util.value.KeyedList) Method(java.lang.reflect.Method) ApiResultObject(com.terran4j.commons.api2doc.domain.ApiResultObject) Test(org.junit.Test)

Example 3 with ApiResultObject

use of com.terran4j.commons.api2doc.domain.ApiResultObject in project commons by terran4j.

the class ApiResultObjectTest method testParseResultTypeWithIgnore.

@Test
public void testParseResultTypeWithIgnore() throws Exception {
    log.info("testParseResultTypeWithIgnore");
    Method method = ReflectionUtils.findMethod(getClass(), "getUsers");
    Assert.assertNotNull(method);
    KeyedList<String, ApiResultObject> totalResults = new KeyedList<>();
    ApiResultObject object = ApiResultObject.parseResultType(method, totalResults);
    Assert.assertNotNull(object);
    Assert.assertNull(object.getChild("deleted"));
}
Also used : KeyedList(com.terran4j.commons.util.value.KeyedList) Method(java.lang.reflect.Method) ApiResultObject(com.terran4j.commons.api2doc.domain.ApiResultObject) Test(org.junit.Test)

Example 4 with ApiResultObject

use of com.terran4j.commons.api2doc.domain.ApiResultObject in project commons by terran4j.

the class ParseApiCommentOnSeeClassLoop method testParseApiCommentOnSeeClassLoop.

@Test
public void testParseApiCommentOnSeeClassLoop() throws Exception {
    log.info("testParseApiCommentOnSeeClass");
    Api2DocCollector collector = new Api2DocCollector();
    ApiFolderObject folder = collector.toApiFolder(new ParseApiCommentOnSeeClassLoop.MyController(), "myController");
    ApiDocObject doc = folder.getDoc("updateUser");
    List<ApiParamObject> params = doc.getParams();
    Assert.assertEquals("用户id", params.get(0).getComment().toString());
    Assert.assertEquals("123", params.get(0).getSample().toString());
    Assert.assertEquals("用户名称", params.get(1).getComment().toString());
    Assert.assertEquals("neo", params.get(1).getSample().toString());
    ApiResultObject user = doc.getResults().get(0);
    ApiResultObject userId = user.getChildren().get(0);
    Assert.assertEquals("id", userId.getId());
    Assert.assertEquals("用户id", userId.getComment().getValue());
    Assert.assertEquals("123", userId.getSample().getValue());
    ApiResultObject userName = user.getChildren().get(1);
    Assert.assertEquals("name", userName.getId());
    Assert.assertEquals("用户名称", userName.getComment().getValue());
    Assert.assertEquals("neo", userName.getSample().getValue());
}
Also used : Api2DocCollector(com.terran4j.commons.api2doc.impl.Api2DocCollector) ApiParamObject(com.terran4j.commons.api2doc.domain.ApiParamObject) ApiFolderObject(com.terran4j.commons.api2doc.domain.ApiFolderObject) ApiDocObject(com.terran4j.commons.api2doc.domain.ApiDocObject) ApiResultObject(com.terran4j.commons.api2doc.domain.ApiResultObject) Test(org.junit.Test)

Example 5 with ApiResultObject

use of com.terran4j.commons.api2doc.domain.ApiResultObject in project commons by terran4j.

the class JavaBeanCodeWriter method getModel.

public // 
Map<String, Object> getModel(// 
ApiResultObject result, // 
String className, CodeConfig config) {
    Map<String, Object> model = new HashedMap<>();
    model.put("class", className);
    if (config == null) {
        config = new CodeConfig();
    }
    model.put("config", config);
    // java 类上的注释。
    String comment = result.getComment().javadoc(0);
    if (StringUtils.hasText(comment)) {
        model.put("comment", comment);
    }
    Set<String> imports = new HashSet<>();
    model.put("imports", imports);
    List<FieldInfo> fields = new ArrayList<FieldInfo>();
    List<ApiResultObject> children = result.getChildren();
    for (ApiResultObject child : children) {
        FieldInfo field = new FieldInfo();
        String name = child.getId();
        field.setName(name);
        String type = toTypeName(child);
        field.setType(type);
        String fieldComment = child.getComment().javadoc(1);
        if (StringUtils.hasText(fieldComment)) {
            field.setComment(fieldComment);
        }
        // Date 自动转成 Long 类型了。
        Class<?> sourceType = getSourceType(child);
        CodeUtils.addImport(sourceType, imports);
        boolean isBooleanClass = (child.getDataType() == ApiDataType.BOOLEAN);
        String getMethod = toGetMethodName(isBooleanClass, name);
        field.setGetMethod(getMethod);
        String setMethod = toSetMethodName(name);
        field.setSetMethod(setMethod);
        fields.add(field);
    }
    model.put("fields", fields);
    return model;
}
Also used : ApiResultObject(com.terran4j.commons.api2doc.domain.ApiResultObject) ApiResultObject(com.terran4j.commons.api2doc.domain.ApiResultObject) HashedMap(org.apache.commons.collections4.map.HashedMap)

Aggregations

ApiResultObject (com.terran4j.commons.api2doc.domain.ApiResultObject)12 Test (org.junit.Test)11 Method (java.lang.reflect.Method)9 KeyedList (com.terran4j.commons.util.value.KeyedList)7 ApiDocObject (com.terran4j.commons.api2doc.domain.ApiDocObject)2 ApiFolderObject (com.terran4j.commons.api2doc.domain.ApiFolderObject)2 ApiParamObject (com.terran4j.commons.api2doc.domain.ApiParamObject)2 Api2DocCollector (com.terran4j.commons.api2doc.impl.Api2DocCollector)2 MemoryCodeOutput (com.terran4j.commons.api2doc.codewriter.MemoryCodeOutput)1 Set (java.util.Set)1 HashedMap (org.apache.commons.collections4.map.HashedMap)1 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)1