2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.netconf.cli;
10 import java.io.IOException;
11 import java.util.Deque;
12 import java.util.HashMap;
13 import java.util.List;
15 import org.opendaylight.netconf.cli.io.ConsoleIOImpl;
17 public class ConsoleIOTestImpl extends ConsoleIOImpl {
19 Map<String, Deque<String>> inputValues = new HashMap<>();
21 private final List<ValueForMessage> valuesForMessages;
23 public ConsoleIOTestImpl(final Map<String, Deque<String>> inputValues,
24 final List<ValueForMessage> valuesForMessages) throws IOException {
25 this.inputValues = inputValues;
26 this.valuesForMessages = valuesForMessages;
29 StringBuilder output = new StringBuilder();
32 public String read() throws IOException {
33 final String prompt = buildPrompt();
34 output.append(prompt);
35 System.console().writer().print(prompt);
37 String value = inputValues.get(prompt).pollFirst();
39 value = getValueForLastMessage();
42 value = value != null ? value : "****NO VALUE****";
44 output.append(value + "\n");
45 System.console().writer().println(value + "\n");
49 private String getValueForLastMessage() {
50 for (final ValueForMessage valueForMessage : valuesForMessages) {
51 if (containsLastMessageKeyWords(valueForMessage.getKeyWords())) {
52 return valueForMessage.getValue();
58 private boolean containsLastMessageKeyWords(final List<String> keyWords) {
59 for (final String keyWord : keyWords) {
60 if (!lastMessage.contains(keyWord)) {
68 public void write(final CharSequence data) throws IOException {
70 lastMessage = (String) data;
71 System.console().writer().print(data);
75 public void writeLn(final CharSequence data) throws IOException {
78 System.console().writer().print("\n");
81 public String getConsoleOutput() {
82 return output.toString();