|
| 1 | +/* |
| 2 | + * Copyright (C) 2015 Red Hat, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.fabric8.kubernetes.model.jackson; |
| 17 | + |
| 18 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 19 | +import com.fasterxml.jackson.databind.exc.InvalidFormatException; |
| 20 | +import lombok.Data; |
| 21 | +import org.junit.jupiter.api.BeforeEach; |
| 22 | +import org.junit.jupiter.api.Nested; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | +import org.junit.jupiter.api.TestInstance; |
| 25 | +import org.junit.jupiter.params.ParameterizedTest; |
| 26 | +import org.junit.jupiter.params.provider.Arguments; |
| 27 | +import org.junit.jupiter.params.provider.MethodSource; |
| 28 | + |
| 29 | +import java.util.stream.Stream; |
| 30 | + |
| 31 | +import static org.assertj.core.api.Assertions.assertThat; |
| 32 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 33 | +import static org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS; |
| 34 | + |
| 35 | +class GoIntegerDeserializerTest { |
| 36 | + |
| 37 | + private ObjectMapper context; |
| 38 | + |
| 39 | + @BeforeEach |
| 40 | + void setUp() { |
| 41 | + context = new ObjectMapper(); |
| 42 | + context.registerModule(new GoCompatibilityModule()); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + void defaultConstructorDoesntParseOctals() throws Exception { |
| 47 | + final Integer result = new GoIntegerDeserializer() |
| 48 | + .deserialize(context.createParser("\"0555\""), context.getDeserializationContext()); |
| 49 | + assertThat(result).isEqualTo(555); |
| 50 | + } |
| 51 | + |
| 52 | + @Nested |
| 53 | + @TestInstance(PER_CLASS) |
| 54 | + class Applicable { |
| 55 | + |
| 56 | + @ParameterizedTest(name = "{index}: with '{'\"{0}\": {1}'}' parses as {2}") |
| 57 | + @MethodSource |
| 58 | + void parsesOctals(String fieldName, String content, Integer expected) throws Exception { |
| 59 | + final IntegerFieldsContainer result = context |
| 60 | + .readValue(String.format("{\"%s\": %s}", fieldName, content), IntegerFieldsContainer.class); |
| 61 | + assertThat(result).hasFieldOrPropertyWithValue(fieldName, expected); |
| 62 | + } |
| 63 | + |
| 64 | + private Stream<Arguments> parsesOctals() { |
| 65 | + return Stream.of("mode", "defaultMode") |
| 66 | + .flatMap(field -> Stream.of( |
| 67 | + Arguments.of(field, "null", null), |
| 68 | + Arguments.of(field, "\"0555\"", 365), |
| 69 | + Arguments.of(field, "\"0o555\"", 365), |
| 70 | + Arguments.of(field, "\"0O555\"", 365), |
| 71 | + Arguments.of(field, "\"555\"", 555), |
| 72 | + Arguments.of(field, "\"0888\"", 888), |
| 73 | + Arguments.of(field, "\"0o12\"", 10), |
| 74 | + Arguments.of(field, "\"0O12\"", 10))); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + // |
| 79 | + @Nested |
| 80 | + class NotApplicable { |
| 81 | + |
| 82 | + @Test |
| 83 | + void parsesOctalsAsDecimal() throws Exception { |
| 84 | + final IntegerFieldsContainer result = context |
| 85 | + .readValue("{\"notApplicable\": \"0555\"}", IntegerFieldsContainer.class); |
| 86 | + assertThat(result).hasFieldOrPropertyWithValue("notApplicable", 555); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + void throwsExceptionForInvalidOctal() { |
| 91 | + assertThatThrownBy(() -> context.readValue("{\"mode\": \"0o955\"}", IntegerFieldsContainer.class)) |
| 92 | + .isInstanceOf(InvalidFormatException.class); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + void throwsExceptionForOctalWithSeparator() { |
| 97 | + assertThatThrownBy(() -> context.readValue("{\"notApplicable\": \"0o555\"}", IntegerFieldsContainer.class)) |
| 98 | + .isInstanceOf(InvalidFormatException.class); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + @Data |
| 103 | + private static final class IntegerFieldsContainer { |
| 104 | + private Integer mode; |
| 105 | + private Integer defaultMode; |
| 106 | + private Integer notApplicable; |
| 107 | + } |
| 108 | +} |
0 commit comments