use of org.apache.commons.io.input.ReaderInputStream in project sw360portal by sw360.
the class CombinedCLIParserTest method testGetCLI.
@Test
public void testGetCLI() throws Exception {
Attachment cliAttachment = new Attachment("A1", "a.xml");
when(connector.getAttachmentStream(anyObject(), anyObject(), anyObject())).thenReturn(new ReaderInputStream(new StringReader(cliTestfile)));
List<LicenseInfoParsingResult> results = parser.getLicenseInfos(cliAttachment, new User(), new Project());
assertThat(results.size(), is(1));
LicenseInfoParsingResult res = results.get(0);
assertLicenseInfoParsingResult(res);
assertThat(res.getLicenseInfo().getFilenames(), containsInAnyOrder("a.xml"));
assertThat(res.getLicenseInfo().getLicenseNamesWithTexts().size(), is(3));
assertThat(res.getLicenseInfo().getLicenseNamesWithTexts().stream().map(LicenseNameWithText::getLicenseText).collect(Collectors.toSet()), containsInAnyOrder("License1Text", "License2Text", "License3&'Text"));
LicenseNameWithText l2 = res.getLicenseInfo().getLicenseNamesWithTexts().stream().filter(l -> l.getLicenseName().equals("License2")).findFirst().orElseThrow(AssertionError::new);
assertThat(l2.getAcknowledgements(), is("License2Acknowledgements"));
assertThat(res.getLicenseInfo().getCopyrights().size(), is(5));
assertThat(res.getLicenseInfo().getCopyrights(), containsInAnyOrder("Copyright1", "Copyright2", "Copyright3", "Copyright4", "Copyright5"));
assertThat(res.getVendor(), is("VendorA"));
assertThat(res.getName(), is("r1"));
assertThat(res.getVersion(), is("1.0"));
}
use of org.apache.commons.io.input.ReaderInputStream in project sw360portal by sw360.
the class CLIParserTest method testIsApplicableTo.
@Test
public void testIsApplicableTo() throws Exception {
when(connector.getAttachmentStream(eq(content), anyObject(), anyObject())).thenReturn(new ReaderInputStream(new StringReader(CLI_TESTFILE)));
assertTrue(parser.isApplicableTo(attachment, new User(), new Project()));
}
use of org.apache.commons.io.input.ReaderInputStream in project sw360portal by sw360.
the class CLIParserTest method testIsApplicableToFailsOnMalformedXML.
@Test
public void testIsApplicableToFailsOnMalformedXML() throws Exception {
AttachmentContent content = new AttachmentContent().setId("A1").setFilename("a.xml").setContentType("application/xml");
when(connector.getAttachmentStream(eq(content), anyObject(), anyObject())).thenReturn(new ReaderInputStream(new StringReader("this is not an xml file")));
assertFalse(parser.isApplicableTo(attachment, new User(), new Project()));
}
use of org.apache.commons.io.input.ReaderInputStream in project midpoint by Evolveum.
the class ImportProducerWorker method processStream.
private void processStream(InputStream input) throws IOException {
ApplicationContext appContext = context.getApplicationContext();
PrismContext prismContext = appContext.getBean(PrismContext.class);
MatchingRuleRegistry matchingRuleRegistry = appContext.getBean(MatchingRuleRegistry.class);
EventHandler<T> handler = new EventHandler<>() {
@Override
public EventResult preMarshall(Element objectElement, Node postValidationTree, OperationResult objectResult) {
currentOid = objectElement.getAttribute("oid");
return EventResult.cont();
}
@Override
public EventResult postMarshall(T object, Element objectElement, OperationResult objectResult) {
try {
if (filter != null) {
boolean match = ObjectQuery.match(object, filter, matchingRuleRegistry);
if (!match) {
operation.incrementSkipped();
return EventResult.skipObject("Object doesn't match filter");
}
}
if (!matchSelectedType(object.getClass())) {
operation.incrementSkipped();
return EventResult.skipObject("Type doesn't match");
}
queue.put(object);
} catch (Exception ex) {
throw new NinjaException(getErrorMessage() + ", reason: " + ex.getMessage(), ex);
}
currentOid = null;
return stopAfterFound ? EventResult.skipObject() : EventResult.cont();
}
@Override
public void handleGlobalError(OperationResult currentResult, Exception cause) {
// This should not
// Should we log error?
operation.incrementError();
String message = getErrorMessage();
if (continueOnInputError) {
if (context.isVerbose()) {
context.getLog().error(message, cause);
} else {
context.getLog().error(message + ", reason: {}", cause.getMessage());
}
} else {
// We need to throw runtime exception in order to stop validator, otherwise validator will continue
// fill queue and this may result in deadlock
operation.finish();
throw new NinjaException(message + ", reason: " + cause.getMessage(), cause);
}
}
};
// FIXME: MID-5151: If validateSchema is false we are not validating unknown attributes on import
LegacyValidator<?> validator = new LegacyValidator<>(prismContext, handler);
validator.setValidateSchema(false);
OperationResult result = operation.getResult();
Charset charset = context.getCharset();
Reader reader = new InputStreamReader(input, charset);
validator.validate(new ReaderInputStream(reader, charset), result, result.getOperation());
}
use of org.apache.commons.io.input.ReaderInputStream in project pentaho-platform by pentaho.
the class ClientUtilsTest method getHttpClientTest.
@Test
public void getHttpClientTest() {
HttpResponse httpResponseMock = mock(HttpResponse.class);
HttpEntity httpEntityMock = mock(HttpEntity.class);
StatusLine statusLineMock = mock(StatusLine.class);
HttpClient httpClientMock = mock(HttpClient.class);
HttpGet method = mock(HttpGet.class);
try {
when(httpResponseMock.getStatusLine()).thenReturn(statusLineMock);
when(httpClientMock.execute(any(HttpUriRequest.class))).thenReturn(httpResponseMock);
when(statusLineMock.getStatusCode()).thenReturn(OK_CODE);
when(httpResponseMock.getStatusLine()).thenReturn(statusLineMock);
InputStream inputStream = new ReaderInputStream(new StringReader(XML_TEXT));
when(httpResponseMock.getEntity()).thenReturn(httpEntityMock);
when(httpEntityMock.getContent()).thenReturn(inputStream);
Document document = ClientUtil.getResultDom4jDocument(httpClientMock, method);
assertTrue(document.getRootElement().getName().equals("Level1"));
} catch (IOException | ServiceException e) {
assertTrue("Shouldn't have thrown exception here", false);
}
}
Aggregations