29
29
import java .util .concurrent .CountDownLatch ;
30
30
import java .util .concurrent .TimeUnit ;
31
31
import java .util .concurrent .atomic .AtomicReference ;
32
+ import java .util .function .Consumer ;
32
33
33
34
import static org .hamcrest .Matchers .equalTo ;
34
35
import static org .hamcrest .Matchers .hasKey ;
41
42
public class TransportXPackInfoActionTests extends ESTestCase {
42
43
43
44
public void testDoExecute () throws Exception {
45
+ EnumSet <XPackInfoRequest .Category > categories = EnumSet .noneOf (XPackInfoRequest .Category .class );
46
+ int maxCategoryCount = randomIntBetween (0 , XPackInfoRequest .Category .values ().length );
47
+ for (int i = 0 ; i < maxCategoryCount ; i ++) {
48
+ categories .add (randomFrom (XPackInfoRequest .Category .values ()));
49
+ }
50
+
51
+ License license = mock (License .class );
52
+ long expiryDate = randomLong ();
53
+ when (license .expiryDate ()).thenReturn (expiryDate );
54
+ LicenseStatus status = randomFrom (LicenseStatus .values ());
55
+ when (license .status ()).thenReturn (status );
56
+ String licenseType = randomAlphaOfLength (10 );
57
+ when (license .type ()).thenReturn (licenseType );
58
+ License .OperationMode licenseMode = randomFrom (License .OperationMode .values ());
59
+ when (license .operationMode ()).thenReturn (licenseMode );
60
+ String uid = randomAlphaOfLength (30 );
61
+ when (license .uid ()).thenReturn (uid );
62
+
63
+ checkAction (categories , -1 , license , (XPackInfoResponse .LicenseInfo licenseInfo ) -> {
64
+ assertThat (licenseInfo .getExpiryDate (), is (expiryDate ));
65
+ assertThat (licenseInfo .getStatus (), is (status ));
66
+ assertThat (licenseInfo .getType (), is (licenseType ));
67
+ assertThat (licenseInfo .getMode (), is (licenseMode .name ().toLowerCase (Locale .ROOT )));
68
+ assertThat (licenseInfo .getUid (), is (uid ));
69
+ });
70
+ }
71
+
72
+ public void testDoExecuteWithEnterpriseLicenseWithoutBackwardsCompat () throws Exception {
73
+ EnumSet <XPackInfoRequest .Category > categories = EnumSet .allOf (XPackInfoRequest .Category .class );
74
+
75
+ License license = mock (License .class );
76
+ when (license .expiryDate ()).thenReturn (randomLong ());
77
+ when (license .status ()).thenReturn (LicenseStatus .ACTIVE );
78
+ when (license .type ()).thenReturn ("enterprise" );
79
+ when (license .operationMode ()).thenReturn (License .OperationMode .ENTERPRISE );
80
+ when (license .uid ()).thenReturn (randomAlphaOfLength (30 ));
81
+
82
+ checkAction (categories , randomFrom (License .VERSION_ENTERPRISE , License .VERSION_CURRENT , -1 ), license ,
83
+ licenseInfo -> {
84
+ assertThat (licenseInfo .getType (), is ("enterprise" ));
85
+ assertThat (licenseInfo .getMode (), is ("enterprise" ));
86
+ });
87
+ }
44
88
89
+ public void testDoExecuteWithEnterpriseLicenseWithBackwardsCompat () throws Exception {
90
+ EnumSet <XPackInfoRequest .Category > categories = EnumSet .allOf (XPackInfoRequest .Category .class );
91
+
92
+ License license = mock (License .class );
93
+ when (license .expiryDate ()).thenReturn (randomLong ());
94
+ when (license .status ()).thenReturn (LicenseStatus .ACTIVE );
95
+ when (license .type ()).thenReturn ("enterprise" );
96
+ when (license .operationMode ()).thenReturn (License .OperationMode .ENTERPRISE );
97
+ when (license .uid ()).thenReturn (randomAlphaOfLength (30 ));
98
+
99
+ checkAction (categories , randomFrom (License .VERSION_START_DATE , License .VERSION_CRYPTO_ALGORITHMS ), license ,
100
+ licenseInfo -> {
101
+ assertThat (licenseInfo .getType (), is ("platinum" ));
102
+ assertThat (licenseInfo .getMode (), is ("platinum" ));
103
+ });
104
+ }
105
+
106
+ private void checkAction (EnumSet <XPackInfoRequest .Category > categories , int licenseVersion , License license ,
107
+ Consumer <XPackInfoResponse .LicenseInfo > licenseVerifier ) throws InterruptedException {
45
108
LicenseService licenseService = mock (LicenseService .class );
46
109
47
110
final Set <XPackFeatureSet > featureSets = new HashSet <>();
@@ -59,28 +122,14 @@ public void testDoExecute() throws Exception {
59
122
TransportXPackInfoAction action = new TransportXPackInfoAction (transportService , mock (ActionFilters .class ),
60
123
licenseService , featureSets );
61
124
62
- License license = mock (License .class );
63
- long expiryDate = randomLong ();
64
- when (license .expiryDate ()).thenReturn (expiryDate );
65
- LicenseStatus status = randomFrom (LicenseStatus .values ());
66
- when (license .status ()).thenReturn (status );
67
- String type = randomAlphaOfLength (10 );
68
- when (license .type ()).thenReturn (type );
69
- License .OperationMode mode = randomFrom (License .OperationMode .values ());
70
- when (license .operationMode ()).thenReturn (mode );
71
- String uid = randomAlphaOfLength (30 );
72
- when (license .uid ()).thenReturn (uid );
73
125
when (licenseService .getLicense ()).thenReturn (license );
74
126
75
127
XPackInfoRequest request = new XPackInfoRequest ();
76
128
request .setVerbose (randomBoolean ());
77
-
78
- EnumSet <XPackInfoRequest .Category > categories = EnumSet .noneOf (XPackInfoRequest .Category .class );
79
- int maxCategoryCount = randomIntBetween (0 , XPackInfoRequest .Category .values ().length );
80
- for (int i = 0 ; i < maxCategoryCount ; i ++) {
81
- categories .add (randomFrom (XPackInfoRequest .Category .values ()));
82
- }
83
129
request .setCategories (categories );
130
+ if (licenseVersion != -1 ) {
131
+ request .setLicenseVersion (licenseVersion );
132
+ }
84
133
85
134
final CountDownLatch latch = new CountDownLatch (1 );
86
135
final AtomicReference <XPackInfoResponse > response = new AtomicReference <>();
@@ -114,11 +163,7 @@ public void onFailure(Exception e) {
114
163
115
164
if (request .getCategories ().contains (XPackInfoRequest .Category .LICENSE )) {
116
165
assertThat (response .get ().getLicenseInfo (), notNullValue ());
117
- assertThat (response .get ().getLicenseInfo ().getExpiryDate (), is (expiryDate ));
118
- assertThat (response .get ().getLicenseInfo ().getStatus (), is (status ));
119
- assertThat (response .get ().getLicenseInfo ().getType (), is (type ));
120
- assertThat (response .get ().getLicenseInfo ().getMode (), is (mode .name ().toLowerCase (Locale .ROOT )));
121
- assertThat (response .get ().getLicenseInfo ().getUid (), is (uid ));
166
+ licenseVerifier .accept (response .get ().getLicenseInfo ());
122
167
} else {
123
168
assertThat (response .get ().getLicenseInfo (), nullValue ());
124
169
}
@@ -136,7 +181,6 @@ public void onFailure(Exception e) {
136
181
} else {
137
182
assertThat (response .get ().getFeatureSetsInfo (), nullValue ());
138
183
}
139
-
140
184
}
141
185
142
186
}
0 commit comments