Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expand JSON test coverage; add datastore and cloud storage #443

Merged
merged 22 commits into from
Sep 8, 2017
12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,18 @@
<version>2.7.17</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
103 changes: 84 additions & 19 deletions src/test/java/com/google/cloud/tools/libraries/LibrariesTest.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
package com.google.cloud.tools.libraries;
/*
* Copyright 2017 Google Inc.
*
* Licensed 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 com.google.cloud.tools.libraries;

import static org.hamcrest.collection.IsArrayContaining.hasItemInArray;

import java.io.File;
import java.io.FileInputStream;
Expand All @@ -7,8 +25,16 @@
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonObject;
import javax.json.JsonReader;
import javax.json.JsonReaderFactory;
import javax.json.JsonValue;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
Expand All @@ -17,13 +43,6 @@
import org.junit.Test;
import org.xml.sax.SAXException;

import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonObject;
import javax.json.JsonReader;
import javax.json.JsonReaderFactory;
import javax.json.JsonValue;

public class LibrariesTest {

@Test
Expand All @@ -43,20 +62,66 @@ public void testJson() throws FileNotFoundException, URISyntaxException {
JsonReader reader = factory.createReader(in);
Iterator<JsonValue> apis = reader.readArray().iterator();
Assert.assertTrue(apis.hasNext());
for (JsonObject api = (JsonObject) apis.next(); apis.hasNext(); api = (JsonObject) apis.next()) {
while (apis.hasNext()) {
JsonObject api = (JsonObject) apis.next();
verifyApi(api);
}
}

private static final String[] statuses = {"alpha", "beta", "GA"};

private static void verifyApi(JsonObject api) throws URISyntaxException {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit : would using assertApi instead of verifyApi clarify that the intention of the method is to make assertions?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Assert.assertFalse(api.getString("name").isEmpty());
Assert.assertFalse(api.getString("description").isEmpty());
String transport = api.getString("transport");
Assert.assertTrue("http".equals(transport) || "grpc".equals(transport));
new URI(api.getString("documentation"));
if (api.getString("icon") != null) {
new URI(api.getString("icon"));
}
JsonArray clients = api.getJsonArray("clients");
Assert.assertFalse(clients.isEmpty());
for (int i = 0; i < clients.size(); i++) {
JsonObject client = (JsonObject) clients.get(i);
String status = client.getString("status");

Assert.assertThat(statuses, hasItemInArray(status));
new URI(client.getString("apireference"));
new URI(client.getString("site"));
Assert.assertTrue(client.getString("languageLevel").matches("1\\.\\d+\\.\\d+"));
String versionString = client.getString("version");
Assert.assertTrue(versionString.matches("\\d+\\.\\d+\\.\\d+"));
Assert.assertNotNull(client.getJsonObject("mavenCoordinates"));
if (client.getString("source") != null) {
new URI(client.getString("source"));
}
}
}

@Test
public void testMavenCoordinates() throws FileNotFoundException, URISyntaxException {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could this be named testMavenCoordinate_fileIsValid or something? It's hard for me to tell (since I didn't really look at the previous CL) what this test is doing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

JsonReaderFactory factory = Json.createReaderFactory(null);
InputStream in =
new FileInputStream("src/main/java/com/google/cloud/tools/libraries/libraries.json");
JsonReader reader = factory.createReader(in);
Iterator<JsonValue> apis = reader.readArray().iterator();
Assert.assertTrue(apis.hasNext());

new URI(api.getString("documentation"));
if (api.getString("icon") != null) {
new URI(api.getString("icon"));
Map<String, String> map = new HashMap<>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

name of map maybe can be more descriptive.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

while (apis.hasNext()) {
JsonObject api = (JsonObject) apis.next();
String name = api.getString("name");
if (map.containsKey(name)) {
Assert.fail(name + " is defined twice");
}
JsonArray clients = api.getJsonArray("clients");
Assert.assertFalse(clients.isEmpty());
for (int i = 0; i < clients.size(); i++) {
JsonObject client = (JsonObject) clients.get(i);
String status = client.getString("status");
Assert.assertTrue(status,
"beta".equals(status) || "alpha".equals(status) || "GA".equals(status));
JsonObject coordinates =
((JsonObject) api.getJsonArray("clients").get(0)).getJsonObject("mavenCoordinates");
String mavenCoordinates =
coordinates.getString("groupId") + ":" + coordinates.getString("artifactId");
if (map.containsValue(mavenCoordinates)) {
Assert.fail(mavenCoordinates + " is defined twice");
}
map.put(name, mavenCoordinates);
}
}

Expand Down