Remove explicit default super-constructor calls
[netconf.git] / netconf / tools / netconf-cli / src / test / java / org / opendaylight / netconf / cli / ConsoleIOTestImpl.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.netconf.cli;
9
10 import java.io.IOException;
11 import java.util.Deque;
12 import java.util.HashMap;
13 import java.util.List;
14 import java.util.Map;
15 import org.opendaylight.netconf.cli.io.ConsoleIOImpl;
16
17 public class ConsoleIOTestImpl extends ConsoleIOImpl {
18
19     Map<String, Deque<String>> inputValues = new HashMap<>();
20     String lastMessage;
21     private final List<ValueForMessage> valuesForMessages;
22
23     public ConsoleIOTestImpl(final Map<String, Deque<String>> inputValues,
24                              final List<ValueForMessage> valuesForMessages) throws IOException {
25         this.inputValues = inputValues;
26         this.valuesForMessages = valuesForMessages;
27     }
28
29     StringBuilder output = new StringBuilder();
30
31     @Override
32     public String read() throws IOException {
33         final String prompt = buildPrompt();
34         output.append(prompt);
35         System.console().writer().print(prompt);
36
37         String value = inputValues.get(prompt).pollFirst();
38         if (value == null) {
39             value = getValueForLastMessage();
40         }
41
42         value = value != null ? value : "****NO VALUE****";
43
44         output.append(value + "\n");
45         System.console().writer().println(value + "\n");
46         return value;
47     }
48
49     private String getValueForLastMessage() {
50         for (final ValueForMessage valueForMessage : valuesForMessages) {
51             if (containsLastMessageKeyWords(valueForMessage.getKeyWords())) {
52                 return valueForMessage.getValue();
53             }
54         }
55         return null;
56     }
57
58     private boolean containsLastMessageKeyWords(final List<String> keyWords) {
59         for (final String keyWord : keyWords) {
60             if (!lastMessage.contains(keyWord)) {
61                 return false;
62             }
63         }
64         return true;
65     }
66
67     @Override
68     public void write(final CharSequence data) throws IOException {
69         output.append(data);
70         lastMessage = (String) data;
71         System.console().writer().print(data);
72     }
73
74     @Override
75     public void writeLn(final CharSequence data) throws IOException {
76         write(data);
77         output.append("\n");
78         System.console().writer().print("\n");
79     }
80
81     public String getConsoleOutput() {
82         return output.toString();
83     }
84 }