-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1222 from mziccard/sink-snippets
Fix Metric.update/delete and add snippets for Metric class
- Loading branch information
Showing
6 changed files
with
296 additions
and
17 deletions.
There are no files selected for viewing
134 changes: 134 additions & 0 deletions
134
...cloud-examples/src/main/java/com/google/cloud/examples/logging/snippets/SinkSnippets.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
/* | ||
* Copyright 2016 Google Inc. All Rights Reserved. | ||
* | ||
* 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. | ||
*/ | ||
|
||
/* | ||
* EDITING INSTRUCTIONS | ||
* This file is referenced in Sink's javadoc. Any change to this file should be reflected in Sink's | ||
* javadoc. | ||
*/ | ||
|
||
package com.google.cloud.examples.logging.snippets; | ||
|
||
import com.google.cloud.logging.Sink; | ||
|
||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.Future; | ||
|
||
/** | ||
* This class contains a number of snippets for the {@link Sink} class. | ||
*/ | ||
public class SinkSnippets { | ||
|
||
private final Sink sink; | ||
|
||
public SinkSnippets(Sink sink) { | ||
this.sink = sink; | ||
} | ||
|
||
/** | ||
* Example of getting the sink's latest information. | ||
*/ | ||
// [TARGET reload()] | ||
public Sink reload() { | ||
// [START reload] | ||
Sink latestSink = sink.reload(); | ||
if (latestSink == null) { | ||
// the sink was not found | ||
} | ||
// [END reload] | ||
return latestSink; | ||
} | ||
|
||
/** | ||
* Example of asynchronously getting the sink's latest information. | ||
*/ | ||
// [TARGET reloadAsync()] | ||
public Sink reloadAsync() throws ExecutionException, InterruptedException { | ||
// [START reloadAsync] | ||
Future<Sink> future = sink.reloadAsync(); | ||
// ... | ||
Sink latestSink = future.get(); | ||
if (latestSink == null) { | ||
// the sink was not found | ||
} | ||
// [END reloadAsync] | ||
return latestSink; | ||
} | ||
|
||
/** | ||
* Example of updating the sink's information. | ||
*/ | ||
// [TARGET update()] | ||
public Sink update() { | ||
// [START update] | ||
Sink updatedSink = sink.toBuilder() | ||
.filter("severity<=ERROR") | ||
.build() | ||
.update(); | ||
// [END update] | ||
return updatedSink; | ||
} | ||
|
||
/** | ||
* Example of asynchronously updating the sink's information. | ||
*/ | ||
// [TARGET updateAsync()] | ||
public Sink updateAsync() throws ExecutionException, InterruptedException { | ||
// [START updateAsync] | ||
Future<Sink> future = sink.toBuilder() | ||
.filter("severity<=ERROR") | ||
.build() | ||
.updateAsync(); | ||
// ... | ||
Sink updatedSink = future.get(); | ||
// [END updateAsync] | ||
return updatedSink; | ||
} | ||
|
||
/** | ||
* Example of deleting the sink. | ||
*/ | ||
// [TARGET delete()] | ||
public boolean delete() { | ||
// [START delete] | ||
boolean deleted = sink.delete(); | ||
if (deleted) { | ||
// the sink was deleted | ||
} else { | ||
// the sink was not found | ||
} | ||
// [END delete] | ||
return deleted; | ||
} | ||
|
||
/** | ||
* Example of asynchronously deleting the sink. | ||
*/ | ||
// [TARGET deleteAsync()] | ||
public boolean deleteAsync() throws ExecutionException, InterruptedException { | ||
// [START deleteAsync] | ||
Future<Boolean> future = sink.deleteAsync(); | ||
// ... | ||
boolean deleted = future.get(); | ||
if (deleted) { | ||
// the sink was deleted | ||
} else { | ||
// the sink was not found | ||
} | ||
// [END deleteAsync] | ||
return deleted; | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
...oud-examples/src/test/java/com/google/cloud/examples/logging/snippets/ITSinkSnippets.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* Copyright 2016 Google Inc. All Rights Reserved. | ||
* | ||
* 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.examples.logging.snippets; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
import com.google.cloud.logging.Logging; | ||
import com.google.cloud.logging.Sink; | ||
import com.google.cloud.logging.SinkInfo; | ||
import com.google.cloud.logging.SinkInfo.Destination; | ||
import com.google.cloud.logging.testing.RemoteLoggingHelper; | ||
|
||
import org.junit.AfterClass; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
|
||
import java.util.concurrent.ExecutionException; | ||
|
||
public class ITSinkSnippets { | ||
|
||
private static final String SINK_NAME = RemoteLoggingHelper.formatForTest("it_sink_snippets"); | ||
private static final String SINK_FILTER = "severity>=ERROR"; | ||
private static final String UPDATED_SINK_FILTER = "severity<=ERROR"; | ||
private static final String DESTINATION = "dataset"; | ||
|
||
private static Logging logging; | ||
private static SinkSnippets sinkSnippets; | ||
|
||
@BeforeClass | ||
public static void beforeClass() { | ||
RemoteLoggingHelper helper = RemoteLoggingHelper.create(); | ||
logging = helper.options().service(); | ||
SinkInfo sinkInfo = SinkInfo.builder(SINK_NAME, Destination.DatasetDestination.of(DESTINATION)) | ||
.filter(SINK_FILTER) | ||
.build(); | ||
sinkSnippets = new SinkSnippets(logging.create(sinkInfo)); | ||
} | ||
|
||
@AfterClass | ||
public static void afterClass() throws Exception { | ||
if (logging != null) { | ||
logging.close(); | ||
} | ||
} | ||
|
||
@Test | ||
public void testSink() throws InterruptedException, ExecutionException { | ||
Sink sink = sinkSnippets.reload(); | ||
assertNotNull(sink); | ||
Sink updatedSink = sinkSnippets.update(); | ||
assertEquals(UPDATED_SINK_FILTER, updatedSink.filter()); | ||
updatedSink = sinkSnippets.reloadAsync(); | ||
assertNotNull(updatedSink); | ||
assertEquals(UPDATED_SINK_FILTER, updatedSink.filter()); | ||
sink.update(); | ||
updatedSink = sinkSnippets.updateAsync(); | ||
assertEquals(UPDATED_SINK_FILTER, updatedSink.filter()); | ||
assertTrue(sinkSnippets.delete()); | ||
assertFalse(sinkSnippets.deleteAsync()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.