From db9fc6f4fdd1af2f05b5ca85b7330c4d0f0b0012 Mon Sep 17 00:00:00 2001 From: EHJ-52n Date: Mon, 30 Oct 2017 16:32:33 +0100 Subject: [PATCH 1/2] Fix NPE in Capabilities decoding when service identification is missing --- .../AbstractCapabilitiesBaseTypeDecoder.java | 3 ++ ...stractCapabilitiesBaseTypeDecoderTest.java | 52 +++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 xmlbeans/src/test/java/org/n52/svalbard/decode/AbstractCapabilitiesBaseTypeDecoderTest.java diff --git a/xmlbeans/src/main/java/org/n52/svalbard/decode/AbstractCapabilitiesBaseTypeDecoder.java b/xmlbeans/src/main/java/org/n52/svalbard/decode/AbstractCapabilitiesBaseTypeDecoder.java index d0e871e31..1ff2773a0 100644 --- a/xmlbeans/src/main/java/org/n52/svalbard/decode/AbstractCapabilitiesBaseTypeDecoder.java +++ b/xmlbeans/src/main/java/org/n52/svalbard/decode/AbstractCapabilitiesBaseTypeDecoder.java @@ -325,6 +325,9 @@ private OwsOperationsMetadata parseOperationMetadata(OperationsMetadata operatio } private OwsServiceIdentification parseServiceIdentification(ServiceIdentification serviceIdentification) { + if (serviceIdentification == null) { + return null; + } OwsCode serviceType = parseCode(serviceIdentification.getServiceType()); Set serviceTypeVersion = Optional.ofNullable(serviceIdentification.getServiceTypeVersionArray()) .map(Arrays::stream).orElseGet(Stream::empty).collect(toSet()); diff --git a/xmlbeans/src/test/java/org/n52/svalbard/decode/AbstractCapabilitiesBaseTypeDecoderTest.java b/xmlbeans/src/test/java/org/n52/svalbard/decode/AbstractCapabilitiesBaseTypeDecoderTest.java new file mode 100644 index 000000000..aa6ffb921 --- /dev/null +++ b/xmlbeans/src/test/java/org/n52/svalbard/decode/AbstractCapabilitiesBaseTypeDecoderTest.java @@ -0,0 +1,52 @@ +/* + * Copyright 2016-2017 52°North Initiative for Geospatial Open Source + * Software GmbH + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.n52.svalbard.decode; + +import java.util.Set; + +import org.junit.Test; +import org.n52.shetland.ogc.ows.OwsCapabilities; +import org.n52.svalbard.decode.exception.DecodingException; + +import net.opengis.ows.x11.CapabilitiesBaseType; + +public class AbstractCapabilitiesBaseTypeDecoderTest { + + @Test + public void shouldNotThrowNullPointerExceptionWhenServiceIdentificationIsMissing() throws DecodingException { + AbstractCapabilitiesBaseTypeDecoder decoder = new TestSeam(); + CapabilitiesBaseType cbt = CapabilitiesBaseType.Factory.newInstance(); + cbt.setVersion("2.0.0"); + cbt.setUpdateSequence("nothing-to-see-here"); + decoder.parseCapabilitiesBaseType("SOS", cbt); + } + + private class TestSeam extends AbstractCapabilitiesBaseTypeDecoder { + + @Override + public OwsCapabilities decode(CapabilitiesBaseType objectToDecode) throws DecodingException { + return null; + } + + @Override + public Set getKeys() { + return null; + } + + } + +} From b6199e341c07b2ebb8435152f874bc9aaa23139f Mon Sep 17 00:00:00 2001 From: EHJ-52n Date: Mon, 30 Oct 2017 16:39:51 +0100 Subject: [PATCH 2/2] Improve test --- .../decode/AbstractCapabilitiesBaseTypeDecoderTest.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/xmlbeans/src/test/java/org/n52/svalbard/decode/AbstractCapabilitiesBaseTypeDecoderTest.java b/xmlbeans/src/test/java/org/n52/svalbard/decode/AbstractCapabilitiesBaseTypeDecoderTest.java index aa6ffb921..ad8562fcc 100644 --- a/xmlbeans/src/test/java/org/n52/svalbard/decode/AbstractCapabilitiesBaseTypeDecoderTest.java +++ b/xmlbeans/src/test/java/org/n52/svalbard/decode/AbstractCapabilitiesBaseTypeDecoderTest.java @@ -18,6 +18,8 @@ import java.util.Set; +import org.hamcrest.core.Is; +import org.junit.Assert; import org.junit.Test; import org.n52.shetland.ogc.ows.OwsCapabilities; import org.n52.svalbard.decode.exception.DecodingException; @@ -32,7 +34,7 @@ public void shouldNotThrowNullPointerExceptionWhenServiceIdentificationIsMissing CapabilitiesBaseType cbt = CapabilitiesBaseType.Factory.newInstance(); cbt.setVersion("2.0.0"); cbt.setUpdateSequence("nothing-to-see-here"); - decoder.parseCapabilitiesBaseType("SOS", cbt); + Assert.assertThat(decoder.parseCapabilitiesBaseType("SOS", cbt).getServiceIdentification().isPresent(), Is.is(false)); } private class TestSeam extends AbstractCapabilitiesBaseTypeDecoder {