1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.chabala.brick.controllab;
20
21 import org.junit.After;
22 import org.junit.Before;
23 import org.junit.Rule;
24 import org.junit.Test;
25 import org.junit.rules.ExpectedException;
26 import org.mockito.Mock;
27 import org.mockito.junit.MockitoJUnit;
28 import org.mockito.junit.MockitoRule;
29 import org.mockito.stubbing.Answer;
30 import org.slf4j.Logger;
31
32 import java.util.Arrays;
33 import java.util.EnumSet;
34 import java.util.List;
35 import java.util.concurrent.atomic.AtomicBoolean;
36
37 import static org.hamcrest.Matchers.*;
38 import static org.junit.Assert.assertThat;
39 import static org.junit.Assume.assumeThat;
40 import static org.mockito.Mockito.*;
41
42
43
44
45 public class ControlLabTest {
46
47 @Rule
48 public MockitoRule rule = MockitoJUnit.rule();
49
50 @Rule
51 public ExpectedException thrown = ExpectedException.none();
52
53 @Mock
54 private SerialPortFactory portFactory;
55
56 @Mock
57 private InputManager inputManager;
58
59 @Mock
60 private SerialPort serialPort;
61
62 @Mock
63 private jssc.SerialPort innerSerialPort;
64
65 @Mock
66 private SerialPortEventListener listener;
67
68 @Mock
69 private Logger log;
70
71 private ControlLab controlLab;
72
73 @Before
74 public void setUp() {
75 controlLab = new ControlLabImpl(log, portFactory, inputManager, (sp, inputManager) -> listener);
76 }
77
78 @After
79 public void tearDown() throws Exception {
80 controlLab.close();
81 }
82
83 @Test
84 public void testGetAvailablePorts() {
85 when(portFactory.getAvailablePorts()).thenReturn(Arrays.asList("one", "two"));
86 List<String> availablePorts = controlLab.getAvailablePorts();
87 assertThat(availablePorts, is(not(empty())));
88 assertThat(availablePorts, hasItems("one", "two"));
89 }
90
91 @Test
92 public void testOpen() throws Exception {
93 final String portName = "one";
94 AtomicBoolean handshakeSeen = new AtomicBoolean(false);
95 when(portFactory.getSerialPort(portName)).thenReturn(serialPort);
96 when(serialPort.getPortName()).thenReturn(portName);
97 when(serialPort.isOpen()).thenReturn(true);
98 when(serialPort.write(Protocol.HANDSHAKE_CHALLENGE.getBytes())).thenAnswer((Answer<Boolean>) invocation -> {
99 handshakeSeen.set(true);
100 return true;
101 });
102 when(listener.isHandshakeSeen()).thenAnswer(i -> handshakeSeen.get());
103 controlLab.open(portName);
104 verify(serialPort, times(1)).openPort();
105 verify(serialPort, times(1)).addEventListener(listener);
106 }
107
108 @Test
109 public void testGetOutput() {
110 OutputId outputId = OutputId.A;
111 Output output = controlLab.getOutput(outputId);
112 assertThat(output.getOutputIdSet(), contains(outputId));
113 }
114
115 @Test
116 public void testGetOutputGroupDelegatesInTheSingleCase() {
117 OutputId outputId = OutputId.B;
118 Output output = controlLab.getOutput(outputId);
119 assumeThat(output.getOutputIdSet(), contains(outputId));
120 Output outputGroup = controlLab.getOutput(EnumSet.of(outputId));
121 assertThat(outputGroup.getOutputIdSet(), contains(outputId));
122 assertThat(outputGroup, is(output));
123 }
124
125 @Test
126 public void testGetOutputGroup() {
127 Output outputGroup = controlLab.getOutput(EnumSet.of(OutputId.C, OutputId.F));
128 assertThat(outputGroup.getOutputIdSet(), contains(OutputId.C, OutputId.F));
129 }
130
131 @Test
132 public void testGetConnectedPortNameWhenNotConnected() {
133 assertThat(controlLab.getConnectedPortName(), is(""));
134 }
135
136 @Test
137 public void testGetConnectedPortNameWhenConnected() throws Exception {
138 final String portName = "two";
139 when(portFactory.getSerialPort(portName)).thenReturn(serialPort);
140 when(serialPort.getPortName()).thenReturn(portName);
141 when(listener.isHandshakeSeen()).thenAnswer(i -> true);
142 controlLab.open(portName);
143 assertThat(controlLab.getConnectedPortName(), is(portName));
144 }
145
146 @Test
147 public void testToString() {
148 assertThat(controlLab + "", containsString("Port=null"));
149 }
150
151 @Test
152 public void testToStringWhenConnected() throws Exception {
153 final String portName = "cool_port_1";
154 when(portFactory.getSerialPort(portName)).thenReturn(new JsscSerialPort(innerSerialPort));
155 when(innerSerialPort.getPortName()).thenReturn(portName);
156 when(listener.isHandshakeSeen()).thenAnswer(i -> true);
157 controlLab.open(portName);
158 assertThat(controlLab + "", containsString(portName));
159 }
160 }