Skip to content
This repository was archived by the owner on Jun 28, 2022. It is now read-only.

Add ConfigV2 Validator #2672

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/* Copyright 2019 Google LLC
*
* 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
*
* https://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 com.google.api.codegen.util;

import com.google.protobuf.DiscardUnknownFieldsParser;
import com.google.protobuf.InvalidProtocolBufferException;
import com.google.protobuf.Parser;
import java.util.Arrays;
import javax.annotation.Nonnull;

public class ConfigVersionValidator {

public static String CONFIG_V2_MAJOR_VERSION = "2";
public static String CONFIG_V2_VERSION = CONFIG_V2_MAJOR_VERSION + ".0.0"; // "2.0.0"

/**
* Throw {@link IllegalStateException} iff the given input contains fields unknown to the {@link
* com.google.api.codegen.v2.ConfigProto} schema.
*/
public void validateV2Config(@Nonnull com.google.api.codegen.ConfigProto configV1Proto)
throws IllegalStateException {
if (!configV1Proto.getConfigSchemaVersion().startsWith(CONFIG_V2_MAJOR_VERSION + ".")
&& !configV1Proto.getConfigSchemaVersion().equals(CONFIG_V2_MAJOR_VERSION)) {
throw new IllegalStateException(
String.format(
"Provided ConfigProto version is %s but should be >= %s",
configV1Proto.getConfigSchemaVersion(), CONFIG_V2_VERSION));
}

try {
// Serialize and deserialize the Config v1 proto under the Config v2 schema to remove fields
// unknown to Config v2 schema.
Parser<com.google.api.codegen.v2.ConfigProto> parser =
DiscardUnknownFieldsParser.wrap(com.google.api.codegen.v2.ConfigProto.parser());
com.google.api.codegen.v2.ConfigProto configV2 =
parser.parseFrom(configV1Proto.toByteString());

// Compare the v1-serialized and v2-serialized strings of the same config proto object.
if (!Arrays.equals(configV2.toByteArray(), configV1Proto.toByteArray())) {
throw new IllegalStateException(
String.format(
"Unknown fields to ConfigProto v2 in configProto: %s", configV1Proto.toString()));
}
} catch (InvalidProtocolBufferException e) {
throw new IllegalStateException(e);
}
}
}
66 changes: 66 additions & 0 deletions src/test/java/com/google/api/codegen/util/ConfigValidatorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/* Copyright 2019 Google LLC
*
* 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
*
* https://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 com.google.api.codegen.util;

import com.google.api.codegen.ConfigProto;
import com.google.api.codegen.FlatteningConfigProto;
import com.google.api.codegen.FlatteningGroupProto;
import com.google.api.codegen.InterfaceConfigProto;
import com.google.api.codegen.MethodConfigProto;
import com.google.protobuf.BoolValue;
import org.junit.Test;

public class ConfigValidatorTest {
private static final ConfigProto validV2Proto =
ConfigProto.newBuilder()
.setConfigSchemaVersion("2.0.0")
// This is a valid field for both Config v1 and Config v2.
.setEnableStringFormatFunctionsOverride(BoolValue.of(true))
.build();

private static final ConfigVersionValidator validator = new ConfigVersionValidator();

@Test(expected = IllegalStateException.class)
public void testProtoIsNotConfigNextVersion() {
ConfigProto smallProto = validV2Proto.toBuilder().setConfigSchemaVersion("1.0.0").build();
validator.validateV2Config(smallProto);
}

@Test(expected = IllegalStateException.class)
public void testProtoIsNotValid() {
ConfigProto smallProto =
validV2Proto
.toBuilder()
.addInterfaces(
InterfaceConfigProto.newBuilder()
.addMethods(
MethodConfigProto.newBuilder()
.setFlattening(
FlatteningConfigProto.newBuilder()
.addGroups(
FlatteningGroupProto.newBuilder()
.addParameters("flattenedParam")
.build())
.build()))
.build())
.build();
validator.validateV2Config(smallProto);
}

@Test
public void testProtoIsValid() {
validator.validateV2Config(validV2Proto);
}
}