DeserializeStrategyGet implements IAddRequestParametersToIObject { @Override public void extract(final IObject message, final Object requestObject) throws AddRequestParametersToIObjectException { FullHttpRequest request = (FullHttpRequest) requestObject; if (!request.uri().contains("/")) { return; } QueryStringDecoder decoder = new QueryStringDecoder( request.uri().substring(request.uri().indexOf("/"))); decoder.parameters().forEach( (k, v) -> { try { IFieldName fieldName = IOC.resolve(Keys.getOrAdd("info.smart_tools.smartactors.iobject.ifield_name.IFieldName"), k); message.setValue(fieldName, v.get(0)); } catch (ResolutionException | ChangeValueException | InvalidArgumentException e) { throw new RuntimeException(e); } } ); Map<String, String> parameters = tree.match(decoder.path()); parameters.forEach( (k, v) -> { try { IFieldName fieldName = IOC.resolve(Keys.getOrAdd("info.smart_tools.smartactors.iobject.ifield_name.IFieldName"), k); message.setValue(fieldName, v); } catch (ResolutionException | ChangeValueException | InvalidArgumentException e) { throw new RuntimeException(e); } } ); return; } DeserializeStrategyGet(final List<String> templates); @Override void extract(final IObject message, final Object requestObject);  }
@Test public void testUriWithEmptyArgs() throws DeserializationException, ResolutionException, InvalidArgumentException, RegistrationException, ChangeValueException, AddRequestParametersToIObjectException { IOC.register(Keys.getOrAdd("EmptyIObject"), new SingletonStrategy( emptyIObject ) ); IFieldName fieldName = new FieldName("messageMapId"); String arg = "/hello"; when(decoder.uri()).thenReturn(arg); when(decoder.parameters()).thenReturn(new HashMap<>()); Map<String, String> outputMap = new HashMap<>(); outputMap.put("messageMapId", "hello"); when(parseTree.match(arg)).thenReturn(outputMap); String testUri = "www.www.ru/hello"; httpRequest = mock(FullHttpRequest.class); when(httpRequest.uri()).thenReturn(testUri); DeserializeStrategyGet deserializeStrategyGet = new DeserializeStrategyGet(new ArrayList<>()); deserializeStrategyGet.extract(emptyIObject, httpRequest); verify(emptyIObject).setValue(fieldName, "hello"); }
@Test public void testUriWithArgs() throws DeserializationException, InvalidArgumentException, ReadValueException, ResolutionException, RegistrationException, AddRequestParametersToIObjectException { String testUri = "www.www.ru/hello?hello=world"; httpRequest = mock(FullHttpRequest.class); when(httpRequest.uri()).thenReturn(testUri); String arg = "/hello"; when(decoder.uri()).thenReturn(arg); Map<String, List<String>> parameters = new HashMap<>(); parameters.put("hello", Collections.singletonList("world")); when(decoder.parameters()).thenReturn(parameters); Map<String, String> outputMap = new HashMap<>(); outputMap.put("messageMapId", "hello"); when(parseTree.match(arg)).thenReturn(outputMap); DeserializeStrategyGet deserializeStrategyGet = new DeserializeStrategyGet(new ArrayList<>()); IObject iObject = new DSObject(); deserializeStrategyGet.extract(iObject, httpRequest); assertEquals((iObject.getValue(new FieldName("hello"))), "world"); assertEquals(iObject.getValue(new FieldName("messageMapId")), "hello"); }
@Test public void testUriWithoutArgs() throws DeserializationException, InvalidArgumentException, ReadValueException, ResolutionException, RegistrationException, AddRequestParametersToIObjectException { IOC.register(Keys.getOrAdd("EmptyIObject"), new SingletonStrategy( emptyIObject ) ); String testUri = "www.www.ru"; httpRequest = mock(FullHttpRequest.class); when(httpRequest.uri()).thenReturn(testUri); DeserializeStrategyGet deserializeStrategyGet = new DeserializeStrategyGet(new ArrayList<>()); IObject iObject = emptyIObject; deserializeStrategyGet.extract(iObject, httpRequest); assertEquals(iObject, this.emptyIObject); }