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.controller.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.controller.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, final List<ValueForMessage> valuesForMessages)
26 this.inputValues = inputValues;
27 this.valuesForMessages = valuesForMessages;
30 StringBuilder output = new StringBuilder();
33 public String read() throws IOException {
34 final String prompt = buildPrompt();
35 output.append(prompt);
36 System.out.print(prompt);
38 String value = inputValues.get(prompt).pollFirst();
40 value = getValueForLastMessage();
43 value = value != null ? value : "****NO VALUE****";
45 output.append(value + "\n");
46 System.out.print(value + "\n");
50 private String getValueForLastMessage() {
51 for (final ValueForMessage valueForMessage : valuesForMessages) {
52 if (containsLastMessageKeyWords(valueForMessage.getKeyWords())) {
53 return valueForMessage.getValue();
59 private boolean containsLastMessageKeyWords(final List<String> keyWords) {
60 for (final String keyWord : keyWords) {
61 if (!lastMessage.contains(keyWord)) {
69 public void write(final CharSequence data) throws IOException {
71 lastMessage = (String) data;
72 System.out.print(data);
76 public void writeLn(final CharSequence data) throws IOException {
79 System.out.print("\n");
82 public String getConsoleOutput() {
83 return output.toString();