Skip to content

Commit

Permalink
Better handling of incorrect model with 2 resources with same id.
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernard31 committed Feb 11, 2021
1 parent 034262a commit d159ae5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
Expand Down Expand Up @@ -156,7 +158,7 @@ private ObjectModel parseObject(Node object, String streamName, Version schemaVe
String version = ObjectModel.DEFAULT_VERSION;
Boolean multiple = null;
Boolean mandatory = null;
List<ResourceModel> resources = new ArrayList<>();
Map<Integer, ResourceModel> resources = new HashMap<>();
String urn = null;
String description2 = null;
String lwm2mVersion = ObjectModel.DEFAULT_VERSION;
Expand Down Expand Up @@ -202,7 +204,14 @@ private ObjectModel parseObject(Node object, String streamName, Version schemaVe
continue;

if (item.getNodeName().equals("Item")) {
resources.add(this.parseResource(item, streamName));
ResourceModel resource = this.parseResource(item, streamName);
if (validate && resources.containsKey(resource.id)) {
throw new InvalidDDFFileException(
"Object %s in %s contains at least 2 resources with same id %s.",
id != null ? id : "", streamName, resource.id);
} else {
resources.put(resource.id, resource);
}
}
}
break;
Expand All @@ -227,8 +236,8 @@ private ObjectModel parseObject(Node object, String streamName, Version schemaVe
}
}

return new ObjectModel(id, name, description, version, multiple, mandatory, resources, urn, lwm2mVersion,
description2);
return new ObjectModel(id, name, description, version, multiple, mandatory, resources.values(), urn,
lwm2mVersion, description2);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ public ObjectModel(Integer id, String name, String description, String version,
for (ResourceModel resource : resources) {
ResourceModel old = resourcesMap.put(resource.id, resource);
if (old != null) {
throw new IllegalStateException(String
.format("Model already exists for resource %d of object %d. Overriding it.", resource.id, id));
throw new IllegalStateException(
String.format("Model for resource %d of object %d already exist", resource.id, id));
}
resourcesMap.put(resource.id, resource);
}
Expand Down

0 comments on commit d159ae5

Please sign in to comment.