Search in sources :

Example 51 with JFieldVar

use of com.sun.codemodel.JFieldVar in project jsonschema2pojo by joelittlejohn.

the class MinItemsMaxItemsRuleTest method testNotUsed.

@Test
public void testNotUsed() {
    when(config.isIncludeJsr303Annotations()).thenReturn(true);
    when(node.has("minItems")).thenReturn(false);
    when(node.has("maxItems")).thenReturn(false);
    when(fieldVar.type().boxify().fullName()).thenReturn(fieldClass.getTypeName());
    JFieldVar result = rule.apply("node", node, null, fieldVar, null);
    assertSame(fieldVar, result);
    verify(fieldVar, never()).annotate(sizeClass);
    verify(annotation, never()).param(anyString(), anyInt());
}
Also used : JFieldVar(com.sun.codemodel.JFieldVar) Test(org.junit.Test)

Example 52 with JFieldVar

use of com.sun.codemodel.JFieldVar in project jsonschema2pojo by joelittlejohn.

the class MinItemsMaxItemsRuleTest method testMaxAndMinLength.

@Test
public void testMaxAndMinLength() {
    when(config.isIncludeJsr303Annotations()).thenReturn(true);
    final int minValue = new Random().nextInt();
    final int maxValue = new Random().nextInt();
    JsonNode maxSubNode = Mockito.mock(JsonNode.class);
    when(subNode.asInt()).thenReturn(minValue);
    when(maxSubNode.asInt()).thenReturn(maxValue);
    when(node.get("minItems")).thenReturn(subNode);
    when(node.get("maxItems")).thenReturn(maxSubNode);
    when(fieldVar.annotate(sizeClass)).thenReturn(annotation);
    when(node.has("minItems")).thenReturn(true);
    when(node.has("maxItems")).thenReturn(true);
    when(fieldVar.type().boxify().fullName()).thenReturn(fieldClass.getTypeName());
    JFieldVar result = rule.apply("node", node, null, fieldVar, null);
    assertSame(fieldVar, result);
    verify(fieldVar, times(isApplicable ? 1 : 0)).annotate(sizeClass);
    verify(annotation, times(isApplicable ? 1 : 0)).param("min", minValue);
    verify(annotation, times(isApplicable ? 1 : 0)).param("max", maxValue);
}
Also used : Random(java.util.Random) JFieldVar(com.sun.codemodel.JFieldVar) JsonNode(com.fasterxml.jackson.databind.JsonNode) Test(org.junit.Test)

Example 53 with JFieldVar

use of com.sun.codemodel.JFieldVar in project jsonschema2pojo by joelittlejohn.

the class MinItemsMaxItemsRuleTest method testMaxLength.

@Test
public void testMaxLength() {
    when(config.isIncludeJsr303Annotations()).thenReturn(true);
    final int maxValue = new Random().nextInt();
    when(subNode.asInt()).thenReturn(maxValue);
    when(node.get("maxItems")).thenReturn(subNode);
    when(fieldVar.annotate(sizeClass)).thenReturn(annotation);
    when(node.has("maxItems")).thenReturn(true);
    when(fieldVar.type().boxify().fullName()).thenReturn(fieldClass.getTypeName());
    JFieldVar result = rule.apply("node", node, null, fieldVar, null);
    assertSame(fieldVar, result);
    verify(fieldVar, times(isApplicable ? 1 : 0)).annotate(sizeClass);
    verify(annotation, times(isApplicable ? 1 : 0)).param("max", maxValue);
    verify(annotation, never()).param(eq("min"), anyInt());
}
Also used : Random(java.util.Random) JFieldVar(com.sun.codemodel.JFieldVar) Test(org.junit.Test)

Example 54 with JFieldVar

use of com.sun.codemodel.JFieldVar in project jsonschema2pojo by joelittlejohn.

the class MinimumMaximumRuleTest method testMaximum.

@Test
public void testMaximum() {
    when(config.isIncludeJsr303Annotations()).thenReturn(true);
    final String maxValue = Integer.toString(new Random().nextInt());
    when(subNode.asText()).thenReturn(maxValue);
    when(node.get("maximum")).thenReturn(subNode);
    when(fieldVar.annotate(decimalMaxClass)).thenReturn(annotationMax);
    when(node.has("maximum")).thenReturn(true);
    when(fieldVar.type().boxify().fullName()).thenReturn(fieldClass.getTypeName());
    JFieldVar result = rule.apply("node", node, null, fieldVar, null);
    assertSame(fieldVar, result);
    verify(fieldVar, times(isApplicable ? 1 : 0)).annotate(decimalMaxClass);
    verify(annotationMax, times(isApplicable ? 1 : 0)).param("value", maxValue);
    verify(fieldVar, never()).annotate(decimalMinClass);
    verify(annotationMin, never()).param(eq("value"), anyString());
}
Also used : Random(java.util.Random) JFieldVar(com.sun.codemodel.JFieldVar) Test(org.junit.Test)

Example 55 with JFieldVar

use of com.sun.codemodel.JFieldVar in project jsonschema2pojo by joelittlejohn.

the class MinimumMaximumRuleTest method testMaximumAndMinimum.

@Test
public void testMaximumAndMinimum() {
    when(config.isIncludeJsr303Annotations()).thenReturn(true);
    final String minValue = Integer.toString(new Random().nextInt());
    final String maxValue = Integer.toString(new Random().nextInt());
    JsonNode maxSubNode = Mockito.mock(JsonNode.class);
    when(subNode.asText()).thenReturn(minValue);
    when(maxSubNode.asText()).thenReturn(maxValue);
    when(node.get("minimum")).thenReturn(subNode);
    when(node.get("maximum")).thenReturn(maxSubNode);
    when(fieldVar.annotate(decimalMinClass)).thenReturn(annotationMin);
    when(fieldVar.annotate(decimalMaxClass)).thenReturn(annotationMax);
    when(node.has("minimum")).thenReturn(true);
    when(node.has("maximum")).thenReturn(true);
    when(fieldVar.type().boxify().fullName()).thenReturn(fieldClass.getTypeName());
    JFieldVar result = rule.apply("node", node, null, fieldVar, null);
    assertSame(fieldVar, result);
    verify(fieldVar, times(isApplicable ? 1 : 0)).annotate(decimalMinClass);
    verify(annotationMin, times(isApplicable ? 1 : 0)).param("value", minValue);
    verify(fieldVar, times(isApplicable ? 1 : 0)).annotate(decimalMaxClass);
    verify(annotationMax, times(isApplicable ? 1 : 0)).param("value", maxValue);
}
Also used : Random(java.util.Random) JFieldVar(com.sun.codemodel.JFieldVar) JsonNode(com.fasterxml.jackson.databind.JsonNode) Test(org.junit.Test)

Aggregations

JFieldVar (com.sun.codemodel.JFieldVar)74 JMethod (com.sun.codemodel.JMethod)33 JVar (com.sun.codemodel.JVar)28 JClass (com.sun.codemodel.JClass)24 Test (org.junit.Test)24 JBlock (com.sun.codemodel.JBlock)21 JInvocation (com.sun.codemodel.JInvocation)19 JDefinedClass (com.sun.codemodel.JDefinedClass)16 Random (java.util.Random)12 ByteString (com.linkedin.data.ByteString)10 HashMap (java.util.HashMap)10 JsonNode (com.fasterxml.jackson.databind.JsonNode)9 JExpression (com.sun.codemodel.JExpression)9 JConditional (com.sun.codemodel.JConditional)8 JType (com.sun.codemodel.JType)8 ArrayList (java.util.ArrayList)7 Map (java.util.Map)6 JFieldRef (com.sun.codemodel.JFieldRef)5 JCodeModel (com.sun.codemodel.JCodeModel)3 JPackage (com.sun.codemodel.JPackage)3