/*
 * Copyright (C) 2016 Red Hat, Inc.
 *
 * 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 io.syndesis.rest.v1.handler;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.validation.Validator;

import io.syndesis.dao.manager.DataManager;
import io.syndesis.inspector.ClassInspector;
import io.syndesis.model.connection.Action;
import io.syndesis.model.connection.DataShape;
import io.syndesis.model.filter.FilterOptions;
import io.syndesis.model.integration.Integration;
import io.syndesis.model.integration.SimpleStep;
import io.syndesis.rest.v1.handler.integration.IntegrationHandler;
import org.junit.Before;
import org.junit.Test;

import static io.syndesis.model.connection.DataShapeKinds.JAVA;
import static io.syndesis.model.connection.DataShapeKinds.NONE;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/**
 * @author roland
 * @since 29.08.17
 */
public class IntegrationHandlerTest {


    private IntegrationHandler handler;
    private ClassInspector inspector;
    private Validator validator;
    private DataManager manager;

    @Before
    public void setup() {
        manager = mock(DataManager.class);
        validator = mock(Validator.class);
        inspector = mock(ClassInspector.class);
        handler = new IntegrationHandler(manager, validator, inspector);
    }

    @Test
    public void filterOptions_simple() {
        when(inspector.getPaths("twitter4j.Status")).thenReturn(Arrays.asList("paramA", "paramB"));
        Integration integration =
            createIntegrationFromDataShapes(dataShape(JAVA, "twitter4j.Status"),null,dataShape(NONE));

        FilterOptions options = handler.getFilterOptions(integration, -1);
        assertThat(options.getPaths()).hasSize(2).contains("paramA","paramB");
    }

    @Test
    public void filterOptions_multipleOutputShapes() {
        when(inspector.getPaths("twitter4j.Status")).thenReturn(Arrays.asList("paramA", "paramB"));
        when(inspector.getPaths("blubber.bla")).thenReturn(Arrays.asList("paramY", "paramZ"));
        Integration integration =
            createIntegrationFromDataShapes(dataShape(JAVA, "blubber.bla"),null, dataShape(JAVA, "twitter4j.Status"));

        assertThat(handler.getFilterOptions(integration, -1).getPaths())
            .hasSize(2).contains("paramA", "paramB");

        assertThat(handler.getFilterOptions(integration, 1).getPaths())
            .hasSize(2).contains("paramY","paramZ");
    }

    @Test
    public void filterOptions_noOutputShape() {
        Integration integration =
            createIntegrationFromDataShapes(dataShape(NONE), null);

        FilterOptions options = handler.getFilterOptions(integration, -1);
        assertThat(options.getPaths()).isEmpty();
    }

    private Integration createIntegrationFromDataShapes(DataShape... dataShapes) {
        return new Integration.Builder().steps(steps(dataShapes)).build();
    }

    private List<SimpleStep> steps(DataShape ... dataShapes) {
        List<SimpleStep> ret = new ArrayList<>();
        for (DataShape shape : dataShapes) {
            Action action = new Action.Builder().outputDataShape(shape).build();
            ret.add(new SimpleStep.Builder().action(action).build());
        }
        return ret;
    }

    private DataShape dataShape(String kind) {
        return dataShape(kind, null);
    }

    private DataShape dataShape(String kind, String type) {
        return new DataShape.Builder().kind(kind).type(type).build();
    }
}

@@ -929,16 +929,68 @@
 aShape;%0A
+import io.syndesis.model.connection.DataShapeKinds;%0A
 import i
@@ -1261,169 +1261,50 @@
 tic 
-io.syndesis.model.connection.DataShapeKinds.JAVA;%0Aimport static io.syndesis.model.connection.DataShapeKinds.NONE;%0Aimport static org.assertj.core.api.Assertions.*
+org.assertj.core.api.Assertions.assertThat
 ;%0Aim
@@ -1548,74 +1548,8 @@
 tor;
-%0A    private Validator validator;%0A    private DataManager manager;
 %0A%0A  
@@ -1577,17 +1577,17 @@
 void set
-u
+U
 p() %7B%0A  
@@ -1592,16 +1592,28 @@
         
+DataManager 
 manager 
@@ -1647,16 +1647,26 @@
         
+Validator 
 validato
@@ -1863,10 +1863,9 @@
 ions
-_s
+S
 impl
@@ -2050,32 +2050,47 @@
 hapes(dataShape(
+DataShapeKinds.
 JAVA, %22twitter4j
@@ -2099,21 +2099,23 @@
 tatus%22),
+ 
 null,
+ 
 dataShap
@@ -2112,24 +2112,39 @@
 , dataShape(
+DataShapeKinds.
 NONE));%0A%0A   
@@ -2344,10 +2344,9 @@
 ions
-_m
+M
 ulti
@@ -2644,24 +2644,39 @@
 s(dataShape(
+DataShapeKinds.
 JAVA, %22blubb
@@ -2700,16 +2700,31 @@
 taShape(
+DataShapeKinds.
 JAVA, %22t
@@ -3047,10 +3047,9 @@
 ions
-_n
+N
 oOut
@@ -3149,16 +3149,31 @@
 taShape(
+DataShapeKinds.
 NONE), n

Make Codacy happy