1+ /**
2+ * Copyright (C) 2006-2025 Talend Inc. - www.talend.com
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 org .talend .sdk .component .server .front ;
17+
18+ import static org .junit .jupiter .api .Assertions .assertEquals ;
19+ import static org .junit .jupiter .api .Assertions .assertFalse ;
20+ import static org .junit .jupiter .api .Assertions .assertNull ;
21+ import static org .junit .jupiter .api .Assertions .assertTrue ;
22+
23+ import java .util .LinkedHashMap ;
24+ import java .util .Map ;
25+
26+ import javax .json .bind .Jsonb ;
27+ import javax .json .bind .JsonbBuilder ;
28+
29+ import com .fasterxml .jackson .databind .ObjectMapper ;
30+
31+ import org .junit .jupiter .api .Test ;
32+ import org .talend .sdk .component .api .record .SchemaProperty ;
33+ import org .talend .sdk .component .api .record .SchemaProperty .LogicalType ;
34+ import org .talend .sdk .component .runtime .record .RecordBuilderFactoryImpl ;
35+ import org .talend .sdk .component .server .front .model .Entry ;
36+ import org .talend .sdk .component .server .front .model .Schema ;
37+
38+ /**
39+ * Unit tests for {@link Entry}.
40+ */
41+ class EntryTest {
42+
43+ private Entry createValidEntry () {
44+ Map <String , String > props = new LinkedHashMap <>(0 );
45+ props .put ("p1" , "v1" );
46+ return new Entry ("name" , "raw" , Schema .Type .STRING , true , false , true ,
47+ true , null , "comment" , props , "default" );
48+ }
49+
50+ private Entry createEmptyEntry () {
51+ Map <String , String > props = new LinkedHashMap <>(0 );
52+ props .put ("p1" , "v1" );
53+ return new Entry ("name" , null , Schema .Type .STRING , true , false , true ,
54+ true , null , null , props , null );
55+ }
56+
57+ // ----------------------------------------------------------------------
58+ // Builder
59+ // ----------------------------------------------------------------------
60+
61+ @ Test
62+ void builderCreatesValidEntry () {
63+ Entry entry = createValidEntry ();
64+
65+ assertEquals ("name" , entry .getName ());
66+ assertEquals ("raw" , entry .getRawName ());
67+ assertEquals ("raw" , entry .getOriginalFieldName ());
68+ assertEquals (Schema .Type .STRING , entry .getType ());
69+ assertTrue (entry .isNullable ());
70+ assertFalse (entry .isMetadata ());
71+ assertTrue (entry .isErrorCapable ());
72+ assertTrue (entry .isValid ());
73+ assertEquals ("comment" , entry .getComment ());
74+ assertEquals ("default" , entry .getDefaultValue ());
75+ assertEquals ("v1" , entry .getProps ().get ("p1" ));
76+ }
77+
78+ // ----------------------------------------------------------------------
79+ // Accessors
80+ // ----------------------------------------------------------------------
81+
82+ @ Test
83+ void getDefaultValueIsTyped () {
84+ Entry entry = createValidEntry ();
85+
86+ String value = entry .getDefaultValue ();
87+ assertEquals ("default" , value );
88+ }
89+
90+ @ Test
91+ void getDefaultValueEmpty () {
92+ Entry entry = createEmptyEntry ();
93+
94+ String value = entry .getDefaultValue ();
95+ assertNull (value );
96+ }
97+
98+ @ Test
99+ void getPropReturnsProperty () {
100+ Entry entry = createValidEntry ();
101+ assertEquals ("v1" , entry .getProp ("p1" ));
102+ assertNull (entry .getProp ("k1" ));
103+ }
104+
105+ // ----------------------------------------------------------------------
106+ // JSON deserialization
107+ // ----------------------------------------------------------------------
108+
109+ @ Test
110+ void deserializeEntryFromJson () throws Exception {
111+ RecordBuilderFactoryImpl factory = new RecordBuilderFactoryImpl ("test" );
112+ org .talend .sdk .component .api .record .Schema .Entry entryImpl = factory .newEntryBuilder ()
113+ .withName ("éèfield" )
114+ .withLogicalType (LogicalType .UUID )
115+ .withNullable (false )
116+ .withMetadata (false )
117+ .withErrorCapable (false )
118+ .withComment ("test comment" )
119+ .withProps (Map .of ("p1" , "v1" ))
120+ .withDefaultValue ("defaultValue" )
121+ .build ();
122+
123+ try (Jsonb jsonb = JsonbBuilder .create ()) {
124+ String json = jsonb .toJson (entryImpl );
125+
126+ ObjectMapper mapper = new ObjectMapper ();
127+ Entry entry = mapper .readValue (json , Entry .class );
128+
129+ assertEquals ("_field" , entry .getName ());
130+ assertEquals ("éèfield" , entry .getRawName ());
131+ assertEquals ("éèfield" , entry .getOriginalFieldName ());
132+ assertEquals (Schema .Type .STRING , entry .getType ());
133+ assertEquals (LogicalType .UUID .key (), entry .getProp (SchemaProperty .LOGICAL_TYPE ));
134+
135+ assertFalse (entry .isNullable ());
136+ assertFalse (entry .isMetadata ());
137+ assertFalse (entry .isErrorCapable ());
138+ assertTrue (entry .isValid ());
139+
140+ assertEquals ("test comment" , entry .getComment ());
141+ assertEquals ("v1" , entry .getProps ().get ("p1" ));
142+ assertEquals ("defaultValue" , entry .getDefaultValue ());
143+ }
144+ }
145+
146+ }
0 commit comments