risdenk commented on a change in pull request #239: KNOX-2153 - CM discovery - Monitor Cloudera Manager URL: https://github.com/apache/knox/pull/239#discussion_r369832496
########## File path: gateway-discovery-cm/src/test/java/org/apache/knox/gateway/topology/discovery/cm/monitor/ServiceConfigurationRecordObjectMapperTest.java ########## @@ -0,0 +1,240 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package org.apache.knox.gateway.topology.discovery.cm.monitor; + +import org.junit.Test; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.nio.charset.StandardCharsets; +import java.util.Collections; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +public class ServiceConfigurationRecordObjectMapperTest { + + @Test + public void testSerializationOfEmptyModel() { + ServiceConfigurationRecord record = new ServiceConfigurationRecord(); + + String serialized = doTestSerialization(record); + assertNotNull(serialized); + assertEquals("{\"clusterName\":null,\"discoveryAddress\":null,\"configs\":null}", serialized); + } + + @Test + public void testDeserializationOfEmptyModel() { + final String serializedEmptyModel = "{\"clusterName\":null,\"discoveryAddress\":null,\"configs\":null}"; + + ServiceConfigurationRecord record = doTestDeserialization(serializedEmptyModel); + assertNull(record.getClusterName()); + assertNull(record.getDiscoveryAddress()); + assertNull(record.getConfigs()); + } + + @Test + public void testSerializationNoConfigs() { + ServiceConfigurationRecord record = new ServiceConfigurationRecord(); + record.setClusterName("myCluster"); + record.setDiscoveryAddress("http://localhost:7180/"); + record.setConfigs(Collections.emptyMap()); + + String serialized = doTestSerialization(record); + assertNotNull(serialized); + final String expectedSerialization = + createTestJSON(record.getClusterName(), record.getDiscoveryAddress(), record.getConfigs()); + assertEquals(expectedSerialization, serialized); + } + + @Test + public void testDeserializationNoConfigs() { + final String clusterName = "testCluster"; + final String discoveryAddress = "http://myhost:1234"; + final String serialized = createTestJSON(clusterName, discoveryAddress, null); + + ServiceConfigurationRecord record = doTestDeserialization(serialized); + assertNotNull(record); + assertEquals(clusterName, record.getClusterName()); + assertEquals(discoveryAddress, record.getDiscoveryAddress()); + Map<String, ServiceConfigurationModel> configs = record.getConfigs(); + assertNull("Expect no configs entry", configs); + } + + @Test + public void testRoundTrip() { + final String clusterName = "Cluster 1"; + final String address = "http://cmhost:7180/"; + final Map<String, ServiceConfigurationModel> configs = new HashMap<>(); + ServiceConfigurationModel model1 = new ServiceConfigurationModel(); + model1.addServiceProperty("svc1.prop1", "svc1.prop1.value"); + model1.addServiceProperty("svc1.prop2", "svc1.prop2.value"); + model1.addRoleProperty("Role1", "r1.prop1", "r1.prop1.value"); + model1.addRoleProperty("Role1", "r1.prop2", "r1.prop2.value"); + model1.addRoleProperty("Role2", "r2.prop1", "r2.prop1.value"); + configs.put("Service1", model1); + + ServiceConfigurationModel model2 = new ServiceConfigurationModel(); + model2.addServiceProperty("svc1.prop1", "svc1.prop1.value"); + model2.addRoleProperty("Role1", "r1.prop1", "r1.prop1.value"); + model2.addRoleProperty("Role2", "r2.prop1", "r2.prop1.value"); + model2.addRoleProperty("Role2", "r2.prop2", "r2.prop2.value"); + configs.put("Service2", model2); + + ServiceConfigurationRecord record = new ServiceConfigurationRecord(); + record.setClusterName(clusterName); + record.setDiscoveryAddress(address); + record.setConfigs(configs); + + String serialized = doTestSerialization(record); + ServiceConfigurationRecord deserialized = doTestDeserialization(serialized); + + assertEquals(clusterName, deserialized.getClusterName()); + assertEquals(address, deserialized.getDiscoveryAddress()); + Map<String, ServiceConfigurationModel> deserializedConfigs = deserialized.getConfigs(); + assertNotNull(deserializedConfigs); + assertEquals(2, deserializedConfigs.size()); + assertTrue(deserializedConfigs.containsKey("Service1")); + assertTrue(deserializedConfigs.containsKey("Service2")); + + ServiceConfigurationModel service1 = deserializedConfigs.get("Service1"); + Map<String, String> svc1Props = service1.getServiceProps(); + assertEquals(2, svc1Props.size()); + assertEquals(model1.getServiceProps().get("svc1.prop1"), svc1Props.get("svc1.prop1")); + assertEquals(model1.getServiceProps().get("svc1.prop2"), svc1Props.get("svc1.prop2")); + assertEquals(2, service1.getRoleTypes().size()); + assertEquals(model1.getRoleProps("Role1").get("r1.prop1"), service1.getRoleProps("Role1").get("r1.prop1")); + assertEquals(model1.getRoleProps("Role1").get("r1.prop2"), service1.getRoleProps("Role1").get("r1.prop2")); + assertEquals(model1.getRoleProps("Role2").get("r2.prop1"), service1.getRoleProps("Role2").get("r2.prop1")); + assertEquals(model1.getRoleProps("Role2").get("r2.prop1"), service1.getRoleProps("Role2").get("r2.prop1")); + + + ServiceConfigurationModel service2 = deserializedConfigs.get("Service2"); + Map<String, String> svc2Props = service2.getServiceProps(); + assertEquals(1, svc2Props.size()); + assertEquals(model2.getServiceProps().get("svc2.prop1"), svc2Props.get("svc2.prop1")); + assertEquals(2, service2.getRoleTypes().size()); + assertEquals(model2.getRoleProps("Role1").get("r1.prop1"), service2.getRoleProps("Role1").get("r1.prop1")); + assertEquals(model2.getRoleProps("Role2").get("r2.prop1"), service2.getRoleProps("Role2").get("r2.prop1")); + assertEquals(model2.getRoleProps("Role2").get("r2.prop2"), service2.getRoleProps("Role2").get("r2.prop2")); + } + + private String doTestSerialization(final ServiceConfigurationRecord record) { + String serialized = null; + try (ByteArrayOutputStream out = new ByteArrayOutputStream()) { + (new ServiceConfigurationRecordObjectMapper()).writeValue(out, record); + serialized = out.toString(StandardCharsets.UTF_8.name()); + } catch (Exception e) { + fail(e.getMessage()); + } + return serialized; + } + + private ServiceConfigurationRecord doTestDeserialization(final String serialized) { + ServiceConfigurationRecord deserialized = null; + try (ByteArrayInputStream in = new ByteArrayInputStream(serialized.getBytes(StandardCharsets.UTF_8))) { + deserialized = + (new ServiceConfigurationRecordObjectMapper()).readValue(in, ServiceConfigurationRecord.class); + } catch (Exception e) { + fail(e.getMessage()); + } + return deserialized; + } + + @SuppressWarnings({"PMD.AppendCharacterWithChar", "PMD.ConsecutiveLiteralAppends"}) Review comment: Not sure why these are suppressed? It would be better to actually do what it says. One of the simple ones is use `'` around literals. ie: `'{'` or `'"'` ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
