001/**
002 * Copyright (C) 2006-2018 Talend Inc. - www.talend.com
003 * <p>
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 * <p>
008 * http://www.apache.org/licenses/LICENSE-2.0
009 * <p>
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.talend.sdk.component.junit;
017
018import java.util.Collection;
019import java.util.HashMap;
020import java.util.Iterator;
021import java.util.Map;
022
023import javax.json.JsonObject;
024import javax.json.bind.Jsonb;
025import javax.json.bind.JsonbBuilder;
026import javax.json.bind.JsonbConfig;
027
028/**
029 * An input factory which joins multiple distinct sources reading them in "parallel".
030 *
031 * IMPORTANT: all entries of the map but have the same "size".
032 */
033public class JoinInputFactory implements ControllableInputFactory {
034
035    private final Map<String, Iterator<?>> data = new HashMap<>();
036
037    private volatile Jsonb jsonb;
038
039    public JoinInputFactory withInput(final String branch, final Collection<?> branchData) {
040        data.put(branch, branchData.iterator());
041        return this;
042    }
043
044    @Override
045    public Object read(final String name) {
046        final Iterator<?> iterator = data.get(name);
047        if (iterator != null && iterator.hasNext()) {
048            return map(iterator.next());
049        }
050        return null;
051    }
052
053    @Override
054    public boolean hasMoreData() {
055        final boolean hasMore = !data.isEmpty() && data.entrySet().stream().allMatch(e -> e.getValue().hasNext());
056        if (!hasMore && jsonb != null) {
057            synchronized (this) {
058                if (jsonb != null) {
059                    try {
060                        jsonb.close();
061                    } catch (final Exception e) {
062                        // no-op: not important here
063                    }
064                }
065            }
066        }
067        return hasMore;
068    }
069
070    @Override
071    public InputFactoryIterable asInputRecords() {
072        return new InputFactoryIterable(this, data);
073    }
074
075    private Object map(final Object next) {
076        if (next == null || JsonObject.class.isInstance(next)) {
077            return next;
078        }
079        if (jsonb == null) {
080            synchronized (this) {
081                if (jsonb == null) {
082                    jsonb = JsonbBuilder.create(new JsonbConfig().setProperty("johnzon.cdi.activated", false));
083                }
084            }
085        }
086        final String str = jsonb.toJson(next);
087        // primitives mainly, not that accurate in main code but for now not forbidden
088        if (str.equals(next.toString())) {
089            return next;
090        }
091        // pojo
092        return jsonb.fromJson(str, JsonObject.class);
093    }
094}