Skip to content

Commit

Permalink
Fixed MQTT subscriptions when multiple relations between EntityTypes …
Browse files Browse the repository at this point in the history
…exist
  • Loading branch information
hylkevds committed Sep 11, 2023
1 parent d54a9d3 commit 2c4cd97
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import de.fraunhofer.iosb.ilt.frostserver.path.PathElement;
import de.fraunhofer.iosb.ilt.frostserver.path.PathElementEntity;
import de.fraunhofer.iosb.ilt.frostserver.path.PathElementEntitySet;
import de.fraunhofer.iosb.ilt.frostserver.path.PathElementEntityType;
import de.fraunhofer.iosb.ilt.frostserver.path.ResourcePath;
import de.fraunhofer.iosb.ilt.frostserver.persistence.PersistenceManager;
import de.fraunhofer.iosb.ilt.frostserver.property.NavigationPropertyMain;
Expand Down Expand Up @@ -94,21 +95,32 @@ public boolean matches(PersistenceManager persistenceManager, Entity newEntity,
}

protected void generateFilter(int pathElementOffset) {
EntityType lastType = getEntityType();
List<Property> properties = new ArrayList<>();
final List<Property> properties = new ArrayList<>();
boolean direct = true;
for (int i = path.size() - 1 - pathElementOffset; i >= 0; i--) {
final int size = path.size();
final int startIdx = size - 1 - pathElementOffset;
if (startIdx < 0) {
return;
}
PathElement nextPathElement = startIdx < size ? path.get(startIdx + 1) : null;
for (int i = startIdx; i >= 0; i--) {
PathElement element = path.get(i);
if (element instanceof PathElementEntitySet) {
final PathElementEntitySet pees = (PathElementEntitySet) element;
final NavigationPropertyMain navProp = lastType.getNavigationProperty(pees.getEntityType());
properties.add(navProp);
lastType = pees.getEntityType();
if (element instanceof PathElementEntitySet pees) {
NavigationPropertyMain navPropInverse = null;
if (nextPathElement instanceof PathElementEntityType peet) {
final NavigationPropertyMain navProp = peet.getNavigationProperty();
if (navProp != null) {
navPropInverse = navProp.getInverse();
}
}
properties.add(navPropInverse);
direct = false;

} else if (element instanceof PathElementEntity) {
final PathElementEntity epe = (PathElementEntity) element;
final NavigationPropertyMain navProp = lastType.getNavigationProperty(epe.getEntityType());
} else if (element instanceof PathElementEntity epe) {
NavigationPropertyMain navProp = null;
if (nextPathElement instanceof PathElementEntityType peet) {
navProp = peet.getNavigationProperty().getInverse();
}

final Id id = epe.getId();
if (direct && navProp != null && !navProp.isEntitySet() && id != null) {
Expand All @@ -118,7 +130,6 @@ protected void generateFilter(int pathElementOffset) {
}

properties.add(navProp);
lastType = epe.getEntityType();

if (id != null) {
createMatchExpression(properties, epe);
Expand All @@ -127,6 +138,7 @@ protected void generateFilter(int pathElementOffset) {
return;
}
}
nextPathElement = element;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,15 @@ class MqttManagerTest {
private static final Logger LOGGER = LoggerFactory.getLogger(MqttManagerTest.class.getName());

private static final int REPEAT_COUNT = 0;
private static final int MESSAGE_COUNT = 20000;
private static final int MESSAGE_COUNT = 2000;

private CoreSettings coreSettings;
private ModelRegistry modelRegistry;
private TestModel testModel;

@BeforeEach
public void init() {
TestMqttServerRegister.getInstance().clear();
Properties properties = new Properties();
properties.put(CoreSettings.TAG_SERVICE_ROOT_URL, "http://localhost/");
properties.put(CoreSettings.TAG_TEMP_PATH, System.getProperty("java.io.tmpdir"));
Expand Down Expand Up @@ -109,6 +110,17 @@ void testVersionParseFail() {
});
}

@Test
void testSubscriptions() throws InterruptedException {
MqttManager mqttManager = new MqttManager(coreSettings);
List<TestMqttServer> mqttServers = TestMqttServerRegister.getInstance().getServers();
TestMqttServer mqttServer = mqttServers.get(0);
mqttServer.subscribe("v1.1/Houses");
mqttServer.subscribe("v1.1/Houses(1)");
mqttServer.subscribe("v1.1/Houses(1)/Rooms");
mqttServer.subscribe("v1.1/Rooms(1)/House");
}

@Test
void testMqttManager() throws InterruptedException {
MqttManager mqttManager = new MqttManager(coreSettings);
Expand Down Expand Up @@ -161,7 +173,7 @@ private void testTopics(List<TestMqttServer> mqttServers, MqttManager mqttManage
.setEntity(
new DefaultEntity(testModel.ET_ROOM, new IdLong(pubId))
.setProperty(testModel.EP_NAME, "" + pubId)
.setProperty(testModel.NP_HOUSE, new DefaultEntity(testModel.ET_HOUSE, new IdLong(topicId))));
.setProperty(testModel.NP_ROOM_HOUSE, new DefaultEntity(testModel.ET_HOUSE, new IdLong(topicId))));
topicId++;
if (topicId >= subscriptionCount) {
topicId = 0;
Expand Down Expand Up @@ -194,8 +206,8 @@ private void add(TestMqttServer server) {
servers.add(server);
}

private boolean remove(TestMqttServer server) {
return servers.remove(server);
private void clear() {
servers.clear();
}

public List<TestMqttServer> getServers() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ void testFilterLinked() {
String query = "$filter=House/id eq 1";
Query expResult = new Query(modelRegistry, coreSettings.getQueryDefaults(), path);
expResult.setFilter(new Equal(
new Path(testModel.NP_HOUSE, testModel.ET_HOUSE.getPrimaryKey()),
new Path(testModel.NP_ROOM_HOUSE, testModel.ET_HOUSE.getPrimaryKey()),
new IntegerConstant(1)));
Query result = QueryParser.parseQuery(query, coreSettings, path);
result.validate(testModel.ET_ROOM);
Expand All @@ -185,7 +185,7 @@ void testFilterLinked() {
query = "$filter=House/Rooms/name eq 1";
expResult = new Query(modelRegistry, coreSettings.getQueryDefaults(), path);
expResult.setFilter(new Equal(
new Path(testModel.NP_HOUSE, testModel.NP_ROOMS, testModel.EP_NAME),
new Path(testModel.NP_ROOM_HOUSE, testModel.NP_HOUSE_ROOMS, testModel.EP_NAME),
new IntegerConstant(1)));
result = QueryParser.parseQuery(query, coreSettings, path);
result.validate(testModel.ET_ROOM);
Expand Down Expand Up @@ -541,7 +541,7 @@ void testOrderByAliasAscDesc() {
void testOrderByMixedPath() {
String query = "$orderby=House/@iot.id";
Query expResult = new Query(modelRegistry, coreSettings.getQueryDefaults(), path);
expResult.getOrderBy().add(new OrderBy(new Path(testModel.NP_HOUSE, testModel.ET_HOUSE.getPrimaryKey())));
expResult.getOrderBy().add(new OrderBy(new Path(testModel.NP_ROOM_HOUSE, testModel.ET_HOUSE.getPrimaryKey())));
Query result = QueryParser.parseQuery(query, coreSettings, path);
result.validate(testModel.ET_ROOM);
assertEquals(expResult, result);
Expand All @@ -562,18 +562,18 @@ void testOrderByMixedPath() {
@Test
void testSelect() {
Query expResult = new Query(modelRegistry, coreSettings.getQueryDefaults(), path);
expResult.getSelect().add(testModel.NP_ROOMS);
expResult.getSelect().add(testModel.NP_HOUSE_ROOMS);
expResult.getSelect().add(testModel.ET_HOUSE.getPrimaryKey());
Query result = new Query(modelRegistry, coreSettings.getQueryDefaults(), path);
result.addSelect(testModel.NP_ROOMS)
result.addSelect(testModel.NP_HOUSE_ROOMS)
.addSelect(testModel.ET_HOUSE.getPrimaryKey());
assertEquals(expResult, result);

expResult.getSelect().clear();
expResult.getSelect().add(testModel.NP_HOUSE);
expResult.getSelect().add(testModel.NP_ROOM_HOUSE);
expResult.getSelect().add(testModel.ET_ROOM.getPrimaryKey());
result.clearSelect();
result.addSelect(testModel.NP_HOUSE)
result.addSelect(testModel.NP_ROOM_HOUSE)
.addSelect(testModel.ET_ROOM.getPrimaryKey());
assertEquals(expResult, result);
}
Expand Down Expand Up @@ -676,7 +676,7 @@ void testSelectDistinct() {
void testSelectNavigationProperty() {
String query = "$select=Rooms";
Query expResult = new Query(modelRegistry, coreSettings.getQueryDefaults(), path);
expResult.getSelect().add(testModel.NP_ROOMS);
expResult.getSelect().add(testModel.NP_HOUSE_ROOMS);
Query result = QueryParser.parseQuery(query, coreSettings, path);
result.validate(testModel.ET_HOUSE);
assertEquals(expResult, result);
Expand All @@ -686,7 +686,7 @@ void testSelectNavigationProperty() {
void testSelectMultipleMixed() {
String query = "$select=Rooms, id";
Query expResult = new Query(modelRegistry, coreSettings.getQueryDefaults(), path);
expResult.addSelect(testModel.NP_ROOMS)
expResult.addSelect(testModel.NP_HOUSE_ROOMS)
.addSelect(testModel.ET_HOUSE.getPrimaryKey());
Query result = QueryParser.parseQuery(query, coreSettings, path);
result.validate(testModel.ET_HOUSE);
Expand All @@ -697,7 +697,7 @@ void testSelectMultipleMixed() {
void testExpandSingleNavigationProperty() {
String query = "$expand=Rooms";
Query expResult = new Query(modelRegistry, coreSettings.getQueryDefaults(), path);
expResult.getExpand().add(new Expand(modelRegistry, testModel.NP_ROOMS));
expResult.getExpand().add(new Expand(modelRegistry, testModel.NP_HOUSE_ROOMS));
Query result = QueryParser.parseQuery(query, coreSettings, path);
result.validate(testModel.ET_HOUSE);
assertEquals(expResult, result);
Expand All @@ -707,7 +707,7 @@ void testExpandSingleNavigationProperty() {
void testExpandSingleNavigationPropertyEmpty() {
String query = "$expand=Rooms()";
Query expResult = new Query(modelRegistry, coreSettings.getQueryDefaults(), path);
expResult.getExpand().add(new Expand(modelRegistry, testModel.NP_ROOMS));
expResult.getExpand().add(new Expand(modelRegistry, testModel.NP_HOUSE_ROOMS));
Query result = QueryParser.parseQuery(query, coreSettings, path);
result.validate(testModel.ET_HOUSE);
assertEquals(expResult, result);
Expand All @@ -717,9 +717,9 @@ void testExpandSingleNavigationPropertyEmpty() {
void testExpandDeep() {
String query = "$expand=Rooms/House";
Query subQuery = new Query(modelRegistry, coreSettings.getQueryDefaults(), path);
subQuery.getExpand().add(new Expand(modelRegistry, testModel.NP_HOUSE));
subQuery.getExpand().add(new Expand(modelRegistry, testModel.NP_ROOM_HOUSE));
Query expResult = new Query(modelRegistry, coreSettings.getQueryDefaults(), path);
expResult.getExpand().add(new Expand(modelRegistry, subQuery, testModel.NP_ROOMS));
expResult.getExpand().add(new Expand(modelRegistry, subQuery, testModel.NP_HOUSE_ROOMS));
Query result = QueryParser.parseQuery(query, coreSettings, path);
result.validate(testModel.ET_HOUSE);
assertEquals(expResult, result);
Expand Down Expand Up @@ -785,9 +785,9 @@ void testExpandDeepQuery() {
Query subQuery = new Query(modelRegistry, coreSettings.getQueryDefaults(), path);
Query subSubQuery = new Query(modelRegistry, coreSettings.getQueryDefaults(), path);
subSubQuery.getSelect().add(testModel.ET_HOUSE.getPrimaryKey());
subQuery.getExpand().add(new Expand(modelRegistry, subSubQuery, testModel.NP_HOUSE));
subQuery.getExpand().add(new Expand(modelRegistry, subSubQuery, testModel.NP_ROOM_HOUSE));
Query expResult = new Query(modelRegistry, coreSettings.getQueryDefaults(), path);
expResult.getExpand().add(new Expand(modelRegistry, subQuery, testModel.NP_ROOMS));
expResult.getExpand().add(new Expand(modelRegistry, subQuery, testModel.NP_HOUSE_ROOMS));
Query result = QueryParser.parseQuery(query, coreSettings, path);
result.validate(testModel.ET_HOUSE);
assertEquals(expResult, result);
Expand All @@ -797,8 +797,8 @@ void testExpandDeepQuery() {
void testExpandMultipleNavigationProperties() {
String query = "$expand=Rooms,House";
Query expResult = new Query(modelRegistry, coreSettings.getQueryDefaults(), path);
expResult.getExpand().add(new Expand(modelRegistry, testModel.NP_ROOMS));
expResult.getExpand().add(new Expand(modelRegistry, testModel.NP_HOUSE));
expResult.getExpand().add(new Expand(modelRegistry, testModel.NP_HOUSE_ROOMS));
expResult.getExpand().add(new Expand(modelRegistry, testModel.NP_ROOM_HOUSE));
Query result = QueryParser.parseQuery(query, coreSettings, path);
assertEquals(expResult, result);
}
Expand All @@ -807,10 +807,10 @@ void testExpandMultipleNavigationProperties() {
void testExpandMultipleNavigationPropertiesDeep1() {
String query = "$expand=Rooms/House,Rooms/Rooms";
Query expResult = new Query(modelRegistry, coreSettings.getQueryDefaults(), path)
.addExpand(new Expand(modelRegistry, testModel.NP_ROOMS)
.addExpand(new Expand(modelRegistry, testModel.NP_HOUSE_ROOMS)
.setSubQuery(new Query(modelRegistry, coreSettings.getQueryDefaults(), path)
.addExpand(new Expand(modelRegistry, testModel.NP_HOUSE))
.addExpand(new Expand(modelRegistry, testModel.NP_ROOMS))));
.addExpand(new Expand(modelRegistry, testModel.NP_ROOM_HOUSE))
.addExpand(new Expand(modelRegistry, testModel.NP_HOUSE_ROOMS))));
Query result = QueryParser.parseQuery(query, coreSettings, path);
result.validate(testModel.ET_HOUSE);
assertEquals(expResult, result);
Expand All @@ -822,8 +822,8 @@ void testExpandMultipleNavigationPropertiesDeep2() {
Query expResult = new Query(modelRegistry, coreSettings.getQueryDefaults(), path)
.addExpand(new Expand(modelRegistry, testModel.NP_HOUSES)
.setSubQuery(new Query(modelRegistry, coreSettings.getQueryDefaults(), path)
.addExpand(new Expand(modelRegistry, testModel.NP_ROOMS))
.addExpand(new Expand(modelRegistry, testModel.NP_HOUSE))));
.addExpand(new Expand(modelRegistry, testModel.NP_HOUSE_ROOMS))
.addExpand(new Expand(modelRegistry, testModel.NP_ROOM_HOUSE))));
Query result = QueryParser.parseQuery(query, coreSettings, path);
assertEquals(expResult, result);
}
Expand All @@ -834,14 +834,14 @@ void testExpandWithSubquery() {
Query expResult = new Query(modelRegistry, coreSettings.getQueryDefaults(), path);
Query subQuery = new Query(modelRegistry, coreSettings.getQueryDefaults(), path);
subQuery.setFilter(new Equal(new Path(testModel.EP_VALUE), new IntegerConstant(1)));
subQuery.getExpand().add(new Expand(modelRegistry, testModel.NP_HOUSE));
subQuery.getExpand().add(new Expand(modelRegistry, testModel.NP_ROOM_HOUSE));
subQuery.getSelect().add(testModel.ET_ROOM.getPrimaryKey());
subQuery.getOrderBy().add(new OrderBy(new Path(testModel.ET_ROOM.getPrimaryKey())));
subQuery.setSkip(5);
subQuery.setTop(10);
subQuery.setCount(true);
expResult.getExpand().add(new Expand(modelRegistry, subQuery, testModel.NP_ROOMS));
expResult.getExpand().add(new Expand(modelRegistry, testModel.NP_HOUSE));
expResult.getExpand().add(new Expand(modelRegistry, subQuery, testModel.NP_HOUSE_ROOMS));
expResult.getExpand().add(new Expand(modelRegistry, testModel.NP_ROOM_HOUSE));
expResult.setTop(10);
Query result = QueryParser.parseQuery(query, coreSettings, path);
result.validate(testModel.ET_ROOM);
Expand All @@ -854,10 +854,10 @@ void testComplex1() {
Query expResult = new Query(modelRegistry, coreSettings.getQueryDefaults(), path);
Query subQuery1 = new Query(modelRegistry, coreSettings.getQueryDefaults(), path);
subQuery1.setFilter(new Equal(new Path(testModel.EP_VALUE), new IntegerConstant(1)));
subQuery1.getExpand().add(new Expand(modelRegistry, testModel.NP_HOUSE));
subQuery1.getExpand().add(new Expand(modelRegistry, testModel.NP_ROOM_HOUSE));
subQuery1.getSelect().add(testModel.ET_ROOM.getPrimaryKey());
expResult.getExpand().add(new Expand(modelRegistry, subQuery1, testModel.NP_ROOMS));
expResult.getExpand().add(new Expand(modelRegistry, testModel.NP_HOUSE));
expResult.getExpand().add(new Expand(modelRegistry, subQuery1, testModel.NP_HOUSE_ROOMS));
expResult.getExpand().add(new Expand(modelRegistry, testModel.NP_ROOM_HOUSE));
expResult.setTop(10);
Query result = QueryParser.parseQuery(query, coreSettings, path);
result.validate(testModel.ET_ROOM);
Expand All @@ -873,18 +873,18 @@ void testFilterComplex() {
new And(
new Equal(
new Path(
testModel.NP_ROOMS,
testModel.NP_HOUSE,
testModel.NP_HOUSE_ROOMS,
testModel.NP_ROOM_HOUSE,
testModel.ET_HOUSE.getPrimaryKey()),
new StringConstant("FOI_1")),
new GreaterEqual(
new Path(testModel.NP_HOUSE,
testModel.NP_ROOMS,
new Path(testModel.NP_ROOM_HOUSE,
testModel.NP_HOUSE_ROOMS,
testModel.EP_TIME),
new DateTimeConstant(PlainTimestamp.of(2010, 06, 01, 0, 0).inZonalView(TIMEZONE_UTC)))),
new LessEqual(
new Path(testModel.NP_HOUSE,
testModel.NP_ROOMS,
new Path(testModel.NP_ROOM_HOUSE,
testModel.NP_HOUSE_ROOMS,
testModel.EP_TIME),
new DateTimeConstant(PlainTimestamp.of(2010, 07, 01, 0, 0).inZonalView(TIMEZONE_UTC)))));
Query result = QueryParser.parseQuery(query, coreSettings, path);
Expand Down
Loading

0 comments on commit 2c4cd97

Please sign in to comment.