View Javadoc
1   /*
2    * Copyright 2019-2022 the original author or authors.
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  
17  package org.bremersee.gpx;
18  
19  import static org.junit.jupiter.api.Assertions.assertInstanceOf;
20  import static org.junit.jupiter.api.Assertions.assertNotNull;
21  
22  import java.io.StringReader;
23  import java.io.StringWriter;
24  import java.math.BigDecimal;
25  import java.util.List;
26  import java.util.Optional;
27  import java.util.ServiceLoader;
28  import javax.xml.datatype.XMLGregorianCalendar;
29  import org.assertj.core.api.SoftAssertions;
30  import org.assertj.core.api.junit.jupiter.SoftAssertionsExtension;
31  import org.bremersee.garmin.creationtime.v1.model.ext.CreationTimeExtension;
32  import org.bremersee.garmin.gpx.v3.model.ext.AddressT;
33  import org.bremersee.garmin.gpx.v3.model.ext.CategoriesT;
34  import org.bremersee.garmin.gpx.v3.model.ext.WaypointExtension;
35  import org.bremersee.gpx.model.ExtensionsType;
36  import org.bremersee.gpx.model.Gpx;
37  import org.bremersee.gpx.model.LinkType;
38  import org.bremersee.gpx.model.WptType;
39  import org.bremersee.xml.JaxbContextBuilder;
40  import org.bremersee.xml.JaxbContextDataProvider;
41  import org.bremersee.xml.SchemaMode;
42  import org.junit.jupiter.api.BeforeAll;
43  import org.junit.jupiter.api.Test;
44  import org.junit.jupiter.api.extension.ExtendWith;
45  import org.springframework.core.io.DefaultResourceLoader;
46  import org.springframework.core.io.ResourceLoader;
47  
48  /**
49   * The jaxb context builder test.
50   *
51   * @author Christian Bremer
52   */
53  @ExtendWith(SoftAssertionsExtension.class)
54  class JaxbContextBuilderTest {
55  
56    private static final ResourceLoader RESOURCE_LOADER = new DefaultResourceLoader();
57  
58    private static JaxbContextBuilder jaxbContextBuilder;
59  
60    /**
61     * Create jaxb context builder.
62     */
63    @BeforeAll
64    static void createJaxbContextBuilder() {
65      jaxbContextBuilder = JaxbContextBuilder.newInstance()
66          .withSchemaMode(SchemaMode.NEVER)
67          .processAll(ServiceLoader.load(JaxbContextDataProvider.class))
68          .initJaxbContext();
69    }
70  
71    private static Object unmarshalClassPathResource(final String classPathResource)
72        throws Exception {
73      return jaxbContextBuilder
74          .buildUnmarshaller()
75          .unmarshal(RESOURCE_LOADER.getResource(classPathResource).getInputStream());
76    }
77  
78    /**
79     * Test gpx with wpt.
80     *
81     * @param softly the soft assertions
82     * @throws Exception the exception
83     */
84    @Test
85    void testGpxWithWpt(SoftAssertions softly) throws Exception {
86      CategoriesT categories = new CategoriesT();
87      categories.getCategories().add("JUNIT");
88  
89      AddressT address = new AddressT();
90      address.setCountry("Italy");
91      address.setCountry("Rom");
92  
93      LinkType link = new LinkType();
94      link.setHref("http://localhost");
95  
96      WaypointExtension waypointExtension = new WaypointExtension();
97      waypointExtension.setCategories(categories);
98      waypointExtension.setAddress(address);
99  
100     ExtensionsType extensionsType = ExtensionsTypeBuilder.newInstance()
101         .addElement(waypointExtension, jaxbContextBuilder.buildMarshaller(waypointExtension))
102         .build(true);
103 
104     WptType wpt = new WptType();
105     wpt.setLat(new BigDecimal("52.4"));
106     wpt.setLon(new BigDecimal("10.8"));
107     wpt.setSrc("test");
108     wpt.getLinks().add(link);
109     wpt.setExtensions(extensionsType);
110 
111     Gpx gpx = new Gpx();
112     gpx.setCreator("org.bremersee");
113     gpx.setVersion("1.1");
114     gpx.getWpts().add(wpt);
115 
116     StringWriter sw = new StringWriter();
117     jaxbContextBuilder.buildMarshaller(gpx).marshal(gpx, sw);
118     String xml = sw.toString();
119     softly.assertThat(xml).isNotNull();
120 
121     Gpx readGpx = (Gpx) jaxbContextBuilder.buildUnmarshaller(Gpx.class)
122         .unmarshal(new StringReader(xml));
123     softly.assertThat(readGpx).isNotNull();
124     softly.assertThat(readGpx.getWpts()).isNotEmpty();
125 
126     WptType readWpt = readGpx.getWpts().get(0);
127     softly.assertThat(readWpt.getLat()).isEqualTo(wpt.getLat());
128     softly.assertThat(readWpt.getLon()).isEqualTo(wpt.getLon());
129     softly.assertThat(readWpt.getSrc()).isEqualTo(wpt.getSrc());
130   }
131 
132   /**
133    * Test address data.
134    *
135    * @param softly the soft assertions
136    * @throws Exception the exception
137    */
138   @Test
139   void testAddressData(SoftAssertions softly) throws Exception {
140     final Object obj = unmarshalClassPathResource("classpath:Adresse.GPX");
141     softly.assertThat(obj).isInstanceOf(Gpx.class);
142 
143     final Gpx gpx = (Gpx) obj;
144     softly.assertThat(gpx.getWpts()).isNotEmpty();
145 
146     final WptType wpt = gpx.getWpts().get(0);
147     softly.assertThat(wpt.getExtensions()).isNotNull();
148 
149     final ExtensionsType extensions = wpt.getExtensions();
150     softly.assertThat(extensions.getAnies()).isNotEmpty();
151 
152     final List<WaypointExtension> waypointExtensions = GpxJaxbContextHelper.findExtensions(
153         WaypointExtension.class,
154         true,
155         GpxJaxbContextHelper.parseExtensions(extensions, jaxbContextBuilder.buildJaxbContext()));
156     softly.assertThat(waypointExtensions).isNotEmpty();
157 
158     WaypointExtension wptExt = waypointExtensions.get(0);
159     softly.assertThat(wptExt).isNotNull();
160     softly.assertThat(wptExt.getAddress()).isNotNull();
161     softly.assertThat(wptExt.getAddress().getStreetAddresses()).isNotEmpty();
162     softly.assertThat(wptExt.getAddress().getStreetAddresses()).contains("Seerosenweg 1");
163 
164     Optional<WaypointExtension> optionalWaypointExtension = GpxJaxbContextHelper
165         .findFirstExtension(
166             WaypointExtension.class,
167             true,
168             extensions,
169             jaxbContextBuilder.buildUnmarshaller());
170 
171     softly.assertThat(optionalWaypointExtension)
172         .map(WaypointExtension::getAddress)
173         .map(AddressT::getStreetAddresses)
174         .contains(List.of("Seerosenweg 1"));
175   }
176 
177   /**
178    * Test picture data.
179    *
180    * @param softly the soft assertions
181    * @throws Exception the exception
182    */
183   @Test
184   void testPictureData(SoftAssertions softly) throws Exception {
185     final Object obj = unmarshalClassPathResource("classpath:Bild.GPX");
186     softly.assertThat(obj).isInstanceOf(Gpx.class);
187 
188     final Gpx gpx = (Gpx) obj;
189     softly.assertThat(gpx.getWpts()).isNotEmpty();
190 
191     final WptType wpt = gpx.getWpts().get(0);
192     softly.assertThat(wpt.getExtensions()).isNotNull();
193 
194     final ExtensionsType extensions = wpt.getExtensions();
195     softly.assertThat(extensions.getAnies()).isNotEmpty();
196 
197     Optional<CreationTimeExtension> cr = GpxJaxbContextHelper.findFirstExtension(
198         CreationTimeExtension.class, true, extensions, jaxbContextBuilder.buildJaxbContext());
199 
200     softly.assertThat(cr)
201         .map(CreationTimeExtension::getCreationTime)
202         .map(XMLGregorianCalendar::getYear)
203         .get()
204         .isEqualTo(2012);
205   }
206 
207   /**
208    * Test route.
209    *
210    * @param softly the soft assertions
211    * @throws Exception the exception
212    */
213   @Test
214   void testRoute(SoftAssertions softly) throws Exception {
215     Object obj = unmarshalClassPathResource("classpath:Route.GPX");
216     softly.assertThat(obj).isInstanceOf(Gpx.class);
217 
218     StringWriter sw = new StringWriter();
219     jaxbContextBuilder.buildMarshaller(obj).marshal(obj, sw);
220     String xml = sw.toString();
221     softly.assertThat(xml).containsIgnoringCase("<gpx");
222   }
223 
224   /**
225    * Test track.
226    *
227    * @param softly the soft assertions
228    * @throws Exception the exception
229    */
230   @Test
231   void testTrack(SoftAssertions softly) throws Exception {
232     Object obj = unmarshalClassPathResource("classpath:Track.GPX");
233     assertNotNull(obj);
234     assertInstanceOf(Gpx.class, obj);
235 
236     StringWriter sw = new StringWriter();
237     jaxbContextBuilder.buildMarshaller(obj).marshal(obj, sw);
238     String xml = sw.toString();
239     softly.assertThat(xml).containsIgnoringCase("<gpx");
240   }
241 
242 }