Fix deserializing license response

Original commit: elastic/x-pack-elasticsearch@dae5e6f545
This commit is contained in:
Chris Earle 2016-04-04 18:45:15 -04:00
parent f13f454ec1
commit aa9f516655
2 changed files with 54 additions and 12 deletions

View File

@ -59,7 +59,7 @@ public class PutLicenseResponse extends AcknowledgedResponse implements ToXConte
int nMessages = in.readVInt();
String[] messages = new String[nMessages];
for (int j = 0; j < nMessages; j++) {
messages[i] = in.readString();
messages[j] = in.readString();
}
acknowledgeMessages.put(feature, messages);
}

View File

@ -5,6 +5,8 @@
*/
package org.elasticsearch.license.plugin;
import org.elasticsearch.common.io.stream.ByteBufferStreamInput;
import org.elasticsearch.common.io.stream.BytesStreamOutput;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
@ -13,30 +15,25 @@ import org.elasticsearch.license.plugin.action.put.PutLicenseResponse;
import org.elasticsearch.license.plugin.core.LicensesStatus;
import org.elasticsearch.test.ESTestCase;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.sameInstance;
public class PutLicenseResponseTests extends ESTestCase {
public void testSerialization() throws Exception {
boolean acknowledged = randomBoolean();
LicensesStatus status = randomFrom(LicensesStatus.VALID, LicensesStatus.INVALID, LicensesStatus.EXPIRED);
int nFeatures = randomIntBetween(1, 5);
Map<String, String[]> ackMessages = new HashMap<>();
for (int i = 0; i < nFeatures; i++) {
String feature = randomAsciiOfLengthBetween(9, 15);
int nMessages = randomIntBetween(1, 5);
String[] messages = new String[nMessages];
for (int j = 0; j < nMessages; j++) {
messages[j] = randomAsciiOfLengthBetween(10, 30);
}
ackMessages.put(feature, messages);
}
Map<String, String[]> ackMessages = randomAckMessages();
PutLicenseResponse response = new PutLicenseResponse(acknowledged, status, "", ackMessages);
XContentBuilder contentBuilder = XContentFactory.jsonBuilder();
contentBuilder.startObject();
response.toXContent(contentBuilder, ToXContent.EMPTY_PARAMS);
@ -60,4 +57,49 @@ public class PutLicenseResponseTests extends ESTestCase {
assertArrayEquals(entry.getValue().toArray(), ackMessages.get(entry.getKey()));
}
}
public void testStreamSerialization() throws IOException {
boolean acknowledged = randomBoolean();
LicensesStatus status = randomFrom(LicensesStatus.VALID, LicensesStatus.INVALID, LicensesStatus.EXPIRED);
Map<String, String[]> ackMessages = randomAckMessages();
// write the steam so that we can attempt to read it back
BytesStreamOutput output = new BytesStreamOutput();
PutLicenseResponse response = new PutLicenseResponse(acknowledged, status, "", ackMessages);
// write it out
response.writeTo(output);
ByteBufferStreamInput input = new ByteBufferStreamInput(ByteBuffer.wrap(output.bytes().toBytes()));
// read it back in
response.readFrom(input);
assertThat(response.isAcknowledged(), equalTo(acknowledged));
assertThat(response.status(), equalTo(status));
assertThat(response.acknowledgeMessages(), not(sameInstance(ackMessages)));
assertThat(response.acknowledgeMessages().size(), equalTo(ackMessages.size()));
for (String key : ackMessages.keySet()) {
assertArrayEquals(ackMessages.get(key), response.acknowledgeMessages().get(key));
}
}
private Map<String, String[]> randomAckMessages() {
int nFeatures = randomIntBetween(1, 5);
Map<String, String[]> ackMessages = new HashMap<>();
for (int i = 0; i < nFeatures; i++) {
String feature = randomAsciiOfLengthBetween(9, 15);
int nMessages = randomIntBetween(1, 5);
String[] messages = new String[nMessages];
for (int j = 0; j < nMessages; j++) {
messages[j] = randomAsciiOfLengthBetween(10, 30);
}
ackMessages.put(feature, messages);
}
return ackMessages;
}
}