You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@brooklyn.apache.org by neykov <gi...@git.apache.org> on 2017/01/03 14:33:59 UTC

[GitHub] brooklyn-server pull request #502: BROOKLYN-421: add tests for catalog libra...

Github user neykov commented on a diff in the pull request:

    https://github.com/apache/brooklyn-server/pull/502#discussion_r94413657
  
    --- Diff: camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/catalog/CatalogOsgiLibraryTest.java ---
    @@ -0,0 +1,286 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + *     http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing,
    + * software distributed under the License is distributed on an
    + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    + * KIND, either express or implied.  See the License for the
    + * specific language governing permissions and limitations
    + * under the License.
    + */
    +package org.apache.brooklyn.camp.brooklyn.catalog;
    +
    +import static org.testng.Assert.assertEquals;
    +
    +import java.io.InputStream;
    +import java.net.URL;
    +import java.nio.charset.StandardCharsets;
    +import java.util.Map;
    +
    +import org.apache.brooklyn.api.catalog.CatalogItem;
    +import org.apache.brooklyn.api.catalog.CatalogItem.CatalogBundle;
    +import org.apache.brooklyn.api.mgmt.ManagementContext;
    +import org.apache.brooklyn.camp.brooklyn.AbstractYamlTest;
    +import org.apache.brooklyn.core.config.external.AbstractExternalConfigSupplier;
    +import org.apache.brooklyn.core.mgmt.internal.ExternalConfigSupplierRegistry;
    +import org.apache.brooklyn.core.mgmt.internal.ManagementContextInternal;
    +import org.apache.brooklyn.core.mgmt.osgi.OsgiVersionMoreEntityTest;
    +import org.apache.brooklyn.entity.stock.BasicApplication;
    +import org.apache.brooklyn.test.Asserts;
    +import org.apache.brooklyn.test.support.TestResourceUnavailableException;
    +import org.apache.brooklyn.util.core.http.BetterMockWebServer;
    +import org.apache.brooklyn.util.stream.Streams;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +import org.testng.annotations.AfterMethod;
    +import org.testng.annotations.BeforeMethod;
    +import org.testng.annotations.Test;
    +
    +import com.google.common.collect.ImmutableMap;
    +import com.google.common.collect.Iterables;
    +import com.google.common.io.BaseEncoding;
    +import com.google.common.net.UrlEscapers;
    +import com.google.mockwebserver.Dispatcher;
    +import com.google.mockwebserver.MockResponse;
    +import com.google.mockwebserver.RecordedRequest;
    +
    +public class CatalogOsgiLibraryTest extends AbstractYamlTest {
    +    
    +    @SuppressWarnings("unused")
    +    private static final Logger log = LoggerFactory.getLogger(CatalogOsgiLibraryTest.class);
    +
    +    private BetterMockWebServer webServer;
    +
    +    private String jarName = "brooklyn-test-osgi-entities.jar";
    +    private String malformedJarName = "thisIsNotAJar.jar";
    +    private String classpathUrl = "classpath:/brooklyn/osgi/" + jarName;
    +    private URL jarUrl;
    +    private URL malformedJarUrl;
    +    
    +    @BeforeMethod(alwaysRun = true)
    +    @Override
    +    public void setUp() throws Exception {
    +        super.setUp();
    +        
    +        // Load the bytes of the jar
    +        TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), "/brooklyn/osgi/" + jarName);
    +        InputStream resource = getClass().getResourceAsStream("/brooklyn/osgi/" + jarName);
    +        final byte[] jarBytes = Streams.readFullyAndClose(resource);
    +        
    +        // Start a mock web-server that will return the jar
    +        webServer = BetterMockWebServer.newInstanceLocalhost();
    +        webServer.setDispatcher(new Dispatcher() {
    +            @Override
    +            public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
    +                if (request.getPath().contains(jarName)) {
    +                    return new MockResponse().setResponseCode(200).setBody(jarBytes);
    +                } else if (request.getPath().contains(malformedJarName)) {
    +                    return new MockResponse().setResponseCode(200).setBody("simulating-malformed-jar");
    +                } else {
    +                    return new MockResponse().setResponseCode(404).setBody(request.getPath() + " not found");
    +                }
    +            }});
    --- End diff --
    
    FYI, there's [TestHttpServer](https://github.com/apache/brooklyn-server/blob/master/utils/common/src/main/java/org/apache/brooklyn/test/http/TestHttpServer.java) which is [a bit easier to set up](https://github.com/apache/brooklyn-server/blob/master/core/src/test/java/org/apache/brooklyn/util/core/ResourceUtilsHttpTest.java#L53-L68). 


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---