MapToIObjectResolveDependencyStrategy implements IResolveDependencyStrategy { @Override public <T> T resolve(final Object... args) throws ResolveDependencyStrategyException { try { Map<String, Object> stringObjectMap = (Map<String, Object>) args[0]; Map<IFieldName, Object> fieldNameObjectMap = new HashMap<>(); for (String key: stringObjectMap.keySet()) { IFieldName fieldName = IOC.resolve(Keys.getOrAdd("info.smart_tools.smartactors.iobject.ifield_name.IFieldName"), key); fieldNameObjectMap.put(fieldName, stringObjectMap.get(key)); } return (T) new DSObject(fieldNameObjectMap); } catch (Exception e) { throw new ResolveDependencyStrategyException("Can't create IObject from Map.", e); } }  @Override T resolve(final Object... args);  }
@Test public void ShouldCorrectConvertMapToIObject() throws Exception { IKey key = mock(IKey.class); when(Keys.getOrAdd("info.smart_tools.smartactors.iobject.ifield_name.IFieldName")).thenReturn(key); IFieldName fieldName1 = mock(IFieldName.class); IFieldName fieldName2 = mock(IFieldName.class); when(IOC.resolve(key, "key1")).thenReturn(fieldName1); when(IOC.resolve(key, "key2")).thenReturn(fieldName2); Map<String, Object> map = new LinkedHashMap<>(); map.put("key1", 123); map.put("key2", "value"); IObject result = strategy.resolve(map); assertEquals(result.getValue(fieldName1), 123); assertEquals(result.getValue(fieldName2), "value"); }
@Test(expected = ResolveDependencyStrategyException.class) public void ShouldThrowException_When_AnyErrorIsOccurred() throws Exception { when(IOC.resolve(any(IKey.class), anyString())).thenThrow(new ResolutionException("")); Map<String, Object> map = new HashMap<>(); map.put("key", "value"); strategy.resolve(map); fail(); }
@Test(expected = ResolveDependencyStrategyException.class) public void ShouldThrowException_When_NullIsPassed() throws Exception { strategy.resolve(null); fail(); }