/*
 * 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.alipay.sofa.smoke.tests.runtime.spring;

import com.alipay.sofa.runtime.api.annotation.SofaReference;
import com.alipay.sofa.runtime.api.annotation.SofaService;
import com.alipay.sofa.smoke.tests.runtime.RuntimeSofaBootApplication;
import com.alipay.sofa.smoke.tests.runtime.aop.StateAspect;
import com.alipay.sofa.smoke.tests.runtime.impl.SampleServiceImpl;
import com.alipay.sofa.smoke.tests.runtime.impl.ServiceWithoutInterface;
import com.alipay.sofa.smoke.tests.runtime.service.SampleService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;

import static org.assertj.core.api.Assertions.assertThat;

/**
 * Integration tests for sofa service publish.
 *
 * @author huzijie
 * @author qilong.zql
 * @since 3.2.0
 */
@SpringBootTest(classes = RuntimeSofaBootApplication.class)
@Import(SofaServicePublishTests.SofaServicePublishTestConfiguration.class)
public class SofaServicePublishTests {

    @SofaReference(uniqueId = "beanMethod")
    private SampleService           sampleService;

    @SofaReference(uniqueId = "clientFactory")
    private SampleService           otherService;

    @SofaReference(uniqueId = "serviceWithoutInterface")
    private ServiceWithoutInterface serviceWithoutInterface;

    @Test
    public void serviceAop() {
        assertThat(sampleService).isNotNull();
        assertThat(StateAspect.isAspectInvoked()).isFalse();
        sampleService.service();
        assertThat(StateAspect.isAspectInvoked()).isTrue();
    }

    @Test
    public void serviceWithInterface() {
        assertThat(serviceWithoutInterface).isNotNull();
        assertThat("ServiceWithoutInterface").isEqualTo(serviceWithoutInterface.service());
    }

    @TestConfiguration(proxyBeanMethods = false)
    @Aspect
    static class SofaServicePublishTestConfiguration {
        @Bean
        @SofaService(uniqueId = "beanMethod")
        public SampleService sampleService() {
            return new SampleServiceImpl();
        }

        @Bean
        public ServiceWithoutInterface serviceWithoutInterface() {
            return new ServiceWithoutInterface();
        }

        @Pointcut("execution(* com.alipay.sofa.smoke.tests.runtime.service.SampleService.*(..))")
        public void pointCut() {
        }

        @Before("pointCut()")
        public void doBefore(JoinPoint joinPoint) {
            StateAspect.setAspectInvoked();
        }
    }
}