Search in sources :

Example 6 with ReaderInputStream

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"));
}
Also used : Project(org.eclipse.sw360.datahandler.thrift.projects.Project) ReaderInputStream(org.apache.commons.io.input.ReaderInputStream) User(org.eclipse.sw360.datahandler.thrift.users.User) LicenseNameWithText(org.eclipse.sw360.datahandler.thrift.licenseinfo.LicenseNameWithText) StringReader(java.io.StringReader) Attachment(org.eclipse.sw360.datahandler.thrift.attachments.Attachment) LicenseInfoParsingResult(org.eclipse.sw360.datahandler.thrift.licenseinfo.LicenseInfoParsingResult) TestHelper.assertLicenseInfoParsingResult(org.eclipse.sw360.licenseinfo.TestHelper.assertLicenseInfoParsingResult) Test(org.junit.Test)

Example 7 with ReaderInputStream

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()));
}
Also used : Project(org.eclipse.sw360.datahandler.thrift.projects.Project) ReaderInputStream(org.apache.commons.io.input.ReaderInputStream) User(org.eclipse.sw360.datahandler.thrift.users.User) StringReader(java.io.StringReader) Test(org.junit.Test)

Example 8 with ReaderInputStream

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()));
}
Also used : Project(org.eclipse.sw360.datahandler.thrift.projects.Project) ReaderInputStream(org.apache.commons.io.input.ReaderInputStream) User(org.eclipse.sw360.datahandler.thrift.users.User) StringReader(java.io.StringReader) AttachmentContent(org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent) Test(org.junit.Test)

Example 9 with ReaderInputStream

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());
}
Also used : PrismContext(com.evolveum.midpoint.prism.PrismContext) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) NinjaException(com.evolveum.midpoint.ninja.impl.NinjaException) EventHandler(com.evolveum.midpoint.common.validator.EventHandler) Charset(java.nio.charset.Charset) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) NinjaException(com.evolveum.midpoint.ninja.impl.NinjaException) ApplicationContext(org.springframework.context.ApplicationContext) ReaderInputStream(org.apache.commons.io.input.ReaderInputStream) LegacyValidator(com.evolveum.midpoint.common.validator.LegacyValidator) MatchingRuleRegistry(com.evolveum.midpoint.prism.match.MatchingRuleRegistry)

Example 10 with ReaderInputStream

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);
    }
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) HttpEntity(org.apache.http.HttpEntity) ReaderInputStream(org.apache.commons.io.input.ReaderInputStream) InputStream(java.io.InputStream) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) Document(org.dom4j.Document) StatusLine(org.apache.http.StatusLine) ReaderInputStream(org.apache.commons.io.input.ReaderInputStream) HttpClient(org.apache.http.client.HttpClient) StringReader(java.io.StringReader) Test(org.junit.Test)

Aggregations

ReaderInputStream (org.apache.commons.io.input.ReaderInputStream)25 StringReader (java.io.StringReader)16 Test (org.junit.Test)15 Project (org.eclipse.sw360.datahandler.thrift.projects.Project)8 User (org.eclipse.sw360.datahandler.thrift.users.User)8 InputStream (java.io.InputStream)7 FileInputStream (java.io.FileInputStream)4 InputStreamReader (java.io.InputStreamReader)4 AttachmentContent (org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent)4 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)3 HttpEntity (org.apache.http.HttpEntity)3 StatusLine (org.apache.http.StatusLine)3 HttpGet (org.apache.http.client.methods.HttpGet)3 Attachment (org.eclipse.sw360.datahandler.thrift.attachments.Attachment)3 LicenseInfoParsingResult (org.eclipse.sw360.datahandler.thrift.licenseinfo.LicenseInfoParsingResult)3 TestHelper.assertLicenseInfoParsingResult (org.eclipse.sw360.licenseinfo.TestHelper.assertLicenseInfoParsingResult)3 EventHandler (com.evolveum.midpoint.common.validator.EventHandler)2 PrismContext (com.evolveum.midpoint.prism.PrismContext)2 MidPointApplication (com.evolveum.midpoint.web.security.MidPointApplication)2 WebApplicationConfiguration (com.evolveum.midpoint.web.security.WebApplicationConfiguration)2