Skip to content

Commit ce31d3b

Browse files
committed
Fix checkstyle in monitoring v3 sample.
Also, start applying checkstyles on test classes so that new samples with out-of-spec test classes won't get added.
1 parent 8539f88 commit ce31d3b

File tree

7 files changed

+117
-57
lines changed

7 files changed

+117
-57
lines changed

google-checks.xml

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,21 @@
22
<!DOCTYPE module PUBLIC
33
"-//Puppy Crawl//DTD Check Configuration 1.3//EN"
44
"http://www.puppycrawl.com/dtds/configuration_1_3.dtd">
5+
<!--
6+
Copyright 2016 Google Inc. All Rights Reserved.
7+
8+
Licensed under the Apache License, Version 2.0 (the "License");
9+
you may not use this file except in compliance with the License.
10+
You may obtain a copy of the License at
11+
12+
http://www.apache.org/licenses/LICENSE-2.0
13+
14+
Unless required by applicable law or agreed to in writing, software
15+
distributed under the License is distributed on an "AS IS" BASIS,
16+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
See the License for the specific language governing permissions and
18+
limitations under the License.
19+
-->
520

621
<!--
722
Checkstyle configuration that checks the Google coding conventions from:
@@ -14,9 +29,7 @@
1429
Most Checks are configurable, be sure to consult the documentation.
1530
To completely disable a check, just comment it out or delete it from the file.
1631
Authors: Max Vetrenko, Ruslan Diachenko, Roman Ivanov.
17-
1832
-->
19-
2033
<module name = "Checker">
2134
<property name="charset" value="UTF-8"/>
2235

@@ -25,11 +38,13 @@
2538
<property name="fileExtensions" value="java, properties, xml"/>
2639
<!-- Checks for whitespace -->
2740
<!-- See http://checkstyle.sf.net/config_whitespace.html -->
28-
<module name="FileTabCharacter">
29-
<property name="eachLine" value="true"/>
30-
</module>
41+
<module name="FileTabCharacter">
42+
<property name="eachLine" value="true"/>
43+
</module>
3144

3245
<module name="TreeWalker">
46+
<!-- Make @SuppressWarnings available to checkstyle http://stackoverflow.com/a/22556386/101923 -->
47+
<module name="SuppressWarningsHolder" />
3348
<module name="UnusedImports"/>
3449
<module name="OuterTypeFilename"/>
3550
<module name="IllegalTokenText">
@@ -179,6 +194,7 @@
179194
</module>
180195
<module name="JavadocMethod">
181196
<property name="scope" value="public"/>
197+
<property name="allowMissingJavadoc" value="true"/>
182198
<property name="allowMissingParamTags" value="true"/>
183199
<property name="allowMissingThrowsTags" value="true"/>
184200
<property name="allowMissingReturnTag" value="true"/>
@@ -198,4 +214,7 @@
198214
<property name="exceptionVariableName" value="expected"/>
199215
</module>
200216
</module>
217+
218+
<!-- Allow silencing rules with annotations http://stackoverflow.com/a/22556386/101923 -->
219+
<module name="SuppressWarningsFilter" />
201220
</module>

monitoring/v3/README.md

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ Go to the [Google Developers Console](https://console.developer.google.com).
1515

1616
* Go too API Manager -> Credentials
1717
* Click ['New Credentials', and create a Service Account](https://console.developers.google.com/project/_/apiui/credential/serviceaccount)
18-
Download the JSON for this service account, and set the `GOOGLE_APPLICATION_CREDENTIALS`
19-
environment variable to point to the file containing the JSON credentials.
18+
Download the JSON for this service account, and set the
19+
`GOOGLE_APPLICATION_CREDENTIALS` environment variable to point to the file
20+
containing the JSON credentials.
2021

2122
```
2223
export GOOGLE_APPLICATION_CREDENTIALS=~/Downloads/<project-id>-0123456789abcdef.json
@@ -26,23 +27,25 @@ Go to the [Google Developers Console](https://console.developer.google.com).
2627

2728
To run locally:
2829
* `mvn clean install`
29-
* `./run_monitoring_example.sh <YOUR-PROJECT-ID>
30+
* `./list_resources_example.sh <YOUR-PROJECT-ID>
3031
* `./run_custom_metrics.sh <YOUR-PROJECT-ID>
3132

3233
## Run Tests
3334

34-
The tests emulate what the scripts accomplish, so there isn't a reason why they need to be run if the examples work.
35-
However, if you'd like to run them, change TEST_PROJECT_ID in ListResourcesTest to the appropriate project ID
36-
that matches the Service Account pointed to by GOOGLE_APPLICATION_CREDENTIALS, then run:
35+
The tests emulate what the scripts accomplish, so there isn't a reason why they
36+
need to be run if the examples work. However, if you'd like to run them, change
37+
`TEST_PROJECT_ID` in [`ListResourcesTest`](src/test/java/ListResourcesTest.java)
38+
to the appropriate project ID that matches the Service Account pointed to by
39+
`GOOGLE_APPLICATION_CREDENTIALS`, then run:
3740

3841
mvn test -DskipTests=false
3942

4043
## Contributing changes
4144

42-
See CONTRIBUTING.md
45+
See [CONTRIBUTING.md](../../CONTRIBUTING.md).
4346

4447
## Licensing
4548

46-
* See [LICENSE](LICENSE)
49+
See [LICENSE](../../LICENSE).
4750

4851

monitoring/v3/pom.xml

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
<relativePath>../..</relativePath>
1515
</parent>
1616

17-
1817
<properties>
1918
<project.http.version>1.19.0</project.http.version>
2019
<project.oauth.version>1.19.0</project.oauth.version>
@@ -43,38 +42,45 @@
4342
<artifactId>google-oauth-client-jetty</artifactId>
4443
<version>${project.oauth.version}</version>
4544
</dependency>
45+
<dependency>
46+
<groupId>joda-time</groupId>
47+
<artifactId>joda-time</artifactId>
48+
<version>2.9</version>
49+
</dependency>
50+
<dependency>
51+
<groupId>org.apache.commons</groupId>
52+
<artifactId>commons-lang3</artifactId>
53+
<version>3.4</version>
54+
</dependency>
55+
<dependency>
56+
<groupId>com.google.apis</groupId>
57+
<artifactId>google-api-services-monitoring</artifactId>
58+
<version>v3-rev1-1.21.0</version>
59+
</dependency>
4660
<dependency>
4761
<groupId>com.google.code.gson</groupId>
4862
<artifactId>gson</artifactId>
4963
<version>2.3.1</version>
5064
</dependency>
65+
66+
<!-- Test Dependencies -->
5167
<dependency>
5268
<groupId>junit</groupId>
5369
<artifactId>junit</artifactId>
54-
<scope>test</scope>
5570
<version>4.12</version>
71+
<scope>test</scope>
5672
</dependency>
5773
<dependency>
5874
<groupId>com.jcabi</groupId>
5975
<artifactId>jcabi-matchers</artifactId>
60-
<scope>test</scope>
6176
<version>1.3</version>
77+
<scope>test</scope>
6278
</dependency>
6379
<dependency>
64-
<groupId>joda-time</groupId>
65-
<artifactId>joda-time</artifactId>
66-
<version>2.9</version>
67-
</dependency>
68-
<dependency>
69-
<groupId>org.apache.commons</groupId>
70-
<artifactId>commons-lang3</artifactId>
71-
<version>3.4</version>
72-
</dependency>
73-
<!-- To be replaced by a Maven dependency on public release -->
74-
<dependency>
75-
<groupId>com.google.apis</groupId>
76-
<artifactId>google-api-services-monitoring</artifactId>
77-
<version>v3-rev1-1.21.0</version>
80+
<groupId>com.google.truth</groupId>
81+
<artifactId>truth</artifactId>
82+
<version>0.28</version>
83+
<scope>test</scope>
7884
</dependency>
7985
</dependencies>
8086
<build>

monitoring/v3/src/test/java/CreateCustomMetricTest.java

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,27 @@
1-
/**
1+
/*
22
* Copyright (c) 2015 Google Inc.
3-
* <p/>
3+
*
44
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
55
* use this file except in compliance with the License. You may obtain a copy of
66
* the License at
7-
* <p/>
7+
*
88
* http://www.apache.org/licenses/LICENSE-2.0
9-
* <p/>
9+
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
1212
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
1313
* License for the specific language governing permissions and limitations under
1414
* the License.
1515
*/
1616

17+
import static com.google.common.truth.Truth.assertThat;
18+
1719
import com.google.api.services.monitoring.v3.Monitoring;
1820
import com.google.api.services.monitoring.v3.model.Point;
21+
import com.google.common.collect.ImmutableList;
22+
1923
import org.apache.commons.lang3.RandomStringUtils;
20-
import org.junit.Assert;
24+
2125
import org.junit.Before;
2226
import org.junit.Test;
2327

@@ -49,8 +53,6 @@ public int nextInt(int bound) {
4953

5054
/**
5155
* Creates the monitoring service client.
52-
*
53-
* @throws Exception
5456
*/
5557
@Before
5658
public void setUp() throws Exception {
@@ -62,8 +64,6 @@ public void setUp() throws Exception {
6264

6365
/**
6466
* Tests that the value written for a custom metric can be read back correctly.
65-
*
66-
* @throws Exception
6767
*/
6868
@Test
6969
public void testValueRead() throws Exception {
@@ -75,17 +75,14 @@ public void testValueRead() throws Exception {
7575
underTest.writeCustomMetricTimeseriesValue();
7676
// give time for write to register
7777
Thread.sleep(2000);
78-
List<Point> response = underTest.readTimeseriesValue()
79-
.getTimeSeries().get(0).getPoints();
78+
List<Point> response =
79+
underTest.readTimeseriesValue().getTimeSeries().get(0).getPoints();
8080

81-
boolean found = false;
81+
ImmutableList.Builder<Long> timeSeries = ImmutableList.builder();
8282
for (Point p : response) {
83-
System.out.println("found a response " + p.getValue().getInt64Value());
84-
if (p.getValue().getInt64Value() == 0) {
85-
found = true;
86-
}
83+
timeSeries.add(p.getValue().getInt64Value());
8784
}
88-
Assert.assertTrue(found);
85+
assertThat(timeSeries.build()).contains(0L);
8986
}
9087

9188
}

monitoring/v3/src/test/java/ListResourcesTest.java

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
1-
/**
1+
/*
22
* Copyright (c) 2015 Google Inc.
3-
* <p/>
3+
*
44
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
55
* use this file except in compliance with the License. You may obtain a copy of
66
* the License at
7-
* <p/>
7+
*
88
* http://www.apache.org/licenses/LICENSE-2.0
9-
* <p/>
9+
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
1212
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
1313
* License for the specific language governing permissions and limitations under
1414
* the License.
1515
*/
1616

17+
import static com.google.common.truth.Truth.assertThat;
1718

1819
import com.google.api.services.monitoring.v3.Monitoring;
19-
import junit.framework.Assert;
20+
2021
import org.junit.Before;
2122
import org.junit.Test;
2223

@@ -34,7 +35,7 @@ public class ListResourcesTest {
3435
/**
3536
* The project ID of the project created for the integration tests.
3637
*/
37-
public static final String TEST_PROJECT_ID = "cloud-monitoring-dev";
38+
public static final String TEST_PROJECT_ID = "cloud-samples-tests";
3839

3940
/**
4041
* Google Cloud Monitoring client to integration test.
@@ -63,7 +64,9 @@ public void setUp() throws Exception {
6364
public void testListMonitoredResourceDescriptors() throws Exception {
6465
this.underTest.listMonitoredResourceDescriptors();
6566
String result = new String(os.toByteArray());
66-
Assert.assertTrue(result.contains("An application running in Google App Engine"));
67+
assertThat(result)
68+
.named("output text stream")
69+
.contains("An application running in Google App Engine");
6770
}
6871

6972
/**
@@ -74,7 +77,9 @@ public void testListMonitoredResourceDescriptors() throws Exception {
7477
public void testListMetrics() throws Exception {
7578
this.underTest.listMetricDescriptors();
7679
String result = new String(os.toByteArray());
77-
Assert.assertTrue(result.contains("Delta CPU usage time. Units are second"));
80+
assertThat(result)
81+
.named("output text stream")
82+
.contains("Delta CPU usage time. Units are second");
7883
}
7984

8085
/**
@@ -85,7 +90,8 @@ public void testListMetrics() throws Exception {
8590
public void testListTimeseries() throws Exception {
8691
this.underTest.listTimeseries();
8792
String result = new String(os.toByteArray());
88-
Assert.assertTrue(result.contains("listTimeseries response"));
93+
assertThat(result)
94+
.named("output text stream")
95+
.contains("listTimeseries response");
8996
}
90-
9197
}

pom.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@
6363
<consoleOutput>true</consoleOutput>
6464
<failOnViolation>true</failOnViolation>
6565
<failsOnError>true</failsOnError>
66+
<includeTestSourceDirectory>true</includeTestSourceDirectory>
67+
<suppressionsLocation>suppressions.xml</suppressionsLocation>
6668
</configuration>
6769
<executions>
6870
<execution><goals><goal>check</goal></goals></execution>

suppressions.xml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0"?>
2+
<!DOCTYPE suppressions PUBLIC
3+
"-//Puppy Crawl//DTD Suppressions 1.1//EN"
4+
"http://www.puppycrawl.com/dtds/suppressions_1_1.dtd">
5+
<!--
6+
Copyright 2016 Google Inc. All Rights Reserved.
7+
8+
Licensed under the Apache License, Version 2.0 (the "License");
9+
you may not use this file except in compliance with the License.
10+
You may obtain a copy of the License at
11+
12+
http://www.apache.org/licenses/LICENSE-2.0
13+
14+
Unless required by applicable law or agreed to in writing, software
15+
distributed under the License is distributed on an "AS IS" BASIS,
16+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
See the License for the specific language governing permissions and
18+
limitations under the License.
19+
-->
20+
<suppressions>
21+
<!-- For our tests we do not demand API documentation. -->
22+
<suppress files="[/\\]src[/\\]test[/\\]java[/\\].*\.java" checks="Javadoc.*"/>
23+
24+
<!-- Suppress checks for generated code. -->
25+
<suppress files="[/\\]target[/\\]" checks=".+"/>
26+
<suppress files=".*AutoValue_.*\.java$" checks=".+"/>
27+
</suppressions>

0 commit comments

Comments
 (0)