EnvironmentRamCalculator { public long calculate(@Nullable Environment environment) throws ServerException { if (environment == null) { return 0; } try { return getInternalEnvironment(environment) .getMachines() .values() .stream() .mapToLong( m -> parseMemoryAttributeValue(m.getAttributes().get(MEMORY_LIMIT_ATTRIBUTE))) .sum() / BYTES_TO_MEGABYTES_DIVIDER; } catch (InfrastructureException | ValidationException | NotFoundException ex) { throw new ServerException(ex.getMessage(), ex); } } @Inject  EnvironmentRamCalculator(Map<String, InternalEnvironmentFactory> environmentFactories); long calculate(@Nullable Environment environment); long calculate(Runtime runtime);  }
@Test public void testCalculatesRamOfEnvironmentWithMultipleMachines() throws Exception { when(machineConfig1.getAttributes()) .thenReturn(ImmutableMap.of(MEMORY_LIMIT_ATTRIBUTE, "2147483648")); when(machineConfig2.getAttributes()) .thenReturn(ImmutableMap.of(MEMORY_LIMIT_ATTRIBUTE, "536870912")); when(recipeMock.getType()).thenReturn(RECIPE_TYPE); final long ram = envRamCalculator.calculate(environment); assertEquals(ram, 2560); }
@Test public void testCalculatesRamOfEnvironmentWhenSomeMachineConfigHasNoAttribute() throws Exception { when(machineConfig1.getAttributes()).thenReturn(Collections.emptyMap()); when(machineConfig2.getAttributes()) .thenReturn(ImmutableMap.of(MEMORY_LIMIT_ATTRIBUTE, "536870912")); when(recipeMock.getType()).thenReturn(RECIPE_TYPE); final long ram = envRamCalculator.calculate(environment); assertEquals(ram, 512); }
@Test(expectedExceptions = ServerException.class) public void testThrowServerExceptionWhenNoEnvFactoryForGivenRecipeTypeFound() throws Exception { when(recipeMock.getType()).thenReturn("unsupported"); envRamCalculator.calculate(environment); }
@Test public void testCalculatesRamOfRuntimeWithMultipleMachines() throws Exception { when(machine1.getAttributes()).thenReturn(ImmutableMap.of(MEMORY_LIMIT_ATTRIBUTE, "805306368")); when(machine2.getAttributes()).thenReturn(ImmutableMap.of(MEMORY_LIMIT_ATTRIBUTE, "805306368")); final long ram = envRamCalculator.calculate(runtime); assertEquals(ram, 1536); }
@Test public void testCalculatesRamOfRuntimeWhenSomeMachineHasNoAttribute() throws Exception { when(machine1.getAttributes()).thenReturn(ImmutableMap.of(MEMORY_LIMIT_ATTRIBUTE, "805306368")); when(machine2.getAttributes()).thenReturn(Collections.emptyMap()); final long ram = envRamCalculator.calculate(runtime); assertEquals(ram, 768); }