|
16 | 16 | // under the License. |
17 | 17 | package org.apache.cloudstack.framework.jobs; |
18 | 18 |
|
19 | | -/* |
20 | | - * This integration test requires real DB setup, it is not meant to run at per-build |
21 | | - * basis, it can only be opened in developer's run |
22 | | - * |
23 | | - * |
| 19 | +import org.apache.cloudstack.framework.jobs.impl.AsyncJobManagerImpl; |
| 20 | +import org.junit.Assert; |
| 21 | +import org.junit.Test; |
| 22 | +import org.junit.runner.RunWith; |
| 23 | +import org.mockito.Spy; |
| 24 | +import org.mockito.junit.MockitoJUnitRunner; |
24 | 25 |
|
25 | | -@RunWith(SpringJUnit4ClassRunner.class) |
26 | | -@ContextConfiguration(locations = "classpath:/AsyncJobManagerTestContext.xml") |
27 | | -public class AsyncJobManagerTest extends TestCase { |
28 | | - private static final Logger s_logger = |
29 | | - Logger.getLogger(AsyncJobManagerTest.class); |
| 26 | +@RunWith (MockitoJUnitRunner.class) |
| 27 | +public class AsyncJobManagerTest { |
30 | 28 |
|
31 | | - @Inject |
32 | | - AsyncJobManager _jobMgr; |
| 29 | + @Spy |
| 30 | + AsyncJobManagerImpl asyncJobManager; |
33 | 31 |
|
34 | | - @Inject |
35 | | - AsyncJobTestDashboard _testDashboard; |
| 32 | + String input = "\"haprovider\":\"kvmhaprovider\"},\"outofbandmanagement\":{\"powerstate\":\"On\",\"enabled\":true,\"driver\":\"redfish\",\"address\":\"oob-address.com\",\"port\":\"80\",\"username\":\"root\",\"password\":\"password\"},\"resourcestate\":\"PrepareForMaintenance\",\"hahost\":false"; |
| 33 | + String expected = "\"haprovider\":\"kvmhaprovider\"},\"outofbandmanagement\":{\"powerstate\":\"On\",\"enabled\":true,\"driver\":\"redfish\",\"address\":\"oob-address.com\",\"port\":\"80\",\"username\":\"root\",\"password\":\"p*****\"},\"resourcestate\":\"PrepareForMaintenance\",\"hahost\":false"; |
| 34 | + String obfuscatedInput = "\"haprovider\":\"kvmhaprovider\"},\"outofbandmanagement\":{\"powerstate\":\"On\",\"enabled\":true,\"driver\":\"redfish\",\"address\":\"oob-address.com\",\"port\":\"80\",\"username\":\"root\",\"password\":\"p***\"},\"resourcestate\":\"PrepareForMaintenance\",\"hahost\":false"; |
| 35 | + String noPassword = "\"haprovider\":\"kvmhaprovider\"},\"outofbandmanagement\":{\"powerstate\":\"On\",\"enabled\":true,\"driver\":\"redfish\",\"address\":\"oob-address.com\",\"port\":\"80\",\"username\":\"root\"},\"resourcestate\":\"PrepareForMaintenance\",\"hahost\":false"; |
36 | 36 |
|
37 | | - @Override |
38 | | - @Before |
39 | | - public void setUp() throws Exception { |
40 | | - try { |
41 | | - ComponentContext.initComponentsLifeCycle(); |
42 | | - } catch (Exception ex) { |
43 | | - ex.printStackTrace(); |
44 | | - s_logger.error(ex.getMessage()); |
45 | | - } |
46 | | - } |
| 37 | + String inputNoBraces = "\"password\":\"password\"\",\"action\":\"OFF\""; |
| 38 | + String expectedNoBraces = "\"password\":\"p*****\",\"action\":\"OFF\""; |
47 | 39 |
|
48 | | - @Override |
49 | | - @After |
50 | | - public void tearDown() throws Exception { |
| 40 | + @Test |
| 41 | + public void obfuscatePasswordTest() { |
| 42 | + String result = asyncJobManager.obfuscatePassword(input, true); |
| 43 | + Assert.assertEquals(expected, result); |
51 | 44 | } |
52 | 45 |
|
53 | | - public void testWaitBehave() { |
54 | | -
|
55 | | - final Object me = this; |
56 | | - new Thread(new Runnable() { |
57 | | -
|
58 | | - @Override |
59 | | - public void run() { |
60 | | - s_logger.info("Sleeping..."); |
61 | | - try { |
62 | | - Thread.sleep(3000); |
63 | | - } catch (InterruptedException e) { |
64 | | - s_logger.debug("[ignored] ."); |
65 | | - } |
66 | | -
|
67 | | - s_logger.info("wakeup"); |
68 | | - synchronized (me) { |
69 | | - me.notifyAll(); |
70 | | - } |
71 | | - } |
72 | | -
|
73 | | - }).start(); |
74 | | -
|
75 | | - s_logger.info("First wait"); |
76 | | - synchronized (me) { |
77 | | - try { |
78 | | - wait(5000); |
79 | | - } catch (InterruptedException e) { |
80 | | - // TODO Auto-generated catch block |
81 | | - e.printStackTrace(); |
82 | | - } |
83 | | - } |
84 | | - s_logger.info("First wait done"); |
85 | | -
|
86 | | - s_logger.info("Second wait"); |
87 | | - synchronized (me) { |
88 | | - try { |
89 | | - wait(5000); |
90 | | - } catch (InterruptedException e) { |
91 | | - // TODO Auto-generated catch block |
92 | | - e.printStackTrace(); |
93 | | - } |
94 | | - } |
95 | | - s_logger.info("Second wait done"); |
| 46 | + @Test |
| 47 | + public void obfuscatePasswordTestNoBraces() { |
| 48 | + String result = asyncJobManager.obfuscatePassword(inputNoBraces, true); |
| 49 | + Assert.assertEquals(expectedNoBraces, result); |
96 | 50 | } |
97 | 51 |
|
98 | 52 | @Test |
99 | | - public void test() { |
100 | | - final int TOTAL_JOBS_PER_QUEUE = 5; |
101 | | - final int TOTAL_QUEUES = 100; |
102 | | -
|
103 | | - for (int i = 0; i < TOTAL_QUEUES; i++) { |
104 | | - for (int j = 0; j < TOTAL_JOBS_PER_QUEUE; j++) { |
105 | | - AsyncJobVO job = new AsyncJobVO(); |
106 | | - job.setCmd("TestCmd"); |
107 | | - job.setDispatcher("TestJobDispatcher"); |
108 | | - job.setCmdInfo("TestCmd info"); |
109 | | -
|
110 | | - _jobMgr.submitAsyncJob(job, "fakequeue", i); |
| 53 | + public void obfuscatePasswordTestHidePasswordFalse() { |
| 54 | + String result = asyncJobManager.obfuscatePassword(input, false); |
| 55 | + Assert.assertEquals(input, result); |
| 56 | + } |
111 | 57 |
|
112 | | - s_logger.info("Job submitted. job " + job.getId() + ", queue: " + i); |
113 | | - } |
114 | | - } |
| 58 | + @Test |
| 59 | + public void obfuscatePasswordTestObfuscatedInput() { |
| 60 | + String result = asyncJobManager.obfuscatePassword(obfuscatedInput, true); |
| 61 | + Assert.assertEquals(expected, result); |
| 62 | + } |
115 | 63 |
|
116 | | - while (true) { |
117 | | - if (_testDashboard.getCompletedJobCount() == TOTAL_JOBS_PER_QUEUE * TOTAL_QUEUES) |
118 | | - break; |
| 64 | + @Test |
| 65 | + public void obfuscatePasswordTestHidePasswordFalseObfuscatedInput() { |
| 66 | + String result = asyncJobManager.obfuscatePassword(obfuscatedInput, false); |
| 67 | + Assert.assertEquals(obfuscatedInput, result); |
| 68 | + } |
119 | 69 |
|
120 | | - try { |
121 | | - Thread.sleep(1000); |
122 | | - } catch (InterruptedException e) { |
123 | | - s_logger.debug("[ignored] ."); |
124 | | - } |
125 | | - } |
| 70 | + @Test |
| 71 | + public void obfuscatePasswordTestNoPassword() { |
| 72 | + String result = asyncJobManager.obfuscatePassword(noPassword, true); |
| 73 | + Assert.assertEquals(noPassword, result); |
| 74 | + } |
126 | 75 |
|
127 | | - s_logger.info("Test done with " + _testDashboard.getCompletedJobCount() + " job executed"); |
| 76 | + @Test |
| 77 | + public void obfuscatePasswordTestHidePasswordNoPassword() { |
| 78 | + String result = asyncJobManager.obfuscatePassword(noPassword, false); |
| 79 | + Assert.assertEquals(noPassword, result); |
128 | 80 | } |
129 | | -} |
130 | 81 |
|
131 | | -*/ |
| 82 | +} |
0 commit comments