/*
 * Copyright 2015 Sergej Meister
 *
 * 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.
 */

package com.intellijob.controllers;

import com.intellijob.BaseTester;
import com.intellijob.domain.Job;
import com.intellijob.domain.JobLink;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * Unit class to test <code>JobLinkController</code>
 */
public class JobControllerTest extends BaseTester {

    @Autowired
    private JobController jobController;

    @Test
    public void setsAllJobAsExtractedLive() {
        if (!RUNNING_LIVE) {
            Assert.assertTrue("Don't run this test.", Boolean.TRUE);
            return;
        }

        List<Job> allJobs = jobController.findAll();
        Assert.assertTrue("Should be not null or empty", allJobs.size() > 0);
        List<Job> updatedJobs = jobController.setExtractedFlag(allJobs, Boolean.TRUE);
        Assert.assertEquals(allJobs.size(), updatedJobs.size());
        Assert.assertTrue(updatedJobs.get(0).isExtracted());
    }

    @Test
    public void setsAllJobAsNotExtractedLive() {
        if (!RUNNING_LIVE) {
            Assert.assertTrue("Don't run this test.", Boolean.TRUE);
            return;
        }

        List<Job> allJobs = jobController.findAll();
        Assert.assertTrue("Should be not null or empty", allJobs.size() > 0);
        List<Job> updatedJobs = jobController.setExtractedFlag(allJobs, Boolean.FALSE);
        Assert.assertEquals(allJobs.size(), updatedJobs.size());
        Assert.assertFalse(updatedJobs.get(0).isExtracted());
    }

//    @Test
//    public void cleanUpJobs() {
//        if (!RUNNING_LIVE) {
//            Assert.assertTrue("Don't run this test.", Boolean.TRUE);
//            return;
//        }
//
//        List<Job> allBackupJobs = jobController.findAll();
//        List<JobLink> jobLinks = jobLinkController.findAll();
//        List<JobNew> newJobs = new ArrayList<>();
//        Set<String> hrefs = new HashSet<>();
//        for (Job jobBackup : allBackupJobs) {
//            if(jobBackup.getJobLink() != null){
//                String href = jobBackup.getJobLink().getHref();
//                if (!hrefs.contains(href)) {
//                    JobLink jobLink = getIfExist(href,jobLinks);
//                    if(jobLink != null){
//                        hrefs.add(href);
//                        JobNew jobNew = new JobNew();
//                        jobNew.setContent(jobBackup.getContent());
//                        jobNew.setExtracted(jobBackup.isExtracted());
//                        jobNew.setReceivedDate(jobBackup.getReceivedDate());
//                        jobNew.setJobLink(jobLink);
//                        newJobs.add(jobNew);
//                    }
//                }
//            }
//        }
//
//        jobRepositoryNew.save(newJobs);
//    }

    private JobLink getIfExist(String href, List<JobLink> jobLinks) {
        for (JobLink jobLink : jobLinks) {
            if (jobLink.getHref().equals(href)) {
                return jobLink;
            }
        }
        return null;
    }
}

@@ -702,46 +702,8 @@
 ob;%0A
-import com.intellijob.domain.JobLink;%0A
 impo
@@ -2084,1530 +2084,7 @@
    %7D
-%0A%0A//    @Test%0A//    public void cleanUpJobs() %7B%0A//        if (!RUNNING_LIVE) %7B%0A//            Assert.assertTrue(%22Don't run this test.%22, Boolean.TRUE);%0A//            return;%0A//        %7D%0A//%0A//        List%3CJob%3E allBackupJobs = jobController.findAll();%0A//        List%3CJobLink%3E jobLinks = jobLinkController.findAll();%0A//        List%3CJobNew%3E newJobs = new ArrayList%3C%3E();%0A//        Set%3CString%3E hrefs = new HashSet%3C%3E();%0A//        for (Job jobBackup : allBackupJobs) %7B%0A//            if(jobBackup.getJobLink() != null)%7B%0A//                String href = jobBackup.getJobLink().getHref();%0A//                if (!hrefs.contains(href)) %7B%0A//                    JobLink jobLink = getIfExist(href,jobLinks);%0A//                    if(jobLink != null)%7B%0A//                        hrefs.add(href);%0A//                        JobNew jobNew = new JobNew();%0A//                        jobNew.setContent(jobBackup.getContent());%0A//                        jobNew.setExtracted(jobBackup.isExtracted());%0A//                        jobNew.setReceivedDate(jobBackup.getReceivedDate());%0A//                        jobNew.setJobLink(jobLink);%0A//                        newJobs.add(jobNew);%0A//                    %7D%0A//                %7D%0A//            %7D%0A//        %7D%0A//%0A//        jobRepositoryNew.save(newJobs);%0A//    %7D%0A%0A    private JobLink getIfExist(String href, List%3CJobLink%3E jobLinks) %7B%0A        for (JobLink jobLink : jobLinks) %7B%0A            if (jobLink.getHref().equals(href)) %7B%0A                return jobLink;%0A            %7D%0A        %7D%0A        return null;%0A    %7D
 %0A%7D%0A

remove unused code