aboutsummaryrefslogtreecommitdiffstats
path: root/graph-inventory/aai-client/src/main/java
diff options
context:
space:
mode:
Diffstat (limited to 'graph-inventory/aai-client/src/main/java')
0 files changed, 0 insertions, 0 deletions
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
/*-
 * ============LICENSE_START=======================================================
 * sdc-distribution-client
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * Modifications copyright (C) 2020 Nokia. All rights reserved.
 * ================================================================================
 * Licensed 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.
 * ============LICENSE_END=========================================================
 */

package org.onap.sdc.impl;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;

import fj.data.Either;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Assertions;
import org.junitpioneer.jupiter.SetEnvironmentVariable;
import org.mockito.Mockito;
import org.onap.sdc.api.IDistributionClient;
import org.onap.sdc.api.consumer.IConfiguration;
import org.onap.sdc.api.notification.IArtifactInfo;
import org.onap.sdc.api.notification.IVfModuleMetadata;
import org.onap.sdc.api.results.IDistributionClientResult;
import org.onap.sdc.http.HttpSdcClient;
import org.onap.sdc.http.SdcConnectorClient;
import org.onap.sdc.utils.ArtifactTypeEnum;
import org.onap.sdc.utils.ArtifactsUtils;
import org.onap.sdc.utils.DistributionActionResultEnum;
import org.onap.sdc.utils.Pair;
import org.onap.sdc.utils.TestConfiguration;
import org.onap.sdc.utils.TestNotificationCallback;
import org.onap.sdc.utils.Wrapper;
import org.onap.sdc.utils.kafka.KafkaDataResponse;
import org.onap.sdc.utils.kafka.SdcKafkaConsumer;

class DistributionClientTest {
    DistributionClientImpl client = Mockito.spy(new DistributionClientImpl());
    IConfiguration testConfiguration = new TestConfiguration();
    SdcConnectorClient connector = Mockito.mock(SdcConnectorClient.class);
	SdcKafkaConsumer consumer = mock(SdcKafkaConsumer.class);


    @AfterEach
    public void afterTest() {
        client.stop();
    }

    @Test
    void validateConfigurationTest() {
		final Pair<DistributionActionResultEnum, Configuration> distributionActionResultEnumConfigurationPair = client.validateAndInitConfiguration(
            new Wrapper<>(), testConfiguration);
        DistributionActionResultEnum validationResult = distributionActionResultEnumConfigurationPair.getFirst();
        Configuration configuration = distributionActionResultEnumConfigurationPair.getSecond();
        assertEquals(DistributionActionResultEnum.SUCCESS, validationResult);
        assertEquals(testConfiguration.getPollingInterval(), configuration.getPollingInterval());
        assertEquals(testConfiguration.getPollingTimeout(), configuration.getPollingTimeout());
    }

    @Test
    void validateConfigurationToDefaultTest() {
        TestConfiguration userConfig = new TestConfiguration();
        userConfig.setPollingInterval(1);
        userConfig.setPollingTimeout(2);
		final Pair<DistributionActionResultEnum, Configuration> distributionActionResultEnumConfigurationPair = client.validateAndInitConfiguration(
            new Wrapper<>(), userConfig);
        DistributionActionResultEnum validationResult = distributionActionResultEnumConfigurationPair.getFirst();
        Configuration configuration = distributionActionResultEnumConfigurationPair.getSecond();
        assertEquals(DistributionActionResultEnum.SUCCESS, validationResult);
        assertEquals(15, configuration.getPollingInterval());
        assertEquals(15, configuration.getPollingTimeout());
    }

    @Test
    void validateConfigurationPasswordTest() {
        Wrapper<IDistributionClientResult> errorWrapper = new Wrapper<>();
        TestConfiguration testPassword = new TestConfiguration();
        testPassword.setPassword(null);
        DistributionActionResultEnum validationResult = client.validateAndInitConfiguration(errorWrapper, testPassword).getFirst();
        assertEquals(DistributionActionResultEnum.CONF_MISSING_PASSWORD, validationResult);

        testPassword.setPassword("");
        validationResult = client.validateAndInitConfiguration(errorWrapper, testPassword).getFirst();
        assertEquals(DistributionActionResultEnum.CONF_MISSING_PASSWORD, validationResult);

    }

    @Test
    void validateConfigurationUserTest() {
        Wrapper<IDistributionClientResult> errorWrapper = new Wrapper<>();
        TestConfiguration testUser = new TestConfiguration();
        testUser.setUser(null);
        DistributionActionResultEnum validationResult = client.validateAndInitConfiguration(errorWrapper, testUser).getFirst();
        assertEquals(DistributionActionResultEnum.CONF_MISSING_USERNAME, validationResult);

        testUser.setUser("");
        validationResult = client.validateAndInitConfiguration(errorWrapper, testUser).getFirst();
        assertEquals(DistributionActionResultEnum.CONF_MISSING_USERNAME, validationResult);

    }

    @Test
	public void initWithMocksBadConfigurationTest() {
		reconfigureSdcConnector(connector, client);
        // no password
        TestConfiguration testPassword = new TestConfiguration();
        testPassword.setPassword(null);
        IDistributionClientResult validationResult = client.init(testPassword, new TestNotificationCallback());
        assertEquals(DistributionActionResultEnum.CONF_MISSING_PASSWORD, validationResult.getDistributionActionResult());

        testPassword.setPassword("");
        validationResult = client.init(testPassword, new TestNotificationCallback());
        assertEquals(DistributionActionResultEnum.CONF_MISSING_PASSWORD, validationResult.getDistributionActionResult());

        // no username
        TestConfiguration testUser = new TestConfiguration();
        testUser.setUser(null);
        validationResult = client.init(testUser, new TestNotificationCallback());
        assertEquals(DistributionActionResultEnum.CONF_MISSING_USERNAME, validationResult.getDistributionActionResult());

        testUser.setUser("");
        validationResult = client.init(testUser, new TestNotificationCallback());
        assertEquals(DistributionActionResultEnum.CONF_MISSING_USERNAME, validationResult.getDistributionActionResult());

		// no SDC server fqdn
        TestConfiguration testServerFqdn = new TestConfiguration();
		testServerFqdn.setSdcAddress(null);
        validationResult = client.init(testServerFqdn, new TestNotificationCallback());
		assertEquals(DistributionActionResultEnum.CONF_MISSING_SDC_FQDN, validationResult.getDistributionActionResult());

		testServerFqdn.setSdcAddress("");
        validationResult = client.init(testServerFqdn, new TestNotificationCallback());
		assertEquals(DistributionActionResultEnum.CONF_MISSING_SDC_FQDN, validationResult.getDistributionActionResult());

		testServerFqdn.setSdcAddress("this##is##bad##fqdn");
        validationResult = client.init(testServerFqdn, new TestNotificationCallback());
		assertEquals(DistributionActionResultEnum.CONF_INVALID_SDC_FQDN, validationResult.getDistributionActionResult());

        // no consumerId
        TestConfiguration testConsumerId = new TestConfiguration();
        testConsumerId.setComsumerID(null);
        validationResult = client.init(testConsumerId, new TestNotificationCallback());
        assertEquals(DistributionActionResultEnum.CONF_MISSING_CONSUMER_ID, validationResult.getDistributionActionResult());

        testConsumerId.setComsumerID("");
        validationResult = client.init(testConsumerId, new TestNotificationCallback());
        assertEquals(DistributionActionResultEnum.CONF_MISSING_CONSUMER_ID, validationResult.getDistributionActionResult());

        // no environmentName
        TestConfiguration testEnv = new TestConfiguration();
        testEnv.setEnvironmentName(null);
        validationResult = client.init(testEnv, new TestNotificationCallback());
        assertEquals(DistributionActionResultEnum.CONF_MISSING_ENVIRONMENT_NAME, validationResult.getDistributionActionResult());

        testEnv.setEnvironmentName("");
        validationResult = client.init(testEnv, new TestNotificationCallback());
        assertEquals(DistributionActionResultEnum.CONF_MISSING_ENVIRONMENT_NAME, validationResult.getDistributionActionResult());

    }

	private void reconfigureSdcConnector(SdcConnectorClient connector, DistributionClientImpl client) {
		doReturn(connector).when(client).createSdcConnector(any());
    }

    @Test
	public void initFailedConnectSdcTest() {

		TestConfiguration badSdcConfig = new TestConfiguration();
		if (badSdcConfig.isUseHttpsWithSDC() == null) {
            System.out.println("null for HTTPS then TRUE");
        } else {
			System.out.println("isUseHttpsWithSDC set to " + badSdcConfig.isUseHttpsWithSDC());
        }
		badSdcConfig.setSdcAddress("badhost:8080");

		IDistributionClientResult init = client.init(badSdcConfig, new TestNotificationCallback());
		assertEquals(DistributionActionResultEnum.SDC_CONNECTION_FAILED, init.getDistributionActionResult());

		badSdcConfig = new TestConfiguration();
		badSdcConfig.setSdcAddress("localhost:8181");

		init = client.init(badSdcConfig, new TestNotificationCallback());
		assertEquals(DistributionActionResultEnum.SDC_CONNECTION_FAILED, init.getDistributionActionResult());

    }

    @Test
	public void initFailedConnectSdcInHttpTest() {

		TestConfiguration badSdcConfig = new TestConfiguration();
		badSdcConfig.setSdcAddress("badhost:8080");
		badSdcConfig.setUseHttpsWithSDC(false);

		IDistributionClientResult init = client.init(badSdcConfig, new TestNotificationCallback());
		assertEquals(DistributionActionResultEnum.SDC_CONNECTION_FAILED, init.getDistributionActionResult());

		badSdcConfig = new TestConfiguration();
		badSdcConfig.setSdcAddress("localhost:8181");
		badSdcConfig.setUseHttpsWithSDC(false);

		init = client.init(badSdcConfig, new TestNotificationCallback());
		assertEquals(DistributionActionResultEnum.SDC_CONNECTION_FAILED, init.getDistributionActionResult());

    }

    @Test
	public void getConfigurationTest() {
        // connectorMock
        mockArtifactTypeList();
		mockKafkaData();
		reconfigureSdcConnector(connector, client);
		TestConfiguration badSdcConfig = new TestConfiguration();
		badSdcConfig.setPollingInterval(-5);

		IDistributionClientResult init = client.init(badSdcConfig, new TestNotificationCallback());
        assertEquals(DistributionActionResultEnum.SUCCESS, init.getDistributionActionResult());

        String confString = client.getConfiguration().toString();
        System.out.println(confString);

    }

    private IDistributionClientResult initSuccesResult() {
        return new IDistributionClientResult() {

            @Override
            public String getDistributionMessageResult() {
                return "success";
            }

            @Override
            public DistributionActionResultEnum getDistributionActionResult() {
                return DistributionActionResultEnum.SUCCESS;
            }
        };
    }

    @Test
	public void initWithMocksTest() {
        mockArtifactTypeList();
		mockKafkaData();
		reconfigureSdcConnector(connector, client);
        IDistributionClientResult initResponse = client.init(testConfiguration, new TestNotificationCallback());
        assertEquals(DistributionActionResultEnum.SUCCESS, initResponse.getDistributionActionResult());
        System.out.println(initResponse);
    }

    private void mockArtifactTypeList() {
        List<String> artifactTypes = new ArrayList<>();
        for (ArtifactTypeEnum artifactType : ArtifactTypeEnum.values()) {
            artifactTypes.add(artifactType.name());
        }

        final Either<List<String>, IDistributionClientResult> eitherArtifactTypes = Either.left(artifactTypes);
        Mockito.when(connector.getValidArtifactTypesList()).thenReturn(eitherArtifactTypes);
    }

    private void mockKafkaData() {
        KafkaDataResponse kafkaData = new KafkaDataResponse();
        kafkaData.setKafkaBootStrapServer("localhost:9092");
        kafkaData.setDistrNotificationTopicName("SDC-DISTR-NOTIF-TOPIC-AUTO");
        kafkaData.setDistrStatusTopicName("SDC-DISTR-STATUS-TOPIC-AUTO");
        final Either<KafkaDataResponse, IDistributionClientResult> eitherArtifactTypes = Either.left(kafkaData);
        Mockito.when(connector.getKafkaDistData()).thenReturn(eitherArtifactTypes);
    }

    @Test
    void testAlreadyInitTest() {
		initWithMocksTest();
        IDistributionClientResult initResponse = client.init(testConfiguration, new TestNotificationCallback());
		assertEquals(DistributionActionResultEnum.DISTRIBUTION_CLIENT_ALREADY_INITIALIZED, initResponse.getDistributionActionResult());
    }

    @Test
    void testStartWithoutInit() {
		IDistributionClientResult result = client.start();
        assertSame(DistributionActionResultEnum.DISTRIBUTION_CLIENT_NOT_INITIALIZED,
            result.getDistributionActionResult());
    }

    @Test
    void testDecodeVfModuleArtifact() {
        String vfModuleContent = getVFModuleExample();
        List<IVfModuleMetadata> decodeVfModuleArtifact = client.decodeVfModuleArtifact(vfModuleContent.getBytes());
        assertEquals(1, decodeVfModuleArtifact.size());
        IVfModuleMetadata iVfModuleMetadata = decodeVfModuleArtifact.get(0);
        assertEquals(11, iVfModuleMetadata.getArtifacts().size());
        assertEquals("Vccfdb..base_vDB_11032016..module-0", iVfModuleMetadata.getVfModuleModelName());
    }

    private String getVFModuleExample() {
        return "[\r\n" +
            "  {\r\n" +
            "    \"vfModuleModelName\": \"Vccfdb..base_vDB_11032016..module-0\",\r\n" +
            "    \"vfModuleModelInvariantUUID\": \"89bcc10e-84f9-475a-b7e3-bdac6cd2b31a\",\r\n" +
            "    \"vfModuleModelVersion\": \"1\",\r\n" +
            "    \"vfModuleModelUUID\": \"f7e1c7aa-cc7b-4dfc-b761-237e8063bd96\",\r\n" +
            "    \"GuguBubu\": true,\r\n" +
            "    \"isBase\": true,\r\n" +
            "    \"artifacts\": [\r\n" +
            "      \"68733000-7656-487c-aecb-040af96df5a5\",\r\n" +
            "      \"d3519bb4-be98-4c04-8815-4557379fdff3\",\r\n" +
            "      \"b445d84b-de23-4f0c-a0aa-8d794d85bebe\",\r\n" +
            "      \"52a6656a-63f4-4ae8-80f4-40febcaa15d6\",\r\n" +
            "      \"fdcf20b5-1bac-4da7-9e77-b0b565115027\",\r\n" +
            "      \"d3fcfd98-941c-4627-8b94-386dd3eab1ab\",\r\n" +
            "      \"bdd6c2b6-793b-49d7-8590-51e7d6998f69\",\r\n" +
            "      \"554a62b0-3a56-4c29-bc5e-23badf6da67f\",\r\n" +
            "      \"4b922d87-f2c9-44da-b933-57a91294fb42\",\r\n" +
            "      \"ad5cceda-0fa4-415e-b319-96f080e4b5c7\",\r\n" +
            "      \"8f4312f4-7be5-4d64-a3f5-564be7a0f01e\"\r\n" +
            "    ]\r\n" +
            "  }\r\n" +
            "]";
    }
}