Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
youji.zzl committed Sep 25, 2023
1 parent b858490 commit 90f07f1
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 1 deletion.
10 changes: 10 additions & 0 deletions sofa-serverless-runtime/sofa-serverless-base-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@
<groupId>com.alipay.sofa.serverless</groupId>
<artifactId>sofa-serverless-common</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
public class BaseRuntimeAutoConfiguration {

@Bean
@ConditionalOnNotMasterBiz
// @ConditionalOnNotMasterBiz
public ApplicationContextEventHandler applicationContextEventHandler() {
return new ApplicationContextEventHandler();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* 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.serverless.plugin.manager.handler;

import com.alipay.sofa.ark.spi.event.biz.BeforeBizStartupEvent;
import com.alipay.sofa.ark.spi.event.biz.BeforeBizStopEvent;
import com.alipay.sofa.ark.spi.model.Biz;
import com.alipay.sofa.serverless.common.BizRuntimeContext;
import com.alipay.sofa.serverless.common.BizRuntimeContextRegistry;
import com.alipay.sofa.serverless.plugin.BaseRuntimeAutoConfiguration;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.WebApplicationType;
import org.springframework.context.ConfigurableApplicationContext;

import java.util.Properties;

@RunWith(MockitoJUnitRunner.class)
public class BizUninstallEventHandlerTest {
private static final String bizName = "eventHandlerTest";
@Mock
private Biz biz;

private ConfigurableApplicationContext ctx;

private SpringApplication springApplication;

@Before
public void before() {
Properties properties = new Properties();
properties.setProperty("spring.application.name", bizName);

springApplication = new SpringApplication(BaseRuntimeAutoConfiguration.class);
springApplication.setDefaultProperties(properties);

springApplication.setWebApplicationType(WebApplicationType.NONE);
// ctx = springApplication.run();
}

@Test
public void handleEvent() {
Mockito.when(biz.getBizName()).thenReturn(bizName);
Mockito.when(biz.getBizClassLoader()).thenReturn(this.getClass().getClassLoader());

BeforeBizStartupEventHandler beforeBizStartupEventHandler = new BeforeBizStartupEventHandler();
beforeBizStartupEventHandler.handleEvent(new BeforeBizStartupEvent(biz));

ctx = springApplication.run();

BizRuntimeContext bizRuntimeContext = BizRuntimeContextRegistry.getBizRuntimeContext(biz);
Assert.assertEquals(bizRuntimeContext.getRootApplicationContext(), ctx);

BizUninstallEventHandler bizUninstallEventHandler = new BizUninstallEventHandler();
bizUninstallEventHandler.handleEvent(new BeforeBizStopEvent(biz));
Assert.assertFalse(ctx.isActive());
}
}
5 changes: 5 additions & 0 deletions sofa-serverless-runtime/sofa-serverless-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* 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.serverless.common;

import com.alipay.sofa.serverless.common.environment.ConditionalOnMasterBiz;
import com.alipay.sofa.serverless.common.environment.ConditionalOnNotMasterBiz;
import com.alipay.sofa.serverless.common.exception.BizRuntimeException;
import com.alipay.sofa.serverless.common.exception.ErrorCodes;
import org.junit.Test;
import org.springframework.boot.context.annotation.UserConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

public class ConditionalTest {
private final ApplicationContextRunner runner = new ApplicationContextRunner()
.withConfiguration(UserConfigurations
.of(ConditionalAutoConfiguration.class));

@Test
public void testConditional() {
runner.run(context -> {
context.assertThat().doesNotHaveBean("nonMasterBizRuntimeException");
context.assertThat().hasBean("masterBizRuntimeException");
});
}

/**
* @author mingmen
* @date 2023/6/14
*/
@Configuration
public static class ConditionalAutoConfiguration {

@Bean
@ConditionalOnNotMasterBiz
public BizRuntimeException nonMasterBizRuntimeException() {
return new BizRuntimeException(ErrorCodes.SpringContextManager.E100001, "error test.");
}

@Bean
@ConditionalOnMasterBiz
public BizRuntimeException masterBizRuntimeException() {
return new BizRuntimeException(ErrorCodes.SpringContextManager.E100002, "error test.");
}
}
}

0 comments on commit 90f07f1

Please sign in to comment.