blob: cd64f32e89794b78240a567c2664b4e22357fe26 [file] [log] [blame]
/**
*
* Copyright (c) 2011, 2016 - Loetz GmbH&Co.KG (69115 Heidelberg, Germany)
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Christophe Loetz (Loetz GmbH&Co.KG) - initial implementation
*/
package org.eclipse.osbp.xtext.entitymock.common;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.osbp.dsl.common.datatypes.IDto;
import org.eclipse.osbp.xtext.entitymock.common.filler.FillerTextProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public abstract class ABaseMockObject {
private final FillerTextProvider fillerProvider;
private ConcurrentHashMap<String, Object> mockData = new ConcurrentHashMap<>();
private static Logger logger = LoggerFactory.getLogger("mock");
public ABaseMockObject() {
FillerTextProvider provider;
try {
provider = FillerTextProvider.get(Locale.getDefault());
} catch (Exception e) {
provider = null;
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
logger.error("{}", sw.toString());
}
this.fillerProvider = provider;
}
public Map<String, Object> getMockData() {
return mockData;
}
public FillerTextProvider getFillerProvider() {
return fillerProvider;
}
public final Object getAttribute(String... keys) {
return mockData.get(StringUtils.join(keys, "."));
}
public final Object getAttribute(String key, AEntityMockDataGenerator mockDataGenerator) {
Object mockValue = mockData.get(key);
if ((mockValue == null) && (mockDataGenerator instanceof AEntityMockDataGenerator)) {
String[] keys = key.split("\\.", 2);
if (keys.length == 2) {
mockValue = mockData.get(keys[0]);
if (mockValue instanceof IDto) {
mockValue = mockDataGenerator.getDtoAttribute((IDto) mockValue, keys[1]);
} else {
mockValue = null;
}
}
}
return mockValue;
}
public final Set<String> getAttributeNames() {
return mockData.keySet();
}
public final ABaseMockObject generateData() {
fillerProvider.reset();
try {
generateDataRow();
} catch (Exception e) {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
logger.error("exception occured:{}", sw.toString());
}
replaceDigits();
replaceVariables();
return this;
}
protected final void replaceDigits() {
ConcurrentHashMap<String, Object> tmpMockData = new ConcurrentHashMap<>();
for (Entry<String, Object> entrySet : mockData.entrySet()) {
Object value = entrySet.getValue();
if (value instanceof String && ((String) value).contains("#")) {
try {
tmpMockData.put(entrySet.getKey(), fillerProvider.numerify((String) value));
} catch (Exception e) {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
logger.error("exception occured:{}", sw.toString());
}
} else {
tmpMockData.put(entrySet.getKey(), entrySet.getValue());
}
}
mockData = tmpMockData;
}
protected final void replaceVariables() {
ConcurrentHashMap<String, Object> tmpMockData = new ConcurrentHashMap<>();
for (Entry<String, Object> entrySet : mockData.entrySet()) {
if (entrySet.getValue() instanceof String) {
String[] parts = ((String) entrySet.getValue()).split("(\\[)|(\\])");
String value = (String) entrySet.getValue();
for (int i = 0; i < parts.length; i++) {
if (mockData.containsKey(parts[i]) && mockData.get(parts[i]) instanceof String) {
value = value.replace(parts[i], (String) mockData.get(parts[i])).replace("[","").replace("]", "");
}
}
tmpMockData.put(entrySet.getKey(), value);
} else {
tmpMockData.put(entrySet.getKey(), entrySet.getValue());
}
}
mockData = tmpMockData;
}
protected abstract void generateDataRow();
protected final void reset() {
mockData.clear();
}
protected final void setFixAttribute(String key, Object value) {
mockData.put(key, value);
}
protected final void generateAttribute(String key, String sourceKey) {
mockData.put(key, mockData.get(sourceKey));
}
protected final void generateAttribute(String key, Object[] values) {
mockData.put(key, values[fillerProvider.unsignedinteger(values.length - 1)]);
}
protected final void generateAttribute(String key, String... part) {
StringBuilder composite = new StringBuilder();
for (int i = 0; i < part.length; i++) {
if (mockData.containsKey(part[i])) {
composite.append(mockData.get(part[i]));
} else {
composite.append(part[i]);
}
}
mockData.put(key, composite);
}
protected final void generateAttribute(String key, String whenKey, Map<?, ?> valuesMap) {
Object[] values = (Object[]) valuesMap.get(mockData.get(whenKey));
if ((values == null) || values.length == 0) {
mockData.put(key, null);
} else {
mockData.put(key, values[fillerProvider.unsignedinteger(values.length - 1)]);
}
}
protected final void generateAttribute(String key, double[] values) {
mockData.put(key, values[fillerProvider.unsignedinteger(values.length - 1)]);
}
protected final void generateSignedAttribute(String key, int decimals, double beginRange, double endRange,
double rounded) {
mockData.put(key, fillerProvider.signeddouble(decimals, beginRange, endRange, rounded));
}
protected final void generateUnsignedAttribute(String key, int decimals, double beginRange, double endRange,
double rounded) {
mockData.put(key, fillerProvider.signeddouble(decimals, beginRange, endRange, rounded));
}
protected final void generateAttribute(String key, int[] values) {
mockData.put(key, values[fillerProvider.unsignedinteger(values.length - 1)]);
}
protected final void generateSignedAttribute(String key, int beginRange, int endRange, double rounded) {
mockData.put(key, fillerProvider.signedinteger(beginRange, endRange, rounded));
}
protected final void generateUnsignedAttribute(String key, int beginRange, int endRange, double rounded) {
mockData.put(key, fillerProvider.signedinteger(beginRange, endRange, rounded));
}
protected final void generateAttribute(String key, ABaseMockObject subObject) {
subObject.reset();
subObject.generateData();
for (String subkey : subObject.mockData.keySet()) {
mockData.put(key + "." + subkey, subObject.mockData.get(subkey));
}
}
protected final void generateBooleanAttribute(String key) {
mockData.put(key, fillerProvider.trueOrFalse());
}
protected final void generateDateAttribute(String key, int pastYears, int futureYears) {
mockData.put(key, fillerProvider.date(pastYears, futureYears));
}
protected final void generateParagraphsAttribute(String key, int count) {
mockData.put(key, fillerProvider.paragraphs(count));
}
protected final void generateSentencesAttribute(String key, int count) {
mockData.put(key, fillerProvider.sentences(count));
}
protected final void generateWordsAttribute(String key, int count) {
mockData.put(key, fillerProvider.words(count));
}
}