|
| 1 | + |
| 2 | +package cz.habarta.typescript.generator; |
| 3 | + |
| 4 | +//import com.fasterxml.jackson.annotation.JsonInclude; |
| 5 | +//import com.fasterxml.jackson.databind.ObjectMapper; |
| 6 | +//import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; |
| 7 | +//import java.util.Objects; |
| 8 | +//import java.util.Optional; |
| 9 | +//import org.junit.Assert; |
| 10 | +//import org.junit.Test; |
| 11 | + |
| 12 | + |
| 13 | +// Java 8 is required for this test |
| 14 | +// TODO uncomment on Java 8 |
| 15 | + |
| 16 | +public class OptionalTest { |
| 17 | + |
| 18 | +// @Test |
| 19 | +// public void test() { |
| 20 | +// final String output = new TypeScriptGenerator(TestUtils.settings()).generateTypeScript(Input.from(Person.class)); |
| 21 | +// Assert.assertEquals( |
| 22 | +// "interface Person {\n" + |
| 23 | +// " name: string;\n" + |
| 24 | +// " email: string;\n" + |
| 25 | +// "}", |
| 26 | +// output.trim()); |
| 27 | +// } |
| 28 | +// |
| 29 | +// @Test |
| 30 | +// public void testJackson2OptionalSupport() throws Exception { |
| 31 | +// final ObjectMapper objectMapper = new ObjectMapper() |
| 32 | +// .registerModule(new Jdk8Module()) |
| 33 | +// .setSerializationInclusion(JsonInclude.Include.NON_NULL); |
| 34 | +// |
| 35 | +// final Person personWithEmail = new Person("afh", Optional.of("[email protected]")); |
| 36 | +// final Person personWithEmptyEmail = new Person("afh", Optional.<String>empty()); |
| 37 | +// final Person personWithoutEmail = new Person("afh", null); |
| 38 | +// |
| 39 | +// final String jsonWithEmail = "{'name':'afh','email':'[email protected]'}".replace('\'', '\"'); |
| 40 | +// final String jsonWithNullEmail = "{'name':'afh','email':null}".replace('\'', '\"'); |
| 41 | +// final String jsonWithoutEmail = "{'name':'afh'}".replace('\'', '\"'); |
| 42 | +// |
| 43 | +// Assert.assertEquals(jsonWithEmail, objectMapper.writeValueAsString(personWithEmail)); |
| 44 | +// Assert.assertEquals(jsonWithNullEmail, objectMapper.writeValueAsString(personWithEmptyEmail)); |
| 45 | +// Assert.assertEquals(jsonWithoutEmail, objectMapper.writeValueAsString(personWithoutEmail)); |
| 46 | +// |
| 47 | +// Assert.assertEquals(personWithEmail, objectMapper.readValue(jsonWithEmail, Person.class)); |
| 48 | +// Assert.assertEquals(personWithEmptyEmail, objectMapper.readValue(jsonWithNullEmail, Person.class)); |
| 49 | +// Assert.assertEquals(personWithoutEmail, objectMapper.readValue(jsonWithoutEmail, Person.class)); |
| 50 | +// } |
| 51 | +// |
| 52 | +// private static class Person { |
| 53 | +// public String name; |
| 54 | +// public Optional<String> email; |
| 55 | +// |
| 56 | +// public Person() { |
| 57 | +// } |
| 58 | +// |
| 59 | +// public Person(String name, Optional<String> email) { |
| 60 | +// this.name = name; |
| 61 | +// this.email = email; |
| 62 | +// } |
| 63 | +// |
| 64 | +// @Override |
| 65 | +// public int hashCode() { |
| 66 | +// int hash = 7; |
| 67 | +// hash = 53 * hash + Objects.hashCode(this.name); |
| 68 | +// hash = 53 * hash + Objects.hashCode(this.email); |
| 69 | +// return hash; |
| 70 | +// } |
| 71 | +// |
| 72 | +// @Override |
| 73 | +// public boolean equals(Object obj) { |
| 74 | +// if (this == obj) { |
| 75 | +// return true; |
| 76 | +// } |
| 77 | +// if (obj == null) { |
| 78 | +// return false; |
| 79 | +// } |
| 80 | +// if (getClass() != obj.getClass()) { |
| 81 | +// return false; |
| 82 | +// } |
| 83 | +// final Person other = (Person) obj; |
| 84 | +// if (!Objects.equals(this.name, other.name)) { |
| 85 | +// return false; |
| 86 | +// } |
| 87 | +// if (!Objects.equals(this.email, other.email)) { |
| 88 | +// return false; |
| 89 | +// } |
| 90 | +// return true; |
| 91 | +// } |
| 92 | +// |
| 93 | +// @Override |
| 94 | +// public String toString() { |
| 95 | +// return "Person{" + "name=" + name + ", email=" + email + '}'; |
| 96 | +// } |
| 97 | +// } |
| 98 | + |
| 99 | +} |
0 commit comments