/**
 * Copyright (C) 2015 The Gravitee team (http://gravitee.io)
 *
 * 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.gravitee.rest.api.service;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;

import io.gravitee.repository.management.api.ApplicationRepository;
import io.gravitee.rest.api.model.application.ApplicationListItem;
import io.gravitee.rest.api.service.impl.ApplicationServiceImpl;
import java.util.Set;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

/**
 * @author Azize ELAMRANI (azize.elamrani at graviteesource.com)
 * @author Nicolas GERAUD (nicolas.geraud at graviteesource.com)
 * @author GraviteeSource Team
 */
@RunWith(MockitoJUnitRunner.class)
public class ApplicationService_FindByNameTest {

    @InjectMocks
    private ApplicationServiceImpl applicationService = new ApplicationServiceImpl();

    @Mock
    private ApplicationRepository applicationRepository;

    @Test
    public void shouldNotFindByNameWhenNull() throws Exception {
        Set<ApplicationListItem> set = applicationService.findByName(null, null);
        assertNotNull(set);
        assertEquals("result is empty", 0, set.size());
        verify(applicationRepository, never()).findByName(any());
    }

    @Test
    public void shouldNotFindByNameWhenEmpty() throws Exception {
        Set<ApplicationListItem> set = applicationService.findByName(null, " ");
        assertNotNull(set);
        assertEquals("result is empty", 0, set.size());
        verify(applicationRepository, never()).findByName(any());
    }

    @Test
    public void shouldNotFindByName() throws Exception {
        Set<ApplicationListItem> set = applicationService.findByName(null, "a");
        assertNotNull(set);
        assertEquals("result is empty", 0, set.size());
        verify(applicationRepository, times(1)).findByName("a");
    }
}

@@ -840,16 +840,60 @@
 ito.*;%0A%0A
+import io.gravitee.common.data.domain.Page;%0A
 import i
@@ -952,16 +952,137 @@
 sitory;%0A
+import io.gravitee.repository.management.api.search.ApplicationCriteria;%0Aimport io.gravitee.rest.api.model.api.ApiQuery;%0A
 import i
@@ -1205,16 +1205,46 @@
 ceImpl;%0A
+import java.util.Collections;%0A
 import j
@@ -1313,16 +1313,51 @@
 unWith;%0A
+import org.mockito.ArgumentCaptor;%0A
 import o
@@ -1405,16 +1405,44 @@
 o.Mock;%0A
+import org.mockito.Mockito;%0A
 import o
@@ -1905,16 +1905,76 @@
 itory;%0A%0A
+    @Mock%0A    private MembershipService membershipService;%0A%0A
     @Tes
@@ -2672,227 +2672,705 @@
     
-Set%3CApplicationListItem%3E set = applicationService.findByName(null, %22a%22);%0A        assertNotNull(set);%0A        assertEquals(%22result is empty%22, 0, set.size());%0A        verify(applicationRepository, times(1)).findByName(%22a%22
+when(membershipService.getMembershipsByMemberAndReference(any(), any(), any())).thenReturn(Collections.emptySet());%0A        when(applicationRepository.search(any(), any())).thenReturn(new Page%3C%3E(Collections.emptyList(), 0, 0, 0));%0A        Set%3CApplicationListItem%3E set = applicationService.findByName(null, %22a%22);%0A        assertNotNull(set);%0A        assertEquals(%22result is empty%22, 0, set.size());%0A        ArgumentCaptor%3CApplicationCriteria%3E queryCaptor = ArgumentCaptor.forClass(ApplicationCriteria.class);%0A        Mockito.verify(applicationRepository).search(queryCaptor.capture(), any());%0A        final ApplicationCriteria query = queryCaptor.getValue();%0A        assertEquals(%22a%22, query.getName()
 );%0A 

fix application searching test