private String marshal() { String result = null; ByteArrayOutputStream out = null; try { JAXBContext jaxbContext = JAXBContext.newInstance(Mapping.class); Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE); out = new ByteArrayOutputStream(); Mapping mapping = new Mapping(); MappingAction action = new MappingAction(); action.setName("ReplaceCustomerId"); action.setActionClassName("com.apifest.example.ReplaceCustomerIdAction"); MappingAction action2 = new MappingAction(); action2.setName("AddSenderIdInBody"); action2.setActionClassName("com.apifest.example.AddSenderIdInBody"); List<MappingAction> actions = new ArrayList<MappingAction>(); actions.add(action); actions.add(action2); ActionsWrapper allActions = new ActionsWrapper(); allActions.setActions(actions); mapping.setActionsWrapper(allActions); MappingEndpoint endpoint = new MappingEndpoint(); MappingAction addAction = new MappingAction(); addAction.setName("ReplaceCustomerId"); endpoint.setAction(addAction); endpoint.setInternalEndpoint("/v0.1/customer/{customerId}"); endpoint.setExternalEndpoint("/v0.1/me"); EndpointsWrapper endpointWrapper = new EndpointsWrapper(); List<MappingEndpoint> endpoints = new ArrayList<MappingEndpoint>(); endpoints.add(endpoint); endpointWrapper.setEndpoints(endpoints); mapping.setEndpointsWrapper(endpointWrapper); marshaller.marshal(mapping, out); result = out.toString("UTF-8"); } catch (JAXBException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } finally { if (out != null) { try { out.close(); } catch (IOException e) { e.printStackTrace(); } } } return result; }
@Test public void when_action_class_is_not_null_do_not_invoke_getAction_from_actions_map() throws Exception { // GIVEN ConfigLoader.loadMappingsConfig(false); MappingAction action = new MappingAction(); action.setName("testAction"); action.setActionClassName("com.apifest.example.AddSenderIdInBodyAction"); ConfigLoader.jarClassLoader = mock(URLClassLoader.class); doReturn(AddSenderIdInBodyAction.class) .when(ConfigLoader.jarClassLoader) .loadClass(action.getActionClassName()); // WHEN BasicAction actionClass = ConfigLoader.getConfig().get(0).getAction(action); // THEN assertTrue(actionClass instanceof AddSenderIdInBodyAction); }
@Test public void when_actionClassname_is_null_get_className_from_actions_config() throws Exception { // GIVEN ConfigLoader.loadMappingsConfig(false); MappingAction mappingAction = new MappingAction(); mappingAction.setName("ReplaceCustomerId"); MappingEndpoint endpoint = new MappingEndpoint(); endpoint.setAction(mappingAction); ConfigLoader.jarClassLoader = mock(URLClassLoader.class); doReturn(ReplaceCustomerIdAction.class) .when(ConfigLoader.jarClassLoader) .loadClass(ReplaceCustomerIdAction.class.getCanonicalName()); // WHEN BasicAction action = ConfigLoader.getConfig().get(0).getAction(mappingAction); // THEN assertTrue(action instanceof ReplaceCustomerIdAction); }