View Javadoc
1   // Copyright (c) Microsoft Corporation. All rights reserved.
2   // Licensed under the MIT License.
3   package com.azure.core.test;
4   
5   import com.azure.core.util.configuration.ConfigurationManager;
6   import com.azure.core.test.utils.TestResourceNamer;
7   import org.junit.After;
8   import org.junit.Assert;
9   import org.junit.Before;
10  import org.junit.BeforeClass;
11  import org.junit.rules.TestName;
12  import org.slf4j.Logger;
13  import org.slf4j.LoggerFactory;
14  
15  import java.io.IOException;
16  import java.util.Locale;
17  
18  /**
19   * Base class for running live and playback tests using {@link InterceptorManager}.
20   */
21  public abstract class TestBase {
22      // Environment variable name used to determine the TestMode.
23      private static final String AZURE_TEST_MODE = "AZURE_TEST_MODE";
24      private static TestMode testMode;
25  
26      private final Logger logger = LoggerFactory.getLogger(TestBase.class);
27  
28      protected InterceptorManager interceptorManager;
29      protected TestResourceNamer testResourceNamer;
30  
31      /**
32       * Before tests are executed, determines the test mode by reading the {@link TestBase#AZURE_TEST_MODE} environment
33       * variable. If it is not set, {@link TestMode#PLAYBACK}
34       */
35      @BeforeClass
36      public static void setupClass() {
37          testMode = initializeTestMode();
38      }
39  
40      /**
41       * Sets-up the {@link TestBase#testResourceNamer} and {@link TestBase#interceptorManager} before each test case is run.
42       * Then calls its implementing class to perform any other set-up commands.
43       */
44      @Before
45      public void setupTest() {
46          final String testName = testName();
47          if (logger.isInfoEnabled()) {
48              logger.info("Test Mode: {}, Name: {}", testMode, testName);
49          }
50  
51          try {
52              interceptorManager = new InterceptorManager(testName, testMode);
53          } catch (IOException e) {
54              if (logger.isErrorEnabled()) {
55                  logger.error("Could not create interceptor for {}", testName, e);
56              }
57              Assert.fail();
58          }
59          testResourceNamer = new TestResourceNamer(testName, testMode, interceptorManager.getRecordedData());
60  
61          beforeTest();
62      }
63  
64      /**
65       * Disposes of {@link InterceptorManager} and its inheriting class' resources.
66       */
67      @After
68      public void teardownTest() {
69          afterTest();
70          interceptorManager.close();
71      }
72  
73      /**
74       * Gets the TestMode that has been initialized.
75       *
76       * @return The TestMode that has been initialized.
77       */
78      public TestMode getTestMode() {
79          return testMode;
80      }
81  
82      /**
83       * Gets the name of the current test being run.
84       * <p>
85       * NOTE: This could not be implemented in the base class using {@link TestName} because it always returns {@code
86       * null}. See https://stackoverflow.com/a/16113631/4220757.
87       *
88       * @return The name of the current test.
89       */
90      protected abstract String testName();
91  
92      /**
93       * Performs any set-up before each test case. Any initialization that occurs in TestBase occurs first before this.
94       * Can be overridden in an inheriting class to add additional functionality during test set-up.
95       */
96      protected void beforeTest() {
97      }
98  
99      /**
100      * Dispose of any resources and clean-up after a test case runs. Can be overridden in an inheriting class to add
101      * additional functionality during test teardown.
102      */
103     protected void afterTest() {
104     }
105 
106     private static TestMode initializeTestMode() {
107         final Logger logger = LoggerFactory.getLogger(TestBase.class);
108         final String azureTestMode = ConfigurationManager.getConfiguration().get(AZURE_TEST_MODE);
109 
110         if (azureTestMode != null) {
111             try {
112                 return TestMode.valueOf(azureTestMode.toUpperCase(Locale.US));
113             } catch (IllegalArgumentException e) {
114                 if (logger.isErrorEnabled()) {
115                     logger.error("Could not parse '{}' into TestEnum. Using 'Playback' mode.", azureTestMode);
116                 }
117 
118                 return TestMode.PLAYBACK;
119             }
120         }
121 
122         if (logger.isInfoEnabled()) {
123             logger.info("Environment variable '{}' has not been set yet. Using 'Playback' mode.", AZURE_TEST_MODE);
124         }
125         return TestMode.PLAYBACK;
126     }
127 }