BakeoutController-Basic  0.1
A Controller for the Omicron vacuum chamber
TestingConfiguration.java
1 package unit.ui;
2 
3 import devices.PowerSupply;
4 import kernel.Kernel;
10 import kernel.views.variables.*;
11 import org.jmock.Expectations;
12 import org.jmock.Mockery;
13 import org.jmock.integration.junit4.JUnit4Mockery;
14 import org.jmock.lib.concurrent.Synchroniser;
15 import org.springframework.context.annotation.*;
17 
18 import java.time.Duration;
19 import java.util.ArrayList;
20 import java.util.List;
21 
26 @Configuration
27 @Import(UserInterfaceConfiguration.class)
28 @Lazy
29 public class TestingConfiguration {
30 
34  private volatile Kernel mockKernel;
35 
39  private volatile Mockery mockingContext;
40 
44  private volatile List<String> portList;
45 
46  private volatile DeviceContainer mockDeviceContainerView;
47 
48  private volatile VariableProviderContainer mockVariableProviderContainer;
49 
50  private volatile MockVoltageProvider mockVoltageProvider =
51  new MockVoltageProvider();
52 
56  @Bean
57  @Scope("singleton")
58  public Mockery mockingContext(){
59  if(mockingContext == null) {
60  mockingContext = new SynchronizedJUnit4Mockery();
61  }
62  return mockingContext;
63  }
64 
68  @Bean
69  @Scope("singleton")
70  public List<String> testData(){
71  portList = new ArrayList<>();
72  portList.add("/dev/ttyUSB0");
73  return portList;
74  }
75 
79  @Bean
80  @Scope("singleton")
82  return mockingContext().mock(CommPortReporter.class);
83  }
84 
85  @Bean
86  @Scope("singleton")
87  public TDKLambdaPowerSupplyFactory tdkLambdaPowerSupplyFactory(){
88  return mockingContext().mock(TDKLambdaPowerSupplyFactory.class);
89  }
90 
91  @Bean
92  @Scope("singleton")
93  public PVCiPressureGaugeFactory pvCiPressureGaugeFactory(){
94  return mockingContext().mock(PVCiPressureGaugeFactory.class);
95  }
96 
97  @Bean
98  @Scope("singleton")
99  public DeviceContainer deviceRegistryView(){
100  if(mockDeviceContainerView == null){
101  mockDeviceContainerView = mockingContext().mock(
102  DeviceContainer.class
103  );
104  }
105  return mockDeviceContainerView;
106  }
107 
108  @Bean
109  @Scope("singleton")
110  public VariableProviderContainer variableProviderRegistry(){
111  if (mockVariableProviderContainer == null){
112  mockVariableProviderContainer = mockingContext().mock(
114  );
115  }
116  return mockVariableProviderContainer;
117  }
118 
119  @Bean
120  @Scope("singleton")
121  public PowerSupply powerSupply(){
122  return mockingContext().mock(PowerSupply.class);
123  }
124 
125  @Bean
126  @Scope("singleton")
127  public PressureProvider pressureProvider(){
128  return mockingContext().mock(PressureProvider.class);
129  }
130 
131  @Bean
132  @Scope("singleton")
133  public VoltageProvider voltageProvider(){
134  return mockVoltageProvider;
135  }
136 
140  @Bean
141  @Scope("singleton")
142  public Kernel kernel() throws Exception {
143  mockKernel = mockingContext().mock(Kernel.class);
144  mockingContext().checking(new ExpectationsForKernel());
145  return mockKernel;
146  }
147 
153  private class SynchronizedJUnit4Mockery extends JUnit4Mockery {
157  public SynchronizedJUnit4Mockery(){
158  setThreadingPolicy(new Synchroniser());
159  }
160  }
161 
166  private class ExpectationsForKernel extends Expectations {
170  public ExpectationsForKernel() throws Exception {
171  expectationsForPortReporter();
172  expectationsForSerialPortNames();
173  expectationsForFactory();
174  expectationsForPressureGaugeFactory();
175  expectationsForDeviceRegistryView();
176  expectationsForVariableProvider();
177  expectationsForGetPressureProvider();
178  expectationsForGetVoltageProvider();
179  }
180 
184  private void expectationsForPortReporter(){
185  allowing(mockKernel).getCommPortReporter();
186  will(returnValue(portReporter()));
187  }
188 
193  private void expectationsForSerialPortNames() {
194  allowing(portReporter()).getSerialPortNames();
195  will(returnValue(testData()));
196  }
197 
198  private void expectationsForFactory(){
199  allowing(mockKernel).getPowerSupplyFactory();
200  will(returnValue(tdkLambdaPowerSupplyFactory()));
201  }
202 
203  private void expectationsForDeviceRegistryView(){
204  allowing(mockKernel).getDeviceRegistryView();
205  will(returnValue(deviceRegistryView()));
206  }
207 
208  private void expectationsForPressureGaugeFactory(){
209  allowing(mockKernel).getPressureGaugeFactory();
210  will(returnValue(pvCiPressureGaugeFactory()));
211  }
212 
213  private void expectationsForVariableProvider(){
214  allowing(mockKernel).getVariableProvidersView();
215  will(returnValue(variableProviderRegistry()));
216  }
217 
218  private void expectationsForGetPressureProvider() throws Exception {
219  allowing(variableProviderRegistry()).getPressureProvider();
220  will(returnValue(pressureProvider()));
221 
222  allowing(variableProviderRegistry()).hasPressureProvider();
223  will(returnValue(Boolean.TRUE));
224 
225  allowing(pressureProvider()).setPollingInterval(
226  with(any(Duration.class))
227  );
228  allowing(pressureProvider()).addOnChangeListener(
229  with(any(VariableChangeEventListener.class))
230  );
231  }
232 
233  private void expectationsForGetVoltageProvider() throws Exception {
234  allowing(variableProviderRegistry()).getVoltageProvider();
235  will(returnValue(voltageProvider()));
236  allowing(variableProviderRegistry()).hasVoltageProvider();
237  will(returnValue(Boolean.TRUE));
238  }
239  }
240 }
Git Repo