Skip to content

Commit

Permalink
Updated Java SDK generation for latest 5.6 code. Added parsing test f…
Browse files Browse the repository at this point in the history
…or 5.6. Removed java generated code references to UrlEscapers as they are no longer used in the Java SDK. Special cased naming of 'protected' flag in Java code since its also a keyword in that language. (#501)
  • Loading branch information
RachelTucker authored Jan 18, 2024
1 parent 79a9a28 commit 9c94ab8
Show file tree
Hide file tree
Showing 24 changed files with 38,160 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,11 @@ public String getType() {
public String getName() {
return name;
}

public String getInternalName() {
if (name.equalsIgnoreCase("protected")) {
return name + "Flag";
}
return name;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ public enum Operation {
CANCEL_FORMAT,
CANCEL_IMPORT,
CANCEL_ONLINE,
CANCEL_TEST,
CANCEL_VERIFY,
CLEAN,
COMPACT,
DEALLOCATE,
DUMP,
EJECT,
FORMAT,
GET_PHYSICAL_PLACEMENT,
Expand All @@ -38,6 +40,7 @@ public enum Operation {
START_BULK_PUT,
START_BULK_STAGE,
START_BULK_VERIFY,
TEST,
VERIFY,
VERIFY_PHYSICAL_PLACEMENT,
VERIFY_SAFE_TO_START_BULK_PUT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public enum Resource {
JOB_CHUNK,
JOB_CHUNK_DAO,
JOB_COMPLETED_NOTIFICATION_REGISTRATION,
JOB_CREATION_FAILED,
JOB_CREATION_FAILED_NOTIFICATION_REGISTRATION,
JOB_CREATED_NOTIFICATION_REGISTRATION,
NODE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,6 @@ ImmutableList<String> commonImports(
if (RequestConverterUtil.isResourceId(ds3Request.getResource())) {
builder.add("java.util.UUID");
}
builder.add("com.google.common.net.UrlEscapers");
}

// Determine if any of the constructor parameters use the Nonnull annotation
Expand Down Expand Up @@ -447,9 +446,6 @@ static ImmutableSet<String> getImportsFromParamList(final ImmutableList<Ds3Param
&& !ds3Param.getType().equals("java.lang.String")) {
importsBuilder.add(ConvertType.toModelName(ds3Param.getType()));
}
if (ds3Param.getType().endsWith("String") || ds3Param.getType().endsWith("UUID")) {
importsBuilder.add("com.google.common.net.UrlEscapers");
}
}
return importsBuilder.build();
}
Expand Down Expand Up @@ -597,7 +593,7 @@ static String getSpectraDs3RequestPath(final Ds3Request ds3Request) {
&& ds3Request.getIncludeInPath()
&& (getNotificationType(ds3Request) == NotificationType.DELETE
|| getNotificationType(ds3Request) == NotificationType.GET)) {
builder.append("/\"").append(" + this.getNotificationId().toString()");
builder.append("/\"").append(" + this.getNotificationId()");
} else if (hasBucketNameInPath(ds3Request)) {
builder.append("/\"").append(" + this.bucketName");
} else if (isResourceAnArg(ds3Request.getResource(), ds3Request.getIncludeInPath())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public static String getModelVariable(final Element element) {
builder.append(indent(1)).append("@JacksonXmlElementWrapper(useWrapping = false)\n");
}
}
builder.append(indent(1)).append("private ").append(ResponseAndParserUtils.convertType(element)).append(" ").append(uncapFirst(element.getName()));
builder.append(indent(1)).append("private ").append(ResponseAndParserUtils.convertType(element)).append(" ").append(uncapFirst(element.getInternalName()));
if (hasContent(element.getComponentType())) {
builder.append(" = new ArrayList<>()");
}
Expand Down Expand Up @@ -239,7 +239,7 @@ public static String paramAssignmentRHS(final Arguments arg) {
if (arg.getType().equals("UUID")) {
return argToString(arg);
}
return uncapFirst(arg.getName());
return uncapFirst(arg.getInternalName());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ public String getName() {
return name;
}

public String getInternalName() {
if (name.equalsIgnoreCase("protected")) {
return name + "Flag";
}
return name;
}

public String getType() {
return type;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ public String getName() {
return name;
}

public String getInternalName() {
if (name.equalsIgnoreCase("protected")) {
return name+"Flag";
}
return name;
}

public boolean isRequired() {
return isRequired;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@

import com.spectralogic.ds3autogen.api.models.Arguments;

import static com.spectralogic.ds3autogen.java.helpers.JavaHelper.getType;
import static com.spectralogic.ds3autogen.java.helpers.JavaHelper.paramAssignmentRHS;
import static com.spectralogic.ds3autogen.java.utils.WithConstructorUtil.*;
import static com.spectralogic.ds3autogen.utils.Helper.indent;
import static com.spectralogic.ds3autogen.utils.Helper.uncapFirst;
import static com.spectralogic.ds3autogen.utils.Helper.*;

/**
* Creates a basic java with-constructor. This is used in the
Expand All @@ -39,7 +40,7 @@ public BaseWithConstructor(final Arguments param, final String requestName) {
public String toJavaCode() {
return withConstructorFirstLine(param, requestName) +
indent(2) + argAssignmentLine(param) +
indent(2) + updateQueryParamLine(param.getName(), uncapFirst(param.getName())) +
indent(2) + updateQueryParamLine(param.getName(), uncapFirst(param.getInternalName())) +
indent(2) + "return this;\n" +
indent(1) + "}\n";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public final class WithConstructorUtil {
*/
public static String withConstructorFirstLine(final Arguments arg, final String requestName) {
return indent(1) + "public " + requestName + " with" + capFirst(arg.getName()) +
"(final " + getType(arg) + " " + uncapFirst(arg.getName()) + ") {\n";
"(final " + getType(arg) + " " + uncapFirst(arg.getInternalName()) + ") {\n";
}

/**
Expand All @@ -43,7 +43,7 @@ public static String withConstructorFirstLine(final Arguments arg, final String
* Example: this.myVariable = myVariable;
*/
public static String argAssignmentLine(final Arguments arg) {
return "this." + uncapFirst(arg.getName()) + " = " + paramAssignmentRHS(arg) + ";\n";
return "this." + uncapFirst(arg.getInternalName()) + " = " + paramAssignmentRHS(arg) + ";\n";
}

/**
Expand Down Expand Up @@ -92,9 +92,6 @@ protected static String queryParamArgToString(final Arguments arg) {
|| arg.getName().equalsIgnoreCase("BucketName")) {
return uncapFirst(arg.getName());
}
if (arg.getType().endsWith("String")) {
return "UrlEscapers.urlFragmentEscaper().escape(" + uncapFirst(arg.getName()) + ").replace(\"+\", \"%2B\")";
}
return argToString(arg);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ ${javaHelper.getModelVariable(elmt)}
* Tests if two objects are equal, and handles the case if either or both objects are null
*/
protected static boolean nullableEquals(final Object obj1, final Object obj2) {
return obj1 == null && obj2 == null || obj1 != null && obj2 != null && obj1.equals(obj2);
return obj1 == null && obj2 == null || obj1 != null && obj1.equals(obj2);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package ${packageName};

import org.apache.commons.codec.binary.Base64;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

public abstract class ${name} {

Expand Down Expand Up @@ -66,7 +67,7 @@ ${javaHelper.getEnumValues(enumConstants, 2)}

@Override
public <T, E extends Throwable> T match(final MatchHandler<T, E> handler) throws E {
return handler.value(this.hash.getBytes(Charset.forName("UTF-8")));
return handler.value(this.hash.getBytes(StandardCharsets.UTF_8));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<#list elements as elmt>
public ${javaHelper.convertType(elmt)} get${elmt.getName()?cap_first}() {
return this.${elmt.getName()?uncap_first};
return this.${elmt.getInternalName()?uncap_first};
}

public void set${elmt.getName()?cap_first}(final ${javaHelper.convertType(elmt)} ${elmt.getName()?uncap_first}) {
this.${elmt.getName()?uncap_first} = ${elmt.getName()?uncap_first};
public void set${elmt.getName()?cap_first}(final ${javaHelper.convertType(elmt)} ${elmt.getInternalName()?uncap_first}) {
this.${elmt.getInternalName()?uncap_first} = ${elmt.getInternalName()?uncap_first};
}

</#list>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<#list classVariables as var>
public ${javaHelper.getType(var)} get${var.getName()?cap_first}() {
return this.${var.getName()?uncap_first};
return this.${var.getInternalName()?uncap_first};
}

</#list>
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<#list classVariables as var>
private <#if var.isRequired() == true>final </#if>${javaHelper.getType(var)} ${var.getName()?uncap_first};
private <#if var.isRequired() == true>final </#if>${javaHelper.getType(var)} ${var.getInternalName()?uncap_first};
</#list>
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import java.io.ByteArrayInputStream;
import java.io.InputStream;
import com.spectralogic.ds3client.serializer.XmlOutput;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
<#include "../imports.ftl"/>

public class ${name} extends ${parentClass} {
Expand Down Expand Up @@ -36,7 +37,7 @@ public class ${name} extends ${parentClass} {

final String xmlOutput = XmlOutput.toXml(requestPayload);

final byte[] stringBytes = xmlOutput.getBytes(Charset.forName("UTF-8"));
final byte[] stringBytes = xmlOutput.getBytes(StandardCharsets.UTF_8);
this.size = stringBytes.length;
return new ByteArrayInputStream(stringBytes);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.List;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
<#include "../imports.ftl"/>

public class ${name} extends ${parentClass} {
Expand All @@ -35,7 +36,7 @@ public class ${name} extends ${parentClass} {

${javaHelper.toXmlLine("xmlOutput", "objects", operation)}

final byte[] stringBytes = xmlOutput.getBytes(Charset.forName("UTF-8"));
final byte[] stringBytes = xmlOutput.getBytes(StandardCharsets.UTF_8);
this.size = stringBytes.length;
return new ByteArrayInputStream(stringBytes);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.charset.Charset;
import com.spectralogic.ds3client.utils.Guard;
import java.nio.charset.StandardCharsets;
<#include "../imports.ftl"/>

public class ${name} extends ${parentClass} {
Expand All @@ -27,7 +28,7 @@ public class ${name} extends ${parentClass} {
if (Guard.isStringNullOrEmpty(requestPayload)) {
return null;
}
final byte[] stringBytes = requestPayload.getBytes(Charset.forName("UTF-8"));
final byte[] stringBytes = requestPayload.getBytes(StandardCharsets.UTF_8);
this.size = stringBytes.length;
return new ByteArrayInputStream(stringBytes);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
public class JavaFunctionalTests {

private static final Logger LOG = LoggerFactory.getLogger(JavaFunctionalTests.class);
private final static GeneratedCodeLogger CODE_LOGGER = new GeneratedCodeLogger(FileTypeToLog.PARSER, LOG);
private final static GeneratedCodeLogger CODE_LOGGER = new GeneratedCodeLogger(FileTypeToLog.REQUEST, LOG);

@Rule
public TemporaryFolder tempFolder = new TemporaryFolder();
Expand Down Expand Up @@ -122,7 +122,6 @@ public void completeMultiPartUploadRequest() throws IOException, TemplateModelEx
assertTrue(hasImport("java.io.InputStream", requestGeneratedCode));
assertTrue(hasImport("com.spectralogic.ds3client.serializer.XmlOutput", requestGeneratedCode));
assertTrue(hasImport("java.util.UUID", requestGeneratedCode));
assertTrue(hasImport("com.google.common.net.UrlEscapers", requestGeneratedCode));
assertTrue(hasImport("java.nio.charset.Charset", requestGeneratedCode));
assertFalse(hasImport("com.spectralogic.ds3client.commands.AbstractRequest", requestGeneratedCode));

Expand Down Expand Up @@ -427,6 +426,16 @@ public void createGetJobRequestHandler() throws IOException, TemplateModelExcept
assertTrue(isOptParamOfType("ChunkClientProcessingOrderGuarantee", "JobChunkClientProcessingOrderGuarantee", requestName, requestGeneratedCode, true));
assertTrue(isOptParamOfType("Priority", "Priority", requestName, requestGeneratedCode, true));
assertFalse(isReqVariable("BucketName", "String", requestGeneratedCode));
assertFalse(isOptParamOfType("Protected", "boolean", requestName, requestGeneratedCode, false));
assertTrue(requestGeneratedCode.contains("private boolean protectedFlag;"));
assertTrue(requestGeneratedCode.contains("public GetBulkJobSpectraS3Request withProtected(final boolean protectedFlag) {\n" +
" this.protectedFlag = protectedFlag;\n" +
" this.updateQueryParam(\"protected\", protectedFlag);\n" +
" return this;\n" +
" }"));
assertTrue(requestGeneratedCode.contains("public boolean getProtected() {\n" +
" return this.protectedFlag;\n" +
" }"));

assertTrue(hasImport("com.spectralogic.ds3client.models.JobChunkClientProcessingOrderGuarantee", requestGeneratedCode));
assertTrue(hasImport("com.spectralogic.ds3client.BulkCommand", requestGeneratedCode));
Expand Down Expand Up @@ -1378,7 +1387,7 @@ public void deleteJobCreatedNotificationRegistrationRequestHandler() throws IOEx
CODE_LOGGER.logFile(requestGeneratedCode, FileTypeToLog.REQUEST);

assertTrue(extendsClass(requestName, "AbstractDeleteNotificationRequest", requestGeneratedCode));
assertTrue(hasPath("\"/_rest_/job_created_notification_registration/\" + this.getNotificationId().toString()", requestGeneratedCode));
assertTrue(hasPath("\"/_rest_/job_created_notification_registration/\" + this.getNotificationId()", requestGeneratedCode));
assertTrue(isOfPackage("com.spectralogic.ds3client.commands.spectrads3.notifications", requestGeneratedCode));
assertTrue(hasImport("java.util.UUID", requestGeneratedCode));
assertTrue(hasCopyright(requestGeneratedCode));
Expand Down Expand Up @@ -2435,7 +2444,6 @@ public void createMultiPartUploadPartRequest() throws IOException, TemplateModel
assertFalse(hasImport("com.spectralogic.ds3client.commands.AbstractRequest", requestGeneratedCode));
assertTrue(hasImport("com.spectralogic.ds3client.networking.HttpVerb", requestGeneratedCode));
assertTrue(hasImport("java.util.UUID", requestGeneratedCode));
assertTrue(hasImport("com.google.common.net.UrlEscapers", requestGeneratedCode));
assertTrue(hasImport("com.spectralogic.ds3client.utils.SeekableByteChannelInputStream", requestGeneratedCode));
assertTrue(hasImport("java.nio.channels.SeekableByteChannel", requestGeneratedCode));
assertTrue(hasImport("java.io.InputStream", requestGeneratedCode));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ public void isSpectraDs3_Test() {
@Test
public void getSpectraDs3RequestPath_Test() {
final Ds3Request deleteNotification = getRequestDeleteNotification();
assertThat(getSpectraDs3RequestPath(deleteNotification), is("\"/_rest_/job_created_notification_registration/\" + this.getNotificationId().toString()"));
assertThat(getSpectraDs3RequestPath(deleteNotification), is("\"/_rest_/job_created_notification_registration/\" + this.getNotificationId()"));

final Ds3Request createNotification = getRequestCreateNotification();
assertThat(getSpectraDs3RequestPath(createNotification), is("\"/_rest_/job_created_notification_registration\""));

final Ds3Request getNotification = getRequestGetNotification();
assertThat(getSpectraDs3RequestPath(getNotification), is("\"/_rest_/job_completed_notification_registration/\" + this.getNotificationId().toString()"));
assertThat(getSpectraDs3RequestPath(getNotification), is("\"/_rest_/job_completed_notification_registration/\" + this.getNotificationId()"));

final Ds3Request verifyPhysicalPlacement = getRequestVerifyPhysicalPlacement();
assertThat(getSpectraDs3RequestPath(verifyPhysicalPlacement), is("\"/_rest_/bucket/\" + this.bucketName"));
Expand Down Expand Up @@ -319,10 +319,9 @@ public void getImportsFromParamList_FullList_Test() {
new Ds3Param("Name", "java.lang.String", true));

final ImmutableSet<String> result = getImportsFromParamList(params);
assertThat(result.size(), is(3));
assertThat(result.size(), is(2));
assertTrue(result.contains("java.util.UUID"));
assertTrue(result.contains("com.spectralogic.ds3client.models.BlobStoreTaskPriority"));
assertTrue(result.contains("com.google.common.net.UrlEscapers"));
}

@Test
Expand All @@ -347,13 +346,12 @@ public void getAllImports_Test() {
generator.toConstructorList(request, "", new Ds3DocSpecEmptyImpl()));

assertThat(result)
.hasSize(5)
.hasSize(4)
.contains(
"com.spectralogic.ds3client.commands.interfaces.AbstractRequest",
"com.spectralogic.ds3client.models.JobRequestType",
"com.spectralogic.ds3client.models.BlobStoreTaskPriority",
"java.util.UUID",
"com.google.common.net.UrlEscapers");
"java.util.UUID");
}

@Test
Expand Down Expand Up @@ -756,7 +754,6 @@ public void commonImportsTest() {
final ImmutableList<String> expected = ImmutableList.of(
"com.spectralogic.ds3client.commands.interfaces.AbstractRequest",
"java.util.UUID",
"com.google.common.net.UrlEscapers",
"com.google.common.base.Preconditions",
"javax.annotation.Nonnull");

Expand Down
Loading

0 comments on commit 9c94ab8

Please sign in to comment.