001//@formatter:off
002/*
003 * OldSys Customer Manager Mock
004 * Code-Beispiel zum Buch Patterns Kompakt, Verlag Springer Vieweg
005 * Copyright 2014 Karl Eilebrecht
006 * 
007 * Licensed under the Apache License, Version 2.0 (the "License"):
008 * you may not use this file except in compliance with the License.
009 * You may obtain a copy of the License at
010 *
011 * http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019//@formatter:on
020package de.calamanari.pk.wrapper;
021
022import java.util.ArrayList;
023import java.util.HashMap;
024import java.util.List;
025import java.util.Map;
026
027import de.calamanari.pk.wrapper.legacy.OldSysCustomerMgr;
028
029/**
030 * OldSys Customer Manager Mock - mocks the old system's customer manager.
031 * 
032 * @author <a href="mailto:Karl.Eilebrecht(a/t)calamanari.de">Karl Eilebrecht</a>
033 */
034public class OldSysCustomerMgrMock extends OldSysCustomerMgr {
035
036    /**
037     * stores the test data for system test
038     */
039    private final Map<Integer, String[]> testData = new HashMap<>();
040
041    /**
042     * New mock
043     */
044    public OldSysCustomerMgrMock() {
045
046    }
047
048    /**
049     * Method allows to add test data.
050     * 
051     * @param customerId id of customer
052     * @param customerData data about customer
053     */
054    public void addTestRecord(int customerId, String[] customerData) {
055        testData.put(customerId, customerData);
056    }
057
058    @Override
059    public String[] getCustomerData(int customerId) {
060        return testData.getOrDefault(customerId, OldSysCustomerMgr.NO_RESULT);
061    }
062
063    @Override
064    public int[] findCustomerByExample(String[] searchData) {
065        List<Integer> resultList = new ArrayList<>();
066        Map<String, String> searchMap = createMapFromLegacyRecord(searchData);
067        for (Map.Entry<Integer, String[]> entry : testData.entrySet()) {
068            if (matchCustomer(searchMap, createMapFromLegacyRecord(entry.getValue()))) {
069                resultList.add(entry.getKey());
070            }
071        }
072
073        int len = resultList.size();
074        int[] res = new int[len];
075        for (int i = 0; i < len; i++) {
076            res[i] = resultList.get(i);
077        }
078        return res;
079    }
080
081    /**
082     * Matches customer with expected data
083     * 
084     * @param expectedData this fields are requested
085     * @param customerData current customer
086     * @return true if customer matches example
087     */
088    private boolean matchCustomer(Map<String, String> expectedData, Map<String, String> customerData) {
089        boolean match = true;
090        for (Map.Entry<String, String> entry : expectedData.entrySet()) {
091            String key = entry.getKey();
092            String expectedValue = entry.getValue();
093            String customerValue = customerData.get(key);
094            if ((customerValue == null && expectedValue == null) || (customerValue != null && customerValue.equals(expectedValue))) {
095                continue;
096            }
097            else {
098                match = false;
099                break;
100            }
101        }
102        return match;
103    }
104
105    /**
106     * Helper method, creates map from a legacy record.
107     * 
108     * @param customerData fields from customer record
109     * @return map key/value
110     */
111    private static Map<String, String> createMapFromLegacyRecord(String[] customerData) {
112        Map<String, String> res = new HashMap<>();
113        for (int i = 0; i < customerData.length; i = i + 2) {
114            res.put(customerData[i], customerData[i + 1]);
115        }
116        return res;
117    }
118}