2020
2121import org .apache .bigtop .manager .common .utils .Environments ;
2222import org .apache .bigtop .manager .dao .po .ClusterPO ;
23+ import org .apache .bigtop .manager .dao .po .ServicePO ;
24+ import org .apache .bigtop .manager .dao .repository .ClusterDao ;
25+ import org .apache .bigtop .manager .dao .repository .ServiceDao ;
2326import org .apache .bigtop .manager .server .command .task .TaskContext ;
27+ import org .apache .bigtop .manager .server .holder .SessionUserHolder ;
28+ import org .apache .bigtop .manager .server .holder .SpringContextHolder ;
2429import org .apache .bigtop .manager .server .utils .StackUtils ;
2530
2631import org .junit .jupiter .api .AfterAll ;
27- import org .junit .jupiter .api .BeforeAll ;
32+ import org .junit .jupiter .api .AfterEach ;
33+ import org .junit .jupiter .api .BeforeEach ;
2834import org .junit .jupiter .api .Test ;
2935import org .junit .jupiter .api .extension .ExtendWith ;
3036import org .mockito .Mock ;
@@ -44,16 +50,41 @@ public class AbstractComponentStageTest {
4450 @ Mock
4551 private AbstractComponentStage stage ;
4652
53+ @ Mock
54+ private ClusterDao clusterDao ;
55+
56+ @ Mock
57+ private ServiceDao serviceDao ;
58+
4759 private static MockedStatic <Environments > mocked ;
60+ private MockedStatic <SpringContextHolder > springContextHolderMocked ;
61+ private MockedStatic <SessionUserHolder > sessionUserHolderMocked ;
4862
49- @ BeforeAll
50- public static void setup () {
63+ @ BeforeEach
64+ public void setup () {
5165 mocked = mockStatic (Environments .class );
5266 when (Environments .isDevMode ()).thenReturn (true );
5367
68+ springContextHolderMocked = mockStatic (SpringContextHolder .class );
69+ when (SpringContextHolder .getBean (ClusterDao .class )).thenReturn (clusterDao );
70+ when (SpringContextHolder .getBean (ServiceDao .class )).thenReturn (serviceDao );
71+
72+ sessionUserHolderMocked = mockStatic (SessionUserHolder .class );
73+ when (SessionUserHolder .getUserId ()).thenReturn (1001L );
74+
5475 StackUtils .parseStack ();
5576 }
5677
78+ @ AfterEach
79+ public void tearDown () {
80+ if (springContextHolderMocked != null ) {
81+ springContextHolderMocked .close ();
82+ }
83+ if (sessionUserHolderMocked != null ) {
84+ sessionUserHolderMocked .close ();
85+ }
86+ }
87+
5788 @ AfterAll
5889 public static void teardown () {
5990 mocked .close ();
@@ -75,6 +106,11 @@ public void testCreateTaskContext() {
75106 ReflectionTestUtils .setField (stage , "stageContext" , stageContext );
76107 ReflectionTestUtils .setField (stage , "clusterPO" , clusterPO );
77108
109+ // Mock serviceDao.findByClusterIdAndName to avoid NPE in createTaskContext
110+ ServicePO servicePO = new ServicePO ();
111+ servicePO .setId (2L );
112+ when (serviceDao .findByClusterIdAndName (any (), any ())).thenReturn (servicePO );
113+
78114 doCallRealMethod ().when (stage ).createTaskContext (any ());
79115 TaskContext taskContext = stage .createTaskContext ("host1" );
80116
@@ -86,5 +122,7 @@ public void testCreateTaskContext() {
86122 assertEquals ("zookeeper" , taskContext .getServiceUser ());
87123 assertEquals ("test" , taskContext .getUserGroup ());
88124 assertEquals ("/opt" , taskContext .getRootDir ());
125+ assertEquals (1001L , taskContext .getOperatorId ());
126+ assertEquals (2L , taskContext .getServiceId ());
89127 }
90128}
0 commit comments