Search in sources :

Example 41 with JsonValue

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

the class HazelcastCloudDiscoveryTest method testJsonResponseParse_withDifferentPortOnPrivateAddress.

@Test
public void testJsonResponseParse_withDifferentPortOnPrivateAddress() throws IOException {
    JsonValue jsonResponse = Json.parse(" [{\"private-address\":\"100.96.5.1:5701\",\"public-address\":\"10.113.44.139:31115\"}," + "{\"private-address\":\"100.96.4.2:5701\",\"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", 5701)));
    assertEquals(new Address("10.113.44.130", 31115), privatePublicMap.get(new Address("100.96.4.2", 5701)));
}
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)

Example 42 with JsonValue

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

the class ElasticCatClient method shards.

/**
 * Returns list of shards for given indexes
 *
 * Only STARTED shards are returned.
 *
 * @param indices indexes to return shards for (wildcard format accepted)
 */
@SuppressFBWarnings("RCN_REDUNDANT_NULLCHECK_WOULD_HAVE_BEEN_A_NPE")
public List<Shard> shards(String... indices) {
    Map<String, String> idToAddress = nodes().stream().collect(toMap(Node::getId, Node::getHttpAddress));
    try {
        Request r = new Request("GET", "/_cat/shards/" + String.join(",", indices));
        r.addParameter("format", "json");
        r.addParameter("h", "id,index,shard,prirep,docs,state,ip,node");
        Response res = withRetry(() -> client.performRequest(r), retries);
        try (InputStreamReader reader = new InputStreamReader(res.getEntity().getContent(), UTF_8)) {
            JsonArray array = Json.parse(reader).asArray();
            List<Shard> shards = new ArrayList<>(array.size());
            for (JsonValue value : array) {
                Optional<Shard> shard = convertToShard(value, idToAddress);
                shard.ifPresent(shards::add);
            }
            LOG.log(FINE, "Shards " + shards);
            return shards;
        }
    } catch (IOException e) {
        throw new JetException("Could not get ES shards", e);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) Request(org.elasticsearch.client.Request) ArrayList(java.util.ArrayList) JsonValue(com.hazelcast.internal.json.JsonValue) IOException(java.io.IOException) JetException(com.hazelcast.jet.JetException) Response(org.elasticsearch.client.Response) JsonArray(com.hazelcast.internal.json.JsonArray) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 43 with JsonValue

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

the class InstalledActionTest method filter_by_plugin_type.

@Test
public void filter_by_plugin_type() throws IOException {
    when(serverPluginRepository.getPlugins()).thenReturn(Arrays.asList(newInstalledPlugin(new PluginInfo("foo-external-1").setName("foo-external-1"), PluginType.EXTERNAL), newInstalledPlugin(new PluginInfo("foo-bundled-1").setName("foo-bundled-1"), PluginType.BUNDLED), newInstalledPlugin(new PluginInfo("foo-external-2").setName("foo-external-2"), PluginType.EXTERNAL)));
    db.pluginDbTester().insertPlugin(p -> p.setKee("foo-external-1"), p -> p.setType(Type.EXTERNAL), p -> p.setUpdatedAt(100L));
    db.pluginDbTester().insertPlugin(p -> p.setKee("foo-bundled-1"), p -> p.setType(Type.BUNDLED), p -> p.setUpdatedAt(101L));
    db.pluginDbTester().insertPlugin(p -> p.setKee("foo-external-2"), p -> p.setType(Type.EXTERNAL), p -> p.setUpdatedAt(102L));
    // no type param
    String response = tester.newRequest().execute().getInput();
    JsonArray jsonArray = Json.parse(response).asObject().get("plugins").asArray();
    assertThat(jsonArray).hasSize(3);
    assertThat(jsonArray).extracting(JsonValue::asObject).extracting(members -> members.get("key").asString()).containsExactlyInAnyOrder("foo-external-1", "foo-bundled-1", "foo-external-2");
    // type param == BUNDLED
    response = tester.newRequest().setParam("type", "BUNDLED").execute().getInput();
    jsonArray = Json.parse(response).asObject().get("plugins").asArray();
    assertThat(jsonArray).hasSize(1);
    assertThat(jsonArray).extracting(JsonValue::asObject).extracting(members -> members.get("key").asString()).containsExactlyInAnyOrder("foo-bundled-1");
    // type param == EXTERNAL
    response = tester.newRequest().setParam("type", "EXTERNAL").execute().getInput();
    jsonArray = Json.parse(response).asObject().get("plugins").asArray();
    assertThat(jsonArray).hasSize(2);
    assertThat(jsonArray).extracting(JsonValue::asObject).extracting(members -> members.get("key").asString()).containsExactlyInAnyOrder("foo-external-1", "foo-external-2");
}
Also used : JsonArray(com.hazelcast.internal.json.JsonArray) JsonValue(com.hazelcast.internal.json.JsonValue) Arrays(java.util.Arrays) Type(org.sonar.db.plugin.PluginDto.Type) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) RunWith(org.junit.runner.RunWith) JsonArray(com.hazelcast.internal.json.JsonArray) Random(java.util.Random) ServerPlugin(org.sonar.server.plugins.ServerPlugin) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) DataProviderRunner(com.tngtech.java.junit.dataprovider.DataProviderRunner) Collections.singletonList(java.util.Collections.singletonList) Mockito.verifyZeroInteractions(org.mockito.Mockito.verifyZeroInteractions) Json(com.hazelcast.internal.json.Json) PluginInfo(org.sonar.core.platform.PluginInfo) WebService(org.sonar.api.server.ws.WebService) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) Arrays.asList(java.util.Arrays.asList) Version(org.sonar.updatecenter.common.Version) Plugin(org.sonar.updatecenter.common.Plugin) ServerPluginRepository(org.sonar.server.plugins.ServerPluginRepository) RETURNS_DEEP_STUBS(org.mockito.Mockito.RETURNS_DEEP_STUBS) JsonObject(com.hazelcast.internal.json.JsonObject) DbTester(org.sonar.db.DbTester) FileAndMd5(org.sonar.server.plugins.PluginFilesAndMd5.FileAndMd5) System2(org.sonar.api.utils.System2) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider) JsonAssert.assertJson(org.sonar.test.JsonAssert.assertJson) IOException(java.io.IOException) Test(org.junit.Test) UpdateCenterMatrixFactory(org.sonar.server.plugins.UpdateCenterMatrixFactory) Mockito.when(org.mockito.Mockito.when) WsActionTester(org.sonar.server.ws.WsActionTester) File(java.io.File) Rule(org.junit.Rule) PluginType(org.sonar.server.plugins.PluginType) UpdateCenter(org.sonar.updatecenter.common.UpdateCenter) Optional(java.util.Optional) Action(org.sonar.api.server.ws.WebService.Action) TemporaryFolder(org.junit.rules.TemporaryFolder) Mockito.mock(org.mockito.Mockito.mock) JsonValue(com.hazelcast.internal.json.JsonValue) PluginInfo(org.sonar.core.platform.PluginInfo) Test(org.junit.Test)

Example 44 with JsonValue

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

the class HazelcastCloudDiscovery method parseJsonResponse.

static Map<Address, Address> parseJsonResponse(JsonValue jsonValue) throws IOException {
    List<JsonValue> response = jsonValue.asArray().values();
    Map<Address, Address> privateToPublicAddresses = new HashMap<Address, Address>();
    for (JsonValue value : response) {
        String privateAddress = value.asObject().get(PRIVATE_ADDRESS_PROPERTY).asString();
        String publicAddress = value.asObject().get(PUBLIC_ADDRESS_PROPERTY).asString();
        Address publicAddr = createAddress(publicAddress, -1);
        // if it is not explicitly given, create the private address with public addresses port
        Address privateAddr = createAddress(privateAddress, publicAddr.getPort());
        privateToPublicAddresses.put(privateAddr, publicAddr);
    }
    return privateToPublicAddresses;
}
Also used : Address(com.hazelcast.cluster.Address) HashMap(java.util.HashMap) JsonValue(com.hazelcast.internal.json.JsonValue)

Example 45 with JsonValue

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

the class GcpComputeApi method zones.

List<String> zones(String project, String region, String accessToken) {
    String url = String.format("%s/compute/v1/projects/%s/regions/%s?alt=json&fields=zones", endpoint, project, region);
    String response = RestClient.create(url).withHeader("Authorization", String.format("OAuth %s", accessToken)).get().getBody();
    JsonArray zoneUrls = toJsonArray(Json.parse(response).asObject().get("zones"));
    List<String> zones = new ArrayList<>();
    for (JsonValue value : zoneUrls) {
        zones.add(lastPartOf(value.asString(), "/"));
    }
    return zones;
}
Also used : JsonArray(com.hazelcast.internal.json.JsonArray) ArrayList(java.util.ArrayList) JsonValue(com.hazelcast.internal.json.JsonValue)

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