Skip to content

Commit

Permalink
Merge pull request #113 from autermann/fix/112
Browse files Browse the repository at this point in the history
Fix NullPointerException when requesting the service identification with the default locale
  • Loading branch information
autermann authored Mar 17, 2020
2 parents ca1b491 + bd39d54 commit 943700d
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -176,37 +176,28 @@ public Set<Locale> getAvailableLocales() {
}

private MultilingualString getAbstract(Locale locale) {
if (this.abstrakt.hasLocale(locale)) {
return this.abstrakt.filter(locale, defaultLocale, showAllLanguageValues);
} else {
MultilingualString multilingualString = new MultilingualString();
for (Locale eqLocale : LocaleHelper.getEquivalents(locale)) {
if (this.abstrakt.hasLocale(eqLocale)) {
for (LocalizedString ls : this.abstrakt.filter(eqLocale, defaultLocale, showAllLanguageValues)) {
multilingualString.addLocalization(new LocalizedString(locale, ls.getText()));
}
return multilingualString;
}
}
}
return this.abstrakt.filter(locale, defaultLocale, showAllLanguageValues);
return filter(this.abstrakt, locale);
}

private MultilingualString getTitle(Locale locale) {
if (this.title.hasLocale(locale)) {
return this.title.filter(locale, defaultLocale, showAllLanguageValues);
} else {
return filter(this.title, locale);
}

private MultilingualString filter(MultilingualString ms, Locale locale) {
if (ms.hasLocale(locale)) {
return ms.filter(locale, defaultLocale, showAllLanguageValues);
} else if (locale != null) {
MultilingualString multilingualString = new MultilingualString();
for (Locale eqLocale : LocaleHelper.getEquivalents(locale)) {
if (this.title.hasLocale(eqLocale)) {
for (LocalizedString ls : this.title.filter(eqLocale, defaultLocale, showAllLanguageValues)) {
if (ms.hasLocale(eqLocale)) {
for (LocalizedString ls : ms.filter(eqLocale, defaultLocale, showAllLanguageValues)) {
multilingualString.addLocalization(new LocalizedString(locale, ls.getText()));
}
return multilingualString;
}
}
}
return this.title.filter(locale, defaultLocale, showAllLanguageValues);
return ms.filter(locale, defaultLocale, showAllLanguageValues);
}

private Set<String> getContraints() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package org.n52.iceland.ogc.ows;

import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.n52.iceland.service.operator.ServiceOperatorRepository;
import org.n52.janmayen.i18n.MultilingualString;
import org.n52.shetland.ogc.ows.OwsServiceIdentification;

import java.util.Collections;
import java.util.Locale;

import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.matchesPattern;
import static org.hamcrest.Matchers.notNullValue;

class OwsServiceIdentificationFactoryTest {

public static final String SERVICE = "service";

@Test
void testNullLocale() {
ServiceOperatorRepository serviceOperatorRepository = Mockito.mock(ServiceOperatorRepository.class);
Mockito.when(serviceOperatorRepository.getSupportedVersions(SERVICE))
.thenReturn(Collections.singleton("1.0.0"));

OwsServiceIdentificationFactory factory = new OwsServiceIdentificationFactory(SERVICE, serviceOperatorRepository);

MultilingualString title = new MultilingualString();
title.addLocalization("en", "Title");
title.addLocalization("de", "Titel");
factory.setTitle(title);

MultilingualString description = new MultilingualString();
description.addLocalization("en", "Description");
description.addLocalization("de", "Beschreibung");
factory.setAbstract(description);

assertThat(factory.create(null), is(notNullValue()));
assertThat(factory.create(Locale.ENGLISH), is(notNullValue()));
assertThat(factory.create(Locale.ENGLISH).getTitle().get().getLocales(), is(Matchers.contains(Locale.ENGLISH)));
assertThat(factory.create(Locale.GERMAN), is(notNullValue()));
assertThat(factory.create(Locale.GERMAN).getTitle().get().getLocales(), is(Matchers.contains(Locale.GERMAN)));
assertThat(factory.create(Locale.CHINESE), is(notNullValue()));
assertThat(factory.create(Locale.CHINESE).getTitle().get().getLocales(), is(Matchers.contains(Locale.ENGLISH)));
}
}

0 comments on commit 943700d

Please sign in to comment.