Search in sources :

Example 36 with JsonValue

use of com.hazelcast.internal.json.JsonValue in project hazelcast by hazelcast.

the class JsonParser_Test method parse_failsOnTooDeeplyNestedMixedObject.

@Test
public void parse_failsOnTooDeeplyNestedMixedObject() {
    JsonValue value = new JsonObject();
    for (int i = 0; i < 1001; i++) {
        value = i % 2 == 0 ? new JsonArray().add(value) : new JsonObject().add("foo", value);
    }
    final String input = value.toString();
    ParseException exception = assertException(ParseException.class, new RunnableEx() {

        public void run() throws IOException {
            parser.parse(input);
        }
    });
    assertEquals("Nesting too deep at 1:4002", exception.getMessage());
}
Also used : JsonArray(com.hazelcast.internal.json.JsonArray) JsonValue(com.hazelcast.internal.json.JsonValue) JsonObject(com.hazelcast.internal.json.JsonObject) RunnableEx(com.hazelcast.internal.json.TestUtil.RunnableEx) ParseException(com.hazelcast.internal.json.ParseException) IOException(java.io.IOException) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 37 with JsonValue

use of com.hazelcast.internal.json.JsonValue in project hazelcast by hazelcast.

the class AzureComputeApi method parsePublicIpResponse.

private Map<String, String> parsePublicIpResponse(String response) {
    Map<String, String> publicIps = new HashMap<>();
    for (JsonValue item : toJsonArray(Json.parse(response).asObject().get("value"))) {
        String id = item.asObject().getString("id", null);
        String ip = toJsonObject(item.asObject().get("properties")).getString("ipAddress", null);
        if (!isNullOrEmptyAfterTrim(ip)) {
            publicIps.put(id, ip);
        }
    }
    return publicIps;
}
Also used : HashMap(java.util.HashMap) JsonValue(com.hazelcast.internal.json.JsonValue)

Example 38 with JsonValue

use of com.hazelcast.internal.json.JsonValue in project hazelcast by hazelcast.

the class KubernetesApiEndpointProvider method extractNodes.

public Map<EndpointAddress, String> extractNodes(JsonObject endpointsListJson, List<EndpointAddress> privateAddresses) {
    Map<EndpointAddress, String> result = new HashMap<>();
    Set<EndpointAddress> left = new HashSet<>(privateAddresses);
    for (JsonValue item : toJsonArray(endpointsListJson.get("items"))) {
        for (JsonValue subset : toJsonArray(item.asObject().get("subsets"))) {
            JsonObject subsetObject = subset.asObject();
            List<Integer> ports = new ArrayList<>();
            for (JsonValue port : toJsonArray(subsetObject.get("ports"))) {
                ports.add(port.asObject().get("port").asInt());
            }
            Map<EndpointAddress, String> nodes = new HashMap<>();
            nodes.putAll(extractNodes(subsetObject.get("addresses"), ports));
            nodes.putAll(extractNodes(subsetObject.get("notReadyAddresses"), ports));
            for (Map.Entry<EndpointAddress, String> nodeEntry : nodes.entrySet()) {
                EndpointAddress address = nodeEntry.getKey();
                if (privateAddresses.contains(address)) {
                    result.put(address, nodes.get(address));
                    left.remove(address);
                }
            }
        }
    }
    if (!left.isEmpty()) {
        // At least one Hazelcast Member POD does not have 'nodeName' assigned.
        throw noNodeNameAssignedException(left);
    }
    return result;
}
Also used : HashMap(java.util.HashMap) JsonValue(com.hazelcast.internal.json.JsonValue) ArrayList(java.util.ArrayList) JsonObject(com.hazelcast.internal.json.JsonObject) KubernetesApiProvider.convertToString(com.hazelcast.kubernetes.KubernetesApiProvider.convertToString) HashMap(java.util.HashMap) Map(java.util.Map) EndpointAddress(com.hazelcast.kubernetes.KubernetesClient.EndpointAddress) HashSet(java.util.HashSet)

Example 39 with JsonValue

use of com.hazelcast.internal.json.JsonValue in project hazelcast by hazelcast.

the class KubernetesApiEndpointSlicesProvider method extractServices.

public Map<EndpointAddress, String> extractServices(JsonObject endpointsListJson, List<EndpointAddress> privateAddresses) {
    Map<EndpointAddress, String> result = new HashMap<>();
    Set<EndpointAddress> left = new HashSet<>(privateAddresses);
    for (JsonValue item : toJsonArray(endpointsListJson.get("items"))) {
        JsonValue ownerRefsValue = item.asObject().get("metadata").asObject().get("ownerReferences");
        if (ownerRefsValue == null || ownerRefsValue.asArray().size() > 1 || !ownerRefsValue.asArray().get(0).asObject().get("kind").asString().equals("Service")) {
            continue;
        }
        String service = ownerRefsValue.asArray().get(0).asObject().get("name").asString();
        List<Endpoint> endpoints = parseEndpointSlices(item);
        // Service must point to exactly one endpoint address, otherwise the public IP would be ambiguous.
        if (endpoints.size() == 1) {
            EndpointAddress address = endpoints.get(0).getPrivateAddress();
            if (privateAddresses.contains(address)) {
                // If multiple services match the pod, then match service and pod names
                if (!result.containsKey(address) || service.equals(extractTargetRefName(item))) {
                    result.put(address, service);
                }
                left.remove(address);
            }
        }
    }
    if (!left.isEmpty()) {
        // At least one Hazelcast Member POD does not have a corresponding service.
        throw noCorrespondingServicesException(left);
    }
    return result;
}
Also used : Endpoint(com.hazelcast.kubernetes.KubernetesClient.Endpoint) HashMap(java.util.HashMap) JsonValue(com.hazelcast.internal.json.JsonValue) KubernetesApiProvider.convertToString(com.hazelcast.kubernetes.KubernetesApiProvider.convertToString) EndpointAddress(com.hazelcast.kubernetes.KubernetesClient.EndpointAddress) HashSet(java.util.HashSet)

Example 40 with JsonValue

use of com.hazelcast.internal.json.JsonValue in project hazelcast by hazelcast.

the class HazelcastCloudDiscoveryTest method testJsonResponseParse.

@Test
public void testJsonResponseParse() throws IOException {
    JsonValue jsonResponse = Json.parse("[{\"private-address\":\"100.96.5.1\",\"public-address\":\"10.113.44.139:31115\"}," + "{\"private-address\":\"100.96.4.2\",\"public-address\":\"10.113.44.130:31115\"} ]");
    Map<Address, Address> privatePublicMap = HazelcastCloudDiscovery.parseJsonResponse(jsonResponse);
    assertEquals(2, privatePublicMap.size());
    assertEquals(new Address("10.113.44.139", 31115), privatePublicMap.get(new Address("100.96.5.1", 31115)));
    assertEquals(new Address("10.113.44.130", 31115), privatePublicMap.get(new Address("100.96.4.2", 31115)));
}
Also used : Address(com.hazelcast.cluster.Address) InetSocketAddress(java.net.InetSocketAddress) JsonValue(com.hazelcast.internal.json.JsonValue) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

JsonValue (com.hazelcast.internal.json.JsonValue)85 Test (org.junit.Test)36 JsonObject (com.hazelcast.internal.json.JsonObject)35 QuickTest (com.hazelcast.test.annotation.QuickTest)30 JsonArray (com.hazelcast.internal.json.JsonArray)28 HazelcastJsonValue (com.hazelcast.core.HazelcastJsonValue)25 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)20 ArrayList (java.util.ArrayList)9 HashMap (java.util.HashMap)8 HazelcastInstance (com.hazelcast.core.HazelcastInstance)6 KubernetesApiProvider.convertToString (com.hazelcast.kubernetes.KubernetesApiProvider.convertToString)6 EndpointAddress (com.hazelcast.kubernetes.KubernetesClient.EndpointAddress)6 HashSet (java.util.HashSet)6 ConnectionResponse (com.hazelcast.internal.ascii.HTTPCommunicator.ConnectionResponse)5 SlowTest (com.hazelcast.test.annotation.SlowTest)5 Address (com.hazelcast.cluster.Address)4 CPMember (com.hazelcast.cp.CPMember)4 IOException (java.io.IOException)4 JsonUtil.fromJsonObject (com.hazelcast.internal.util.JsonUtil.fromJsonObject)3 JsonUtil.getString (com.hazelcast.internal.util.JsonUtil.getString)3