All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.telekom.test.bddwebapp.interaction.Interaction Maven / Gradle / Ivy

There is a newer version: 3.2
Show newest version
package de.telekom.test.bddwebapp.interaction;

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

import static org.junit.Assert.assertNotNull;

/**
 * Interface for Interactions
 *
 * @author Daniel Keiss {@literal }
 * 

* Copyright (c) 2018 Daniel Keiss, Deutsche Telekom AG * This file is distributed under the conditions of the Apache License, Version 2.0. * For details see the file license on the toplevel. */ interface Interaction { String OBJECT_KEY_SEPARATOR = "."; /** * Store some data in the interaction context for later use. */ void remember(String key, Object value); /** * Store some data in the interaction context for later use. Use enum as key. */ default void remember(Enum key, Object value) { remember(key.toString(), value); } /** * Get some data in the interaction context. */ default S recall(String key) { return (S) getContext().get(key); } /** * Get some data in the interaction context. Use enum as key. */ default S recall(Enum key) { return recall(key.toString()); } default S recall(String key, Class type) { return type.cast(recall(key)); } default S recall(Enum key, Class type) { return recall(key.toString(), type); } default S recallNotNull(String key) { S value = recall(key); assertNotNull(String.format("Recalled '%s' for story interaction value '%s'", value, key), value); return value; } default S recallNotNull(Enum key) { return recallNotNull(key.toString()); } default S recallNotNull(String key, Class type) { return type.cast(recallNotNull(key)); } default S recallNotNull(Enum key, Class type) { return type.cast(recallNotNull(key.toString())); } Map getContext(); // ------------------------------------------------------------------------- // Object Handling // ------------------------------------------------------------------------- default void rememberObject(String entityKey, String objectKey, Object value) { Object object = recall(entityKey); Map objectMap; if (object instanceof Map) { objectMap = (Map) object; } else { objectMap = new HashMap<>(); } objectMap.put(objectKey, value); remember(entityKey, objectMap); /* This is a fallback for the special case that the key with a simple value already exists in interaction. * E.g. if you remember("key", "value") and than remember("key", "object", "value"). * Try to avoid this construct as it leads to inconsistencies! */ if (object != null && !(object instanceof Map)) { remember(entityKey, object); } } default void rememberObject(Enum entityKey, String objectKey, Object value) { rememberObject(entityKey.toString(), objectKey, value); } default void rememberObject(String entityKey, Map object) { remember(entityKey, object); } default S recallObject(String objectKey, String attributeKey) { String key = objectKey + OBJECT_KEY_SEPARATOR + attributeKey; return recall(key); } default S recallObjectNotNull(String objectKey, String attributeKey) { String key = objectKey + OBJECT_KEY_SEPARATOR + attributeKey; return recallNotNull(key); } default S recallObject(Enum objectKey, String attributeKey) { return recallObject(objectKey.toString(), attributeKey); } default S recallObjectNotNull(Enum objectKey, String attributeKey) { return recallObjectNotNull(objectKey.toString(), attributeKey); } default Map recallMap(String key) { return recall(key); } default Map recallMap(Enum key) { return recall(key.toString()); } // ------------------------------------------------------------------------- // List Handling // ------------------------------------------------------------------------- default void rememberToList(String key, S value) { List list = recallList(key); if (list == null) { list = new ArrayList<>(); } list.add(value); remember(key, list); } default void rememberToList(Enum key, S value) { rememberToList(key.toString(), value); } default List recallList(String key) { return recall(key); } default List recallList(Enum key) { return recall(key); } }