This repository was archived by the owner on Jun 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 129
Add ConfigV2 Validator #2672
Merged
andreamlin
merged 8 commits into
googleapis:gapic_config_v2
from
andreamlin:validate_config_v2
Apr 1, 2019
Merged
Add ConfigV2 Validator #2672
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8212282
add configv2 validator
andreamlin 98fe47a
javadoc
andreamlin fe7464a
ConfigVersionValidator
andreamlin ab64503
validateV2Config
andreamlin 9e5281a
Merge branch 'gapic_config_v2' into validate_config_v2
andreamlin 56b0d28
use DiscardUnkonwnFieldsParser
andreamlin 617fc9a
cleanup
andreamlin 43a9a6c
cleanup
andreamlin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
src/main/java/com/google/api/codegen/util/ConfigVersionValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* 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 in to ConfigProto v2 in configProto: %s", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. s/in to ConfigProto/to ConfigProto/ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
configV1Proto.toString())); | ||
} | ||
} catch (InvalidProtocolBufferException e) { | ||
throw new IllegalStateException(e); | ||
} | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
src/test/java/com/google/api/codegen/util/ConfigValidatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: remove extra line
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done