Java tutorial
/* * Copyright (c) 2015 Intel Ltd. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.sfc.bootstrap; import static junit.framework.Assert.assertTrue; import static org.junit.Assert.assertEquals; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import org.json.JSONException; import org.json.JSONObject; import org.junit.Test; import org.junit.runner.RunWith; import org.opendaylight.sfc.provider.config.SfcProviderConfig; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.sun.jersey.api.client.ClientHandlerException; /** * This class contains unit test for SfcProviderBootstrapRestAPI * * @author Vladimir Lavor * @version 0.1 * @see SfcProviderBootstrapRestAPI * @since 2015-05-28 */ @RunWith(PowerMockRunner.class) @PrepareForTest(SfcProviderConfig.class) public class SfcProviderBootstrapRestAPITest { private static final Logger LOG = LoggerFactory.getLogger(SfcProviderBootstrapRestAPI.class); /* * Method putBootstrapData reads sfc_provider_config.json file from specific location, this * location does not exists, * so method is mocked and put file from /resources. This json contains info about url address. * Second json file, ipfix-class-id.json contains data. Method creates json data file from these * sources and put * it as a rest to specified url. */ @Test public void testReadJsonFiles() { String configOriginalPath = "sfc-provider/src/test/resources/SfcProviderConfig/sfc_provider_config_test.json"; String jsonConfigString = null; JSONObject configFile = null; byte[] encoded = null; // create json file. File is slightly changed because original path in bootstrapDataDir does // not exists Path providerPath = Paths.get(configOriginalPath); try { encoded = Files.readAllBytes(providerPath); jsonConfigString = new String(encoded, StandardCharsets.UTF_8); } catch (IOException e) { LOG.error("Failed to...", e); } if (encoded != null) { try { configFile = new JSONObject(jsonConfigString); } catch (JSONException e) { LOG.error("Error instantiating {}", jsonConfigString, e); } } if (configFile != null) { try { configFile = configFile.getJSONObject("bootstrap"); } catch (JSONException e) { LOG.error("Error retrieving bootstrap object", e); } } // first mock returns true when method tries to find json file (is does not exists), second // puts created json as a result PowerMockito.stub(PowerMockito.method(SfcProviderConfig.class, "readConfigFile")).toReturn(true); PowerMockito.stub(PowerMockito.method(SfcProviderConfig.class, "getJsonBootstrapObject")) .toReturn(configFile); /* * Actual test. It checks, if both json files has been read successfully, contain all * necessary data and if * the rest json file has been created. If so, this file is then PUT to url address location * - this step needs running * sfc-karaf (or any server for test purposes). It is not running - so method should throw * ClientHandlerException. * If so, test catch that exception, check it and consider test to pass (all steps like * reading json etc. were successful). * If some other exception is thrown (or none), test will fail. */ try { // SfcProviderBootstrapRestAPI.getPutBootstrapData(new Object[0], new Class[0]); SfcProviderBootstrapRestAPI sfcProviderBootstrapRestAPI = new SfcProviderBootstrapRestAPI(new Object[0], new Class[0], "param"); sfcProviderBootstrapRestAPI.putBootstrapData(); } catch (Exception e) { if (e.getClass() == ClientHandlerException.class) { assertEquals("Must be equal", e.getClass(), (ClientHandlerException.class)); assertTrue("Must be true", e.getCause().getMessage().toLowerCase().contains("connection refused")); } else // test is ok in IDE, build throws null pointer, don't know why assertEquals("Must be equal", e.getClass(), (NullPointerException.class)); } } }