Skip to content

Commit d5cac93

Browse files
committed
Add log entry's Operation class and tests
1 parent 48971b2 commit d5cac93

2 files changed

Lines changed: 311 additions & 0 deletions

File tree

Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
/*
2+
* Copyright 2016 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.logging;
18+
19+
import static com.google.common.base.Preconditions.checkNotNull;
20+
21+
import com.google.common.base.MoreObjects;
22+
import com.google.logging.v2.LogEntryOperation;
23+
24+
import java.io.Serializable;
25+
import java.util.Objects;
26+
27+
/**
28+
* Additional information about a potentially long-running operation with which a log entry is
29+
* associated.
30+
*
31+
* @see <a href=
32+
* "https://cloud.google.com/logging/docs/api/ref_v2beta1/rest/v2beta1/LogEntry#LogEntryOperation">
33+
* Log Entry Operation</a>
34+
*/
35+
public final class Operation implements Serializable {
36+
37+
private static final long serialVersionUID = 1586890644894328269L;
38+
39+
private final String id;
40+
private final String producer;
41+
private final boolean first;
42+
private final boolean last;
43+
44+
/**
45+
* A builder for {@code Operation} objects.
46+
*/
47+
public static final class Builder {
48+
49+
private String id;
50+
private String producer;
51+
private boolean first;
52+
private boolean last;
53+
54+
Builder(String id, String producer) {
55+
this.id = id;
56+
this.producer = producer;
57+
}
58+
59+
Builder(Operation operation) {
60+
this.id = operation.id;
61+
this.producer = operation.producer;
62+
this.first = operation.first;
63+
this.last = operation.last;
64+
}
65+
66+
/**
67+
* Sets the operation identifier. Log entries with the same identifier are assumed to be part
68+
* of the same operation. The combination of id and producer must be globally unique.
69+
*/
70+
public Builder id(String id) {
71+
this.id = id;
72+
return this;
73+
}
74+
75+
/**
76+
* Sets an arbitrary producer identifier. The combination of producer and id must be globally
77+
* unique. Examples: {@code MyDivision.MyBigCompany.com},
78+
* {@code github.com/MyProject/MyApplication}.
79+
*/
80+
public Builder producer(String producer) {
81+
this.producer = producer;
82+
return this;
83+
}
84+
85+
/**
86+
* Sets whether the corresponding entry is the first log entry in the operation. If not set,
87+
* {@code false} is used.
88+
*/
89+
public Builder first(boolean first) {
90+
this.first = first;
91+
return this;
92+
}
93+
94+
/**
95+
* Sets whether the corresponding entry is the last log entry in the operation. If not set,
96+
* {@code false} is used.
97+
*/
98+
public Builder last(boolean last) {
99+
this.last = last;
100+
return this;
101+
}
102+
103+
/**
104+
* Creates an {@code Operation} object for this builder.
105+
*/
106+
public Operation build() {
107+
return new Operation(this);
108+
}
109+
}
110+
111+
Operation(Builder builder) {
112+
this.id = checkNotNull(builder.id);
113+
this.producer = checkNotNull(builder.producer);
114+
this.first = builder.first;
115+
this.last = builder.last;
116+
}
117+
118+
/**
119+
* Returns the operation identifier. Log entries with the same identifier are assumed to be part
120+
* of the same operation. The combination of this value and {@link #producer()} must be globally
121+
* unique.
122+
*/
123+
public String id() {
124+
return id;
125+
}
126+
127+
/**
128+
* Returns an arbitrary producer identifier. The combination of this value and {@link #id()}
129+
* must be globally unique. Examples: {@code MyDivision.MyBigCompany.com},
130+
* {@code github.com/MyProject/MyApplication}.
131+
*/
132+
public String producer() {
133+
return producer;
134+
}
135+
136+
/**
137+
* Returns {@code true} if the corresponding entry is the first log entry in the operation,
138+
* {@code false} otherwise.
139+
*/
140+
public boolean first() {
141+
return first;
142+
}
143+
144+
/**
145+
* Returns {@code true} if the corresponding entry is the last log entry in the operation,
146+
* {@code false} otherwise.
147+
*/
148+
public boolean last() {
149+
return last;
150+
}
151+
152+
@Override
153+
public boolean equals(Object obj) {
154+
if (obj == this) {
155+
return true;
156+
}
157+
if (!(obj instanceof Operation)) {
158+
return false;
159+
}
160+
Operation other = (Operation) obj;
161+
return Objects.equals(id, other.id)
162+
&& Objects.equals(producer, other.producer)
163+
&& Objects.equals(first, other.first)
164+
&& Objects.equals(last, other.last);
165+
}
166+
167+
@Override
168+
public int hashCode() {
169+
return Objects.hash(id, producer, first, last);
170+
}
171+
172+
@Override
173+
public String toString() {
174+
return MoreObjects.toStringHelper(this)
175+
.add("id", id)
176+
.add("producer", producer)
177+
.add("first", first)
178+
.add("last", last)
179+
.toString();
180+
}
181+
182+
/**
183+
* Returns a {@code Builder} for this operation.
184+
*/
185+
public Builder toBuilder() {
186+
return new Builder(this);
187+
}
188+
189+
LogEntryOperation toPb() {
190+
LogEntryOperation.Builder builder = LogEntryOperation.newBuilder();
191+
builder.setId(id);
192+
builder.setProducer(producer);
193+
builder.setFirst(first);
194+
builder.setLast(last);
195+
return builder.build();
196+
}
197+
198+
/**
199+
* Returns a builder for {@code Operation} objects given the operation and producer identifiers.
200+
* The combination of producer and id must be globally unique.
201+
*/
202+
public static Builder builder(String id, String producer) {
203+
return new Builder(id, producer);
204+
}
205+
206+
/**
207+
* Returns a {@code Operation} object given the operation and producer identifiers. The
208+
* combination of producer and id must be globally unique.
209+
*/
210+
public static Operation of(String id, String producer) {
211+
return builder(id, producer).build();
212+
}
213+
214+
static Operation fromPb(LogEntryOperation operationPb) {
215+
return builder(operationPb.getId(), operationPb.getProducer())
216+
.first(operationPb.getFirst())
217+
.last(operationPb.getLast())
218+
.build();
219+
}
220+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Copyright 2016 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.logging;
18+
19+
import static org.junit.Assert.assertEquals;
20+
import static org.junit.Assert.assertFalse;
21+
import static org.junit.Assert.assertTrue;
22+
23+
import org.junit.Test;
24+
25+
public class OperationTest {
26+
27+
private static final String ID = "id";
28+
private static final String PRODUCER = "producer";
29+
private static final Boolean FIRST = true;
30+
private static final Boolean LAST = false;
31+
private static final Operation OPERATION = Operation.builder(ID, PRODUCER)
32+
.first(FIRST)
33+
.last(LAST)
34+
.build();
35+
36+
@Test
37+
public void testBuilder() {
38+
assertEquals(ID, OPERATION.id());
39+
assertEquals(PRODUCER, OPERATION.producer());
40+
assertTrue(OPERATION.first());
41+
assertFalse(OPERATION.last());
42+
}
43+
44+
@Test
45+
public void testToBuilder() {
46+
compareLogOperation(OPERATION, OPERATION.toBuilder().build());
47+
Operation operation = OPERATION.toBuilder()
48+
.id("newId")
49+
.producer("newProducer")
50+
.first(false)
51+
.last(true)
52+
.build();
53+
assertEquals("newId", operation.id());
54+
assertEquals("newProducer", operation.producer());
55+
assertFalse(operation.first());
56+
assertTrue(operation.last());
57+
operation = operation.toBuilder()
58+
.id(ID)
59+
.producer(PRODUCER)
60+
.first(FIRST)
61+
.last(LAST)
62+
.build();
63+
compareLogOperation(OPERATION, operation);
64+
}
65+
66+
@Test
67+
public void testOf() {
68+
Operation operation = Operation.of(ID, PRODUCER);
69+
assertEquals(ID, operation.id());
70+
assertEquals(PRODUCER, operation.producer());
71+
assertFalse(operation.first());
72+
assertFalse(operation.last());
73+
}
74+
75+
@Test
76+
public void testToAndFromPb() {
77+
compareLogOperation(OPERATION, Operation.fromPb(OPERATION.toPb()));
78+
Operation operation = Operation.of(ID, PRODUCER);
79+
compareLogOperation(operation, Operation.fromPb(operation.toPb()));
80+
}
81+
82+
private void compareLogOperation(Operation expected, Operation value) {
83+
assertEquals(expected, value);
84+
assertEquals(expected.id(), value.id());
85+
assertEquals(expected.producer(), value.producer());
86+
assertEquals(expected.first(), value.first());
87+
assertEquals(expected.last(), value.last());
88+
assertEquals(expected.hashCode(), value.hashCode());
89+
assertEquals(expected.toString(), value.toString());
90+
}
91+
}

0 commit comments

Comments
 (0)