/*
 * Copyright (C) 2017 Black Duck Software Inc.
 * http://www.blackducksoftware.com/
 *
 * 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 com.blackducksoftware.integration.fortify.batch.util;

import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.blackducksoftware.integration.exception.IntegrationException;
import com.blackducksoftware.integration.fortify.batch.BatchSchedulerConfig;
import com.blackducksoftware.integration.fortify.batch.TestApplication;
import com.blackducksoftware.integration.fortify.batch.job.BlackDuckFortifyJobConfig;
import com.blackducksoftware.integration.fortify.batch.job.SpringConfiguration;
import com.blackducksoftware.integration.fortify.batch.model.BlackDuckFortifyMapperGroup;
import com.blackducksoftware.integration.fortify.batch.model.Vulnerability;
import com.blackducksoftware.integration.hub.model.view.ProjectVersionView;
import com.google.gson.JsonIOException;

import junit.framework.TestCase;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = TestApplication.class)
@ContextConfiguration(classes = { SpringConfiguration.class, BlackDuckFortifyJobConfig.class, BatchSchedulerConfig.class, PropertyConstants.class })
public class VulnerabilityUtilTest extends TestCase {
    private String HUB_PROJECT_NAME_1;

    private String HUB_PROJECT_VERSION_NAME_1;

    private String HUB_PROJECT_NAME_2;

    private String HUB_PROJECT_VERSION_NAME_2;

    @Autowired
    private HubServices hubServices;

    @Autowired
    private MappingParser mappingParser;

    @Autowired
    private PropertyConstants propertyConstants;

    @Override
    @Before
    public void setUp() throws JsonIOException, IOException, IntegrationException {
        final List<BlackDuckFortifyMapperGroup> blackDuckFortifyMappers = mappingParser
                .createMapping(propertyConstants.getMappingJsonPath());
        HUB_PROJECT_NAME_1 = blackDuckFortifyMappers.get(0).getHubProjectVersion().get(0).getHubProject();
        HUB_PROJECT_VERSION_NAME_1 = blackDuckFortifyMappers.get(0).getHubProjectVersion().get(0).getHubProjectVersion();
        HUB_PROJECT_NAME_2 = blackDuckFortifyMappers.get(1).getHubProjectVersion().get(0).getHubProject();
        HUB_PROJECT_VERSION_NAME_2 = blackDuckFortifyMappers.get(1).getHubProjectVersion().get(0).getHubProjectVersion();
    }

    @Test
    public void testRemoveDuplicates() {
        System.out.println("Executing testRemoveDuplicates");
        List<Vulnerability> vulnerabilities = new ArrayList<>();
        try {
            try (Writer writer = new BufferedWriter(
                    new OutputStreamWriter(new FileOutputStream(propertyConstants.getBatchJobStatusFilePath()), "utf-8"))) {
                writer.write("");
            } catch (UnsupportedEncodingException e) {
                // do nothing
            } catch (FileNotFoundException e) {
                // do nothing
            } catch (IOException e) {
                // do nothing
            }

            ProjectVersionView projectVersionItem1 = hubServices.getProjectVersion(HUB_PROJECT_NAME_1,
                    HUB_PROJECT_VERSION_NAME_1);
            ProjectVersionView projectVersionItem2 = hubServices.getProjectVersion(HUB_PROJECT_NAME_2,
                    HUB_PROJECT_VERSION_NAME_2);
            vulnerabilities
                    .addAll(VulnerabilityUtil.transformMapping(hubServices.getVulnerabilityComponentViews(projectVersionItem1),
                            HUB_PROJECT_NAME_1, HUB_PROJECT_VERSION_NAME_1, new Date(), propertyConstants));
            vulnerabilities
                    .addAll(VulnerabilityUtil.transformMapping(hubServices.getVulnerabilityComponentViews(projectVersionItem2),
                            HUB_PROJECT_NAME_2, HUB_PROJECT_VERSION_NAME_2, new Date(), propertyConstants));

            vulnerabilities = VulnerabilityUtil.removeDuplicates(vulnerabilities);

            assertNotNull(vulnerabilities);
            System.out.println("vulnerabilities count::" + vulnerabilities.size() + ", vulnerabilities::" + vulnerabilities);
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IntegrationException e) {
            e.printStackTrace();
        }
    }
}