protected String loadResourceAsString(String resourceName) throws IOException { return IOUtils.getResourceAsString(resourceName, getClass()); }
private void executeGroovyScript() { try { GroovyShell shell = new GroovyShell(); resolvePropertiesUsingLambdas(); properties.forEach((key, value) -> shell.setProperty(key, value)); shell.evaluate(IOUtils.getResourceAsString(scriptPath, GroovyScriptExecutor.class)); LOGGER.info("Groovy script executed"); } catch (IOException e) { throw new RuntimeException("Error reading Groovy script: " + scriptPath, e); } catch (CompilationFailedException e) { throw new RuntimeException("Compilation error were found on: " + scriptPath, e); } }
@Override public String getObject() throws Exception { String returnData = content; if (file != null) { try { returnData = getResourceAsString(file, getClass()); } catch (IOException e) { throw new MuleRuntimeException(createStaticMessage("Failed to load test-data resource: " + file), e); } } return returnData; }
@Before public void setup() throws IOException { expectedSchema = getResourceAsString("schemas/" + expectedXSD, getClass()); }
@Before public void setup() throws IOException { generator = new ExtensionModelJsonSerializer(true); expectedJson = getResourceAsString("models/" + expectedSource, getClass()).trim(); }
@Before public void setUp() throws IOException { jsonSerializer = new ExtensionModelJsonSerializer(true); expectedJson = getResourceAsString(expectedSource, ModuleExtensionModelJsonTestCase.class).trim(); }
@Test public void parseTemplateFromLocationWithKnownMediaType() throws Exception { parseTemplateProcessor.setLocation(LOCATION); parseTemplateProcessor.initialise(); String expectedExpression = IOUtils.getResourceAsString(LOCATION, this.getClass()); when(mockExpressionManager.parseLogTemplate(eq(expectedExpression), eq(event), any(), any())).thenReturn("Parsed"); CoreEvent response = parseTemplateProcessor.process(event); assertThat(response.getMessage().getPayload().getDataType().getMediaType(), is(equalTo(LOCATION_MEDIA_TYPE))); }
@Test public void parseTemplateFromLocationWithUnknownMediaType() throws Exception { parseTemplateProcessor.setLocation(UNKNOWN_MEDIATYPE_LOCATION); parseTemplateProcessor.initialise(); String expectedExpression = IOUtils.getResourceAsString(UNKNOWN_MEDIATYPE_LOCATION, this.getClass()); when(mockExpressionManager.parseLogTemplate(eq(expectedExpression), eq(event), any(), any())).thenReturn("Parsed"); CoreEvent response = parseTemplateProcessor.process(event); assertThat(response.getMessage().getPayload().getDataType().getMediaType(), is(equalTo(ANY))); }
@Test public void parseTemplateFromLocation() throws InitialisationException, IOException { parseTemplateProcessor.setLocation(LOCATION); parseTemplateProcessor.initialise(); when(mockMuleMessage.getInboundProperty("errorMessage")).thenReturn("ERROR!!!"); String expectedExpression = IOUtils.getResourceAsString(LOCATION, this.getClass()); when(mockMuleMessage.getPayload()).thenReturn(of("Parsed")); when(mockMuleMessage.getAttributes()).thenReturn(of(new HashMap<>())); when(mockExpressionManager.parseLogTemplate(eq(expectedExpression), eq(event), any(), any())).thenReturn("Parsed"); CoreEvent response = parseTemplateProcessor.process(event); assertNotNull(response); assertEquals("Parsed", response.getMessage().getPayload().getValue()); // Call a second time to make sure the template is stored once the transformer has been initialized response = parseTemplateProcessor.process(event); assertNotNull(response); assertEquals("Parsed", response.getMessage().getPayload().getValue()); }
@Test public void parseTemplateWithOverriddenDataTypeAsExpression() throws Exception { String customEncoding = "UTF-16"; MediaType customMediaType = create("application", "lrmextension"); parseTemplateProcessor.setLocation(UNKNOWN_MEDIATYPE_LOCATION); parseTemplateProcessor.setOutputMimeType(customMediaType.toRfcString()); parseTemplateProcessor.setOutputEncoding(customEncoding); parseTemplateProcessor.initialise(); String expectedExpression = IOUtils.getResourceAsString(UNKNOWN_MEDIATYPE_LOCATION, this.getClass()); when(mockExpressionManager.parseLogTemplate(eq(expectedExpression), eq(event), any(), any())).thenReturn("Parsed"); CoreEvent response = parseTemplateProcessor.process(event); assertThat(response.getMessage().getPayload().getDataType().getMediaType().getPrimaryType(), is(equalTo(customMediaType.getPrimaryType()))); assertThat(response.getMessage().getPayload().getDataType().getMediaType().getSubType(), is(equalTo(customMediaType.getSubType()))); assertThat(response.getMessage().getPayload().getDataType().getMediaType().getCharset().get().toString(), is(equalTo(customEncoding))); }
@Test public void parseTemplateWithOverriddenDataType() throws Exception { String customEncoding = "UTF-16"; MediaType customMediaType = create("application", "lrmextension"); parseTemplateProcessor.setLocation(UNKNOWN_MEDIATYPE_LOCATION); parseTemplateProcessor.setOutputMimeType(customMediaType.toRfcString()); parseTemplateProcessor.setOutputEncoding(customEncoding); parseTemplateProcessor.initialise(); String expectedExpression = IOUtils.getResourceAsString(UNKNOWN_MEDIATYPE_LOCATION, this.getClass()); when(mockExpressionManager.parseLogTemplate(eq(expectedExpression), eq(event), any(), any())).thenReturn("Parsed"); CoreEvent response = parseTemplateProcessor.process(event); assertThat(response.getMessage().getPayload().getDataType().getMediaType().getPrimaryType(), is(equalTo(customMediaType.getPrimaryType()))); assertThat(response.getMessage().getPayload().getDataType().getMediaType().getSubType(), is(equalTo(customMediaType.getSubType()))); assertThat(response.getMessage().getPayload().getDataType().getMediaType().getCharset().get().toString(), is(equalTo(customEncoding))); }
IOUtils.getResourceAsString(moduleNamePrefix + XSD_SUFFIX, classLoader), extensionModel.getName() }; } catch (IOException e) {
private void loadContentFromLocation() throws InitialisationException { try { if (location == null) { throw new IllegalArgumentException("Location cannot be null"); } content = IOUtils.getResourceAsString(location, this.getClass()); } catch (Exception e) { throw new InitialisationException(e, this); } }
@Override public void initialise() throws InitialisationException { if (expressionFile != null) { try { expression = getResourceAsString(expressionFile, getClass()); } catch (IOException e) { throw new InitialisationException(e, this); } } else if (expression == null) { throw new InitialisationException(objectIsNull("expression"), this); } }
@Before public void setup() throws IOException { expectedSchema = getResourceAsString("schemas/" + expectedXSD, getClass()); }
@Before public void setup() throws IOException { generator = new ExtensionModelJsonSerializer(true); expectedJson = getResourceAsString("models/" + expectedSource, getClass()).trim(); }
private void loadGlobalFunctions() throws InitialisationException { // Global functions defined in external file if (globalFunctionsFile != null) { try { globalFunctions.putAll(CompilerTools .extractAllDeclaredFunctions(new ExpressionCompiler(IOUtils.getResourceAsString(globalFunctionsFile, getClass())) .compile())); } catch (IOException e) { throw new InitialisationException(CoreMessages.failedToLoad(globalFunctionsFile), e, this); } } // Global functions defined in configuration file (take precedence over functions in file) globalFunctions.putAll(CompilerTools.extractAllDeclaredFunctions(new ExpressionCompiler(globalFunctionsString).compile())); }
@Test public void testParseTemplateFromLocation() throws InitialisationException, IOException { parseTemplateProcessor.setLocation(LOCATION); parseTemplateProcessor.initialise(); when(mockMuleMessage.getInboundProperty("errorMessage")).thenReturn("ERROR!!!"); String expectedExpression = IOUtils.getResourceAsString(LOCATION, this.getClass()); when(mockMuleMessage.getPayload()).thenReturn(TypedValue.of("Parsed")); when(mockMuleMessage.getAttributes()).thenReturn(TypedValue.of(new HashMap<>())); when(mockExpressionManager.parseLogTemplate(eq(expectedExpression), eq(event), any(), any())).thenReturn("Parsed"); CoreEvent response = parseTemplateProcessor.process(event); assertNotNull(response); assertEquals("Parsed", response.getMessage().getPayload().getValue()); // Call a second time to make sure the template is stored once the transformer has been initialized response = parseTemplateProcessor.process(event); assertNotNull(response); assertEquals("Parsed", response.getMessage().getPayload().getValue()); }