/**
 * 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.am.management.handlers.management.api;

import io.gravitee.am.management.service.CertificatePluginService;
import io.gravitee.am.management.service.ExtensionGrantPluginService;
import io.gravitee.am.management.service.IdentityProviderPluginService;
import io.gravitee.am.service.*;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.JerseyTest;
import org.junit.After;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;

import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Application;

import static org.mockito.Mockito.mock;

/**
 * @author Titouan COMPIEGNE (titouan.compiegne at graviteesource.com)
 * @author GraviteeSource Team
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader = AnnotationConfigContextLoader.class)
public abstract class JerseySpringTest {

    @Autowired
    protected DomainService domainService;

    @Autowired
    protected UserService userService;

    @Autowired
    protected ScopeService scopeService;

    @Autowired
    protected RoleService roleService;

    @Autowired
    protected IdentityProviderService identityProviderService;

    @Autowired
    protected ExtensionGrantService extensionGrantService;

    @Autowired
    protected CertificateService certificateService;

    @Autowired
    protected ClientService clientService;

    @Autowired
    protected CertificatePluginService certificatePluginService;

    @Autowired
    protected TokenService tokenService;

    @Autowired
    protected ExtensionGrantPluginService extensionGrantPluginService;

    @Autowired
    protected IdentityProviderPluginService identityProviderPluginService;

    @Configuration
    @ComponentScan("io.gravitee.am.management.handlers.management.api.resources.enhancer")
    static class ContextConfiguration {
        @Bean
        public DomainService domainService() {
            return mock(DomainService.class);
        }

        @Bean
        public UserService userService() {
            return mock(UserService.class);
        }

        @Bean
        public ScopeService scopeService() {
            return mock(ScopeService.class);
        }

        @Bean
        public RoleService roleService() {
            return mock(RoleService.class);
        }

        @Bean
        public IdentityProviderService identityProviderService() {
            return mock(IdentityProviderService.class);
        }

        @Bean
        public ExtensionGrantService extensionGrantService() {
            return mock(ExtensionGrantService.class);
        }

        @Bean
        public CertificateService certificateService() {
            return mock(CertificateService.class);
        }

        @Bean
        public ClientService clientService() {
            return mock(ClientService.class);
        }

        @Bean
        public CertificatePluginService certificatePluginService() {
            return mock(CertificatePluginService.class);
        }

        @Bean
        public TokenService tokenService() {
            return mock(TokenService.class);
        }

        @Bean
        public ExtensionGrantPluginService extensionGrantPluginService() {
            return mock(ExtensionGrantPluginService.class);
        }

        @Bean
        public IdentityProviderPluginService identityProviderPluginService() {
            return mock(IdentityProviderPluginService.class);
        }
    }

    private JerseyTest _jerseyTest;

    public final WebTarget target(final String path) {
        return _jerseyTest.target(path);
    }

    @Before
    public void setup() throws Exception {
        _jerseyTest.setUp();
    }

    @After
    public void tearDown() throws Exception {
        _jerseyTest.tearDown();
    }

    @Autowired
    public void setApplicationContext(final ApplicationContext context) {
        _jerseyTest = new JerseyTest()
        {
            @Override
            protected Application configure()
            {
                ResourceConfig application = new ManagementApplication();
                application.property("contextConfig", context);

                return application;
            }
        };
    }
}

@@ -684,16 +684,105 @@
 t.api;%0A%0A
+import io.gravitee.am.management.handlers.management.api.certificate.CertificateManager;%0A
 import i
@@ -832,32 +832,32 @@
 ePluginService;%0A
-
 import io.gravit
@@ -974,32 +974,105 @@
 rPluginService;%0A
+import io.gravitee.am.plugins.certificate.core.CertificatePluginManager;%0A
 import io.gravit
@@ -2968,24 +2968,93 @@
 inService;%0A%0A
+    @Autowired%0A    protected CertificateManager certificateManager;%0A%0A
     @Configu
@@ -4700,32 +4700,32 @@
 uginService() %7B%0A
-
             retu
@@ -4775,24 +4775,308 @@
 );%0A        %7D
+%0A%0A        @Bean%0A        public CertificateManager certificateManager() %7B%0A            return mock(CertificateManager.class);%0A        %7D%0A%0A        @Bean%0A        public CertificatePluginManager certificatePluginManager() %7B%0A            return mock(CertificatePluginManager.class);%0A        %7D
 %0A    %7D%0A%0A    

fix certificate resource tests