Skip to content

Commit

Permalink
fix(#2894): Fix flaky e2e test (#2895)
Browse files Browse the repository at this point in the history
  • Loading branch information
tenthe authored May 23, 2024
1 parent c5867cf commit 649d2a9
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ protected Map<String, Object> applyNested(Map<String, Object> event,

event.remove(key);

if (newSubEvent != null && !newSubEvent.isEmpty()) {
if (newSubEvent != null) {
event.put(key, newSubEvent);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,22 @@

package org.apache.streampipes.connect.shared.preprocessing.transform.schema;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

public class DeleteTransformationRuleTest {


private ObjectMapper objectMapper = new ObjectMapper();

@Test
public void transformSimple() {
var event = new HashMap<String, Object>();
Expand All @@ -37,25 +43,63 @@ public void transformSimple() {

var result = deleteRule.apply(event);

assertEquals(0, result.keySet().size());
assertEquals(0,
result.keySet()
.size()
);
}

@Test
public void transformNested() {
var child = new HashMap<String, Object>();
child.put("child", "value");
child.put("secondChild", "value");
var event = new HashMap<String, Object>();
event.put("parent", child);
event.put("keepProperty", "test");
public void transformNested() throws JsonProcessingException {
var jsonString = """
{
"parent": {
"child": "value",
"child2": "value2"
},
"keepProperty": "test"
}
""";

var event = getEvent(jsonString);

var deleteRule = new DeleteTransformationRule(List.of("parent", "child"));

var result = deleteRule.apply(event);

assertEquals(2, result.keySet().size());
assertEquals(2,
result.keySet()
.size()
);
assertEquals(1, ((Map) result.get("parent")).size());
}

@Test
public void testRemoveParent() throws JsonProcessingException {
var jsonString = """
{
"parent": {
"child": "value",
"child2": "value2"
},
"keepProperty": "test"
}
""";

var event = getEvent(jsonString);

var deleteRule = new DeleteTransformationRule(List.of("parent"));

var result = deleteRule.apply(event);

assertEquals(1,
result.keySet()
.size()
);
assertEquals("test", result.get("keepProperty"));
}


@Test
// verifying that mathod applyTransformation method works when passed a null event.
public void applyTransformationWithNullParameter() {
Expand All @@ -64,19 +108,32 @@ public void applyTransformationWithNullParameter() {
}

@Test
public void deleteNestedChildWithParentProperty() {
var child = new HashMap<String, Object>();
child.put("child", "value");
var event = new HashMap<String, Object>();
event.put("parent", child);
event.put("keepProperty", "test");
public void deleteNestedChildWithParentProperty() throws JsonProcessingException {

var jsonString = """
{
"parent": {
"child": "value"
},
"keepProperty": "test"
}
""";

var event = getEvent(jsonString);

var deleteRule = new DeleteTransformationRule(Arrays.asList("parent", "child"));

var result = deleteRule.apply(event);

assertEquals(1, result.keySet().size());
assertEquals(2,
result.keySet()
.size()
);
assertEquals("test", result.get("keepProperty"));
}

private Map<String, Object> getEvent(String eventJson) throws JsonProcessingException {
return objectMapper.readValue(eventJson, HashMap.class);
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"timestamp": 1667904471000,
"parent": {
"child": "text"
"child": "text",
"child_two": "textTwo"
}
}
9 changes: 8 additions & 1 deletion ui/cypress/support/utils/ConnectEventSchemaUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,14 @@ export class ConnectEventSchemaUtils {
.click({ force: true });
cy.dataCy('connect-schema-delete-properties-btn', {
timeout: 10000,
}).click();
}).click({ force: true });

// The following two commands are required to fix flaky tests
// if another solution can be found, it can be removed
cy.wait(200);
cy.dataCy('connect-schema-update-preview-btn', {
timeout: 10000,
}).click({ force: true });
}

public static changePropertyDataType(
Expand Down
30 changes: 30 additions & 0 deletions ui/cypress/tests/adapter/rules/deleteTransformationRule.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,25 @@ describe('Connect delete rule transformation', () => {
ConnectEventSchemaUtils.deleteProperty('child');

// The resulting string contains non-breaking spaces character (\u00A0)
cy.dataCy('schema-preview-result-event').should(
'have.text',
'{\u00A0\u00A0\u00A0\u00A0"parent":\u00A0{\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0"child_two":\u00A0"textTwo"\u00A0\u00A0\u00A0\u00A0},\u00A0\u00A0\u00A0\u00A0"timestamp":\u00A01667904471000}',
);

ConnectBtns.refreshSchema().click();

// Test to delete the parent property
ConnectEventSchemaUtils.deleteProperty('parent');
cy.dataCy('schema-preview-result-event').should(
'have.text',

'{\u00A0\u00A0\u00A0\u00A0"timestamp":\u00A01667904471000}',
);

ConnectBtns.refreshSchema().click();
ConnectEventSchemaUtils.deleteProperty('child');
ConnectEventSchemaUtils.deleteProperty('child_two');

cy.dataCy('schema-preview-result-event').should(
'have.text',
'{\u00A0\u00A0\u00A0\u00A0"parent":\u00A0{},\u00A0\u00A0\u00A0\u00A0"timestamp":\u00A01667904471000}',
Expand All @@ -83,5 +102,16 @@ describe('Connect delete rule transformation', () => {

'{\u00A0\u00A0\u00A0\u00A0"timestamp":\u00A01667904471000}',
);

ConnectBtns.refreshSchema().click();

// Test to delete both child properties
ConnectEventSchemaUtils.deleteProperty('child');
ConnectEventSchemaUtils.deleteProperty('child_two');

cy.dataCy('schema-preview-result-event').should(
'have.text',
'{\u00A0\u00A0\u00A0\u00A0"parent":\u00A0{},\u00A0\u00A0\u00A0\u00A0"timestamp":\u00A01667904471000}',
);
});
});

0 comments on commit 649d2a9

Please sign in to comment.