|
| 1 | +// Copyright 2023-2025 Buf Technologies, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package build.buf.protovalidate; |
| 16 | + |
| 17 | +import static org.assertj.core.api.Assertions.assertThat; |
| 18 | + |
| 19 | +import build.buf.validate.Violation; |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | + |
| 22 | +public class Issue427Test { |
| 23 | + @Test |
| 24 | + public void testMessageOneofWithNameOnly() throws Exception { |
| 25 | + com.example.imports.validationtest.Issue427 msg = |
| 26 | + com.example.imports.validationtest.Issue427.newBuilder().setName("foo").build(); |
| 27 | + Validator validator = ValidatorFactory.newBuilder().build(); |
| 28 | + assertThat(validator.validate(msg).toProto().getViolationsList()).isEmpty(); |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + public void testMessageOneofWithTagsOnly() throws Exception { |
| 33 | + com.example.imports.validationtest.Issue427 msg = |
| 34 | + com.example.imports.validationtest.Issue427.newBuilder().addTags("a").addTags("b").build(); |
| 35 | + Validator validator = ValidatorFactory.newBuilder().build(); |
| 36 | + assertThat(validator.validate(msg).toProto().getViolationsList()).isEmpty(); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + public void testMessageOneofWithMappingsOnly() throws Exception { |
| 41 | + com.example.imports.validationtest.Issue427 msg = |
| 42 | + com.example.imports.validationtest.Issue427.newBuilder().putMappings("k", "v").build(); |
| 43 | + Validator validator = ValidatorFactory.newBuilder().build(); |
| 44 | + assertThat(validator.validate(msg).toProto().getViolationsList()).isEmpty(); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void testMessageOneofNoneSet() throws Exception { |
| 49 | + com.example.imports.validationtest.Issue427 msg = |
| 50 | + com.example.imports.validationtest.Issue427.getDefaultInstance(); |
| 51 | + Validator validator = ValidatorFactory.newBuilder().build(); |
| 52 | + assertThat(validator.validate(msg).toProto().getViolationsList()) |
| 53 | + .containsExactly( |
| 54 | + Violation.newBuilder() |
| 55 | + .setRuleId("message.oneof") |
| 56 | + .setMessage("one of name, tags, mappings must be set") |
| 57 | + .build()); |
| 58 | + } |
| 59 | +} |
0 commit comments