Skip to content

Commit

Permalink
Add more unit tests for Core (#3977)
Browse files Browse the repository at this point in the history
* Add more unit tests for Core

* Add comments

* Fix typo and add primitive type tests

* Fix checkstyle
  • Loading branch information
srnagar authored Jun 19, 2019
1 parent ac748cb commit 84be0dd
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -256,26 +256,7 @@ public String path(Object[] methodArguments) {
* @return an Iterable with the encoded query parameters
*/
public Iterable<EncodedParameter> encodedQueryParameters(Object[] swaggerMethodArguments) {
final List<EncodedParameter> result = new ArrayList<>();
if (querySubstitutions != null) {
final PercentEscaper escaper = UrlEscapers.QUERY_ESCAPER;

for (Substitution querySubstitution : querySubstitutions) {
final int parameterIndex = querySubstitution.methodParameterIndex();
if (0 <= parameterIndex && parameterIndex < swaggerMethodArguments.length) {
final Object methodArgument = swaggerMethodArguments[querySubstitution.methodParameterIndex()];
String parameterValue = serialize(methodArgument);
if (parameterValue != null) {
if (querySubstitution.shouldEncode() && escaper != null) {
parameterValue = escaper.escape(parameterValue);
}

result.add(new EncodedParameter(querySubstitution.urlParameterName(), parameterValue));
}
}
}
}
return result;
return encodeParameters(swaggerMethodArguments, querySubstitutions);
}

/**
Expand All @@ -287,23 +268,27 @@ public Iterable<EncodedParameter> encodedQueryParameters(Object[] swaggerMethodA
* @return an Iterable with the encoded form parameters
*/
public Iterable<EncodedParameter> encodedFormParameters(Object[] swaggerMethodArguments) {
if (formSubstitutions == null) {
return encodeParameters(swaggerMethodArguments, formSubstitutions);
}

private Iterable<EncodedParameter> encodeParameters(Object[] swaggerMethodArguments,
List<Substitution> substitutions) {
if (substitutions == null) {
return Collections.emptyList();
}

final List<EncodedParameter> result = new ArrayList<>();
final PercentEscaper escaper = UrlEscapers.QUERY_ESCAPER;

for (Substitution formSubstitution : formSubstitutions) {
final int parameterIndex = formSubstitution.methodParameterIndex();
for (Substitution substitution : substitutions) {
final int parameterIndex = substitution.methodParameterIndex();
if (0 <= parameterIndex && parameterIndex < swaggerMethodArguments.length) {
final Object methodArgument = swaggerMethodArguments[formSubstitution.methodParameterIndex()];
final Object methodArgument = swaggerMethodArguments[substitution.methodParameterIndex()];
String parameterValue = serialize(methodArgument);
if (parameterValue != null) {
if (formSubstitution.shouldEncode() && escaper != null) {
if (substitution.shouldEncode() && escaper != null) {
parameterValue = escaper.escape(parameterValue);
}

result.add(new EncodedParameter(formSubstitution.urlParameterName(), parameterValue));
result.add(new EncodedParameter(substitution.urlParameterName(), parameterValue));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,6 @@ public static void validate(Object parameter) {
}

Class<?> type = parameter.getClass();
if (type == Double.class
|| type == Float.class
|| type == Long.class
|| type == Integer.class
|| type == Short.class
|| type == Character.class
|| type == Byte.class
|| type == Boolean.class) {
type = wrapperToPrimitive(type);
}
if (type.isPrimitive()
|| type.isEnum()
|| type.isAssignableFrom(Class.class)
Expand All @@ -72,34 +62,6 @@ public static void validate(Object parameter) {
}
}

private static Class<?> wrapperToPrimitive(Class<?> clazz) {
if (!clazz.isPrimitive()) {
return clazz;
}

if (clazz == Integer.class) {
return Integer.TYPE;
} else if (clazz == Long.class) {
return Long.TYPE;
} else if (clazz == Boolean.class) {
return Boolean.TYPE;
} else if (clazz == Byte.class) {
return Byte.TYPE;
} else if (clazz == Character.class) {
return Character.TYPE;
} else if (clazz == Float.class) {
return Float.TYPE;
} else if (clazz == Double.class) {
return Double.TYPE;
} else if (clazz == Short.class) {
return Short.TYPE;
} else if (clazz == Void.class) {
return Void.TYPE;
}

return clazz;
}

private static void validateClass(Class<?> c, Object parameter) {
// Ignore checks for Object type.
if (c.isAssignableFrom(Object.class)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

package com.azure.core.implementation;

import com.azure.core.annotations.SkipParentValidation;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.node.TextNode;
import org.junit.Assert;
Expand Down Expand Up @@ -133,6 +134,27 @@ public void validateRecursive() {
Validator.validate(textNode);
}

@Test
public void validateSkipParentAnnotation() {
Child child = new Child();
child.name(new StringWrapper()); //parent field - StringWrapper's value is null but marked as required
child.count(5);
// Without SkipParentAnnotation, this would have failed
Validator.validate(child);
}

@Test
public void validatePrimitives() {
Validator.validate(4);
Validator.validate(4L);
Validator.validate(4.0f);
Validator.validate(4.0);
Validator.validate('a');
Validator.validate(true);
Validator.validate((short) 4);
Validator.validate((byte) 4);
}

public final class IntWrapper {
@JsonProperty(required = true)
private int value;
Expand Down Expand Up @@ -259,4 +281,28 @@ public void tag(String tag) {
this.tag = tag;
}
}

public class Parent {
private StringWrapper name;
public StringWrapper name() {
return name;
}

public void name(StringWrapper name) {
this.name = name;
}
}

@SkipParentValidation
public class Child extends Parent {
private int count;

public int count() {
return this.count;
}

public void count(int count) {
this.count = count;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
package com.azure.core.implementation.util;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;
import io.netty.util.ReferenceCountUtil;
import org.junit.Ignore;
import org.junit.Test;
import reactor.core.Exceptions;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;

import java.io.BufferedOutputStream;
Expand Down Expand Up @@ -246,6 +249,19 @@ public void toByteArrayWithNonEmptyByteBuffer() {
assertArrayEquals(new byte[] { 0, 1, 2, 3, 4 }, byteArray);
assertEquals(5, byteBuffer.readableBytes());
}

@Test
public void testCollectByteBufStream() {
Flux<ByteBuf> byteBufFlux = Flux
.just(Unpooled.copyInt(1), Unpooled.copyInt(255), Unpooled.copyInt(256));
Mono<ByteBuf> result = FluxUtil.collectByteBufStream(byteBufFlux, false);
byte[] bytes = ByteBufUtil.getBytes(result.block());
assertEquals(12, bytes.length);
assertArrayEquals(new byte[]{
0, 0, 0, 1,
0, 0, 0, (byte) 255,
0, 0, 1, 0}, bytes);
}
//
private static byte[] toBytes(ByteBuf bb) {
byte[] bytes = new byte[bb.readableBytes()];
Expand Down
13 changes: 13 additions & 0 deletions pom.client.xml
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,19 @@
<propertyName>surefireArgLine</propertyName>
</configuration>
</execution>
<execution>
<!-- This generates the unit test reports for individual modules.
jacoco-test-coverage generates aggregate reports for all modules -->
<id>post-unit-test</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<!-- Sets the output directory for the code coverage report. -->
<outputDirectory>${project.reporting.outputDirectory}/test-coverage</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>

Expand Down

0 comments on commit 84be0dd

Please sign in to comment.