@Before public void setUp() throws Exception { device = mock(Device.class); manager = mock(Manager.class); ManagedSet defaultManagedSet = mock(ManagedSet.class); when(manager.getManagedSet(Constants.DEFAULT_MANAGED_SET)).thenReturn(defaultManagedSet); appliance = new WamtAppliance(manager, device); SecurityContext.setContext("test-user"); EntityManager manager = PersistenceTools.setPersistenceContext(); ActionStatusImpl status = mock(ActionStatusImpl.class); when(manager.find(ActionStatusImpl.class, new Long(0))).thenReturn(status); }
@Test public void getBestFirmware() throws DeletedException { // Set up a whole menagerie of mocked firmwares and versions. // Two kinds of firmware, featuresABC and features BCD. Each has two // levels, one of which overlap (both have a v1). Each is thus the best // choice for the level that only it has available; a choice has to be // made for v1 and we arbitrarily say that featuresABC is best at that // level. final FirmwareVersion ABCv1 = mock(FirmwareVersion.class); when(ABCv1.getLevel()).thenReturn("v1"); final FirmwareVersion ABCv3 = mock(FirmwareVersion.class); when(ABCv3.getLevel()).thenReturn("v3"); final FirmwareVersion BCDv1 = mock(FirmwareVersion.class); when(BCDv1.getLevel()).thenReturn("v1"); final FirmwareVersion BCDv2 = mock(FirmwareVersion.class); when(BCDv2.getLevel()).thenReturn("v2"); Firmware featuresABC = mock(Firmware.class); when(featuresABC.getVersions()).thenReturn(new FirmwareVersion[] {ABCv1, ABCv3}); Firmware featuresBCD = mock(Firmware.class); when(featuresBCD.getVersions()).thenReturn(new FirmwareVersion[] {BCDv1, BCDv2}); when(manager.getFirmwares()).thenReturn(new Firmware[] {featuresABC, featuresBCD}); when(manager.getBestFirmware(null, null, null, "v1")).thenReturn(featuresABC); when(manager.getBestFirmware(null, null, null, "v2")).thenReturn(featuresBCD); when(manager.getBestFirmware(null, null, null, "v3")).thenReturn(featuresABC); // Add other mocked methods necessary to support calls that WamtAppliance // and WamtFirmware will make. when(featuresABC.getLevel(anyString())) .thenAnswer( new Answer<FirmwareVersion>() { public FirmwareVersion answer(InvocationOnMock invocation) throws Throwable { if (invocation.getArguments()[0].equals("v1")) return ABCv1; if (invocation.getArguments()[0].equals("v3")) return ABCv3; return null; } }); when(featuresBCD.getLevel(anyString())) .thenAnswer( new Answer<FirmwareVersion>() { public FirmwareVersion answer(InvocationOnMock invocation) throws Throwable { if (invocation.getArguments()[0].equals("v1")) return BCDv1; if (invocation.getArguments()[0].equals("v2")) return BCDv2; return null; } }); when(BCDv1.getAbsoluteDisplayName()).thenReturn("featuresBCD level v1"); when(BCDv2.getAbsoluteDisplayName()).thenReturn("featuresBCD level v2"); when(ABCv1.getAbsoluteDisplayName()).thenReturn("featuresABC level v1"); when(ABCv3.getAbsoluteDisplayName()).thenReturn("featuresABC level v3"); // Now, finally, do the test. We should get exactly one best firmware // for each level, v1, v2, v3. List<SvrFirmware> bestFirmwares = appliance.getBestFirmwareVersions(); assertTrue(bestFirmwares.size() == 3); assertEquals("featuresABC level v1", bestFirmwares.get(0).getDisplayName()); assertEquals("featuresBCD level v2", bestFirmwares.get(1).getDisplayName()); assertEquals("featuresABC level v3", bestFirmwares.get(2).getDisplayName()); }