Skip to content

Commit

Permalink
GH-1674: Fix NPE when creating Composite failure Response
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernard31 committed Nov 20, 2024
1 parent 5f0d210 commit 8bd4416
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,15 @@ public ReadCompositeResponse(ResponseCode responseCode, Map<LwM2mPath, LwM2mNode
}
} else {
// handle if content (not timestamped) value is passed
this.timestampedValues = null;
this.content = content;
}

if (ResponseCode.CONTENT.equals(code)) {
// check content is not null
if (content == null) {
if (this.content == null) {
throw new IllegalArgumentException("content OR timestampedValue should be not null");
}

this.timestampedValues = null;
this.content = content;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*******************************************************************************
* Copyright (c) 2024 Sierra Wireless and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v20.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.html.
*
* Contributors:
* Michał Wadowski (Orange) - Add Observe-Composite feature.
*******************************************************************************/
package org.eclipse.leshan.core.response;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

import org.eclipse.leshan.core.ResponseCode;
import org.junit.jupiter.api.Test;

public class CancelCompositeObservationResponseTest {

@Test
public void create_failure_reponse() {
CancelCompositeObservationResponse response = new CancelCompositeObservationResponse(ResponseCode.NOT_FOUND,
null, null, null, null, null);
assertEquals(response.getCode(), ResponseCode.NOT_FOUND);
assertNull(response.getContent());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@
import static org.eclipse.leshan.core.ResponseCode.CONTENT;
import static org.eclipse.leshan.core.node.LwM2mSingleResource.newResource;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.leshan.core.ResponseCode;
import org.eclipse.leshan.core.node.LwM2mNode;
import org.eclipse.leshan.core.node.LwM2mPath;
import org.junit.jupiter.api.Test;
Expand All @@ -42,4 +44,11 @@ public void should_create_response_with_content() {
// then
assertEquals(exampleContent, response.getContent());
}

@Test
public void create_failure_reponse() {
ObserveCompositeResponse response = ObserveCompositeResponse.notFound();
assertEquals(response.getCode(), ResponseCode.NOT_FOUND);
assertNull(response.getContent());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*******************************************************************************
* Copyright (c) 2024 Sierra Wireless and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v20.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.html.
*
* Contributors:
* Sierra Wireless - initial API and implementation
*******************************************************************************/
package org.eclipse.leshan.core.response;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

import org.eclipse.leshan.core.ResponseCode;
import org.junit.jupiter.api.Test;

public class ReadCompositeResponseTest {

@Test
public void create_failure_reponse() {
ReadCompositeResponse response = ReadCompositeResponse.notFound();
assertEquals(response.getCode(), ResponseCode.NOT_FOUND);
assertNull(response.getContent());
}
}

0 comments on commit 8bd4416

Please sign in to comment.