summaryrefslogtreecommitdiffstats
path: root/tests_mgrapp/src/com/google/codereview/manager/unpack/ReceivedBundleUnpackerTest.java
blob: cad463e7f918ac6f729c64c2792f4a5feb857784 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// Copyright 2008 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.codereview.manager.unpack;

import com.google.codereview.TrashTestCase;
import com.google.codereview.internal.NextReceivedBundle.NextReceivedBundleRequest;
import com.google.codereview.internal.NextReceivedBundle.NextReceivedBundleResponse;
import com.google.codereview.internal.UpdateReceivedBundle.UpdateReceivedBundleRequest;
import com.google.codereview.internal.UpdateReceivedBundle.UpdateReceivedBundleResponse;
import com.google.codereview.manager.Backend;
import com.google.codereview.manager.RepositoryCache;
import com.google.codereview.rpc.MockRpcChannel;
import com.google.protobuf.ByteString;
import com.google.protobuf.Message;
import com.google.protobuf.RpcCallback;
import com.google.protobuf.RpcChannel;
import com.google.protobuf.RpcController;
import com.google.protobuf.Descriptors.MethodDescriptor;

import org.spearce.jgit.lib.Repository;

import java.io.File;

public class ReceivedBundleUnpackerTest extends TrashTestCase {
  private MockRpcChannel rpc;
  private RepositoryCache repoCache;
  private Backend server;

  @Override
  protected void setUp() throws Exception {
    super.setUp();

    rpc = new MockRpcChannel();
    repoCache = new RepositoryCache(tempRoot);
    server = new Backend(repoCache, rpc, null, null);
    new Repository(new File(tempRoot, "foo.git")).create();
  }

  public void testNextReceivedBundle_EmptyQueue() throws Exception {
    final ReceivedBundleUnpacker rbu = newRBU();
    rpc.add(new RpcChannel() {
      public void callMethod(final MethodDescriptor method,
          final RpcController controller, final Message request,
          final Message responsePrototype, final RpcCallback<Message> done) {
        assertEquals("NextReceivedBundle", method.getName());
        assertSame(NextReceivedBundleRequest.getDefaultInstance(), request);

        final NextReceivedBundleResponse.Builder r =
            NextReceivedBundleResponse.newBuilder();
        r.setStatusCode(NextReceivedBundleResponse.CodeType.QUEUE_EMPTY);
        done.run(r.build());
      }
    });
    rbu.run();
    rpc.assertAllCallsMade();
  }

  public void testNextReceivedBundle_GetBundle() throws Exception {
    final String bundleKey = "bundle-key-abcd123efg";
    final NextReceivedBundleResponse nrb[] = new NextReceivedBundleResponse[1];
    final UpdateReceivedBundleRequest urbr[] =
        new UpdateReceivedBundleRequest[1];

    final ReceivedBundleUnpacker rbu =
        new ReceivedBundleUnpacker(server) {
          @Override
          protected UpdateReceivedBundleRequest unpackImpl(
              final NextReceivedBundleResponse rsp) {
            assertNotNull("unpackImpl only after bundle available", nrb[0]);
            assertSame(nrb[0], rsp);

            final UpdateReceivedBundleRequest.Builder r =
                UpdateReceivedBundleRequest.newBuilder();
            r.setBundleKey(bundleKey);
            r.setStatusCode(UpdateReceivedBundleRequest.CodeType.UNPACKED_OK);
            urbr[0] = r.build();
            return urbr[0];
          }
        };

    rpc.add(new RpcChannel() {
      public void callMethod(final MethodDescriptor method,
          final RpcController controller, final Message request,
          final Message responsePrototype, final RpcCallback<Message> done) {
        assertEquals("NextReceivedBundle", method.getName());
        assertSame(NextReceivedBundleRequest.getDefaultInstance(), request);

        final NextReceivedBundleResponse.Builder r =
            NextReceivedBundleResponse.newBuilder();
        r.setStatusCode(NextReceivedBundleResponse.CodeType.BUNDLE_AVAILABLE);
        r.setBundleKey(bundleKey);
        r.setBundleData(ByteString.EMPTY);
        r.setDestProject("foo.git");
        r.setDestProjectKey("project:foo.git");
        r.setDestBranchKey("branch:refs/heads/master");
        r.setOwner("author@example.com");
        nrb[0] = r.build();
        done.run(nrb[0]);
      }
    });

    rpc.add(new RpcChannel() {
      public void callMethod(final MethodDescriptor method,
          final RpcController controller, final Message request,
          final Message responsePrototype, final RpcCallback<Message> done) {
        assertEquals("UpdateReceivedBundle", method.getName());
        assertSame(urbr[0], request);

        final UpdateReceivedBundleResponse.Builder r =
            UpdateReceivedBundleResponse.newBuilder();
        r.setStatusCode(UpdateReceivedBundleResponse.CodeType.UPDATED);
        done.run(r.build());
      }
    });

    rpc.add(new RpcChannel() {
      public void callMethod(final MethodDescriptor method,
          final RpcController controller, final Message request,
          final Message responsePrototype, final RpcCallback<Message> done) {
        assertEquals("NextReceivedBundle", method.getName());
        assertSame(NextReceivedBundleRequest.getDefaultInstance(), request);

        final NextReceivedBundleResponse.Builder r =
            NextReceivedBundleResponse.newBuilder();
        r.setStatusCode(NextReceivedBundleResponse.CodeType.QUEUE_EMPTY);
        done.run(r.build());
      }
    });

    rbu.run();
    assertNotNull(nrb[0]);
    assertNotNull(urbr[0]);
    rpc.assertAllCallsMade();
  }

  public void testNextReceivedBundle_RpcFailure() throws Exception {
    final ReceivedBundleUnpacker rbu = newRBU();
    rpc.add(new RpcChannel() {
      public void callMethod(final MethodDescriptor method,
          final RpcController controller, final Message request,
          final Message responsePrototype, final RpcCallback<Message> done) {
        assertEquals("NextReceivedBundle", method.getName());
        controller.setFailed("mock failure");
      }
    });
    rbu.run();
    rpc.assertAllCallsMade();
  }

  public void testNextReceivedBundle_RuntimeException() throws Exception {
    final String msg = "test-a-message-win-a-prize";
    final ReceivedBundleUnpacker rbu = newRBU();
    rpc.add(new RpcChannel() {
      public void callMethod(final MethodDescriptor method,
          final RpcController controller, final Message request,
          final Message responsePrototype, final RpcCallback<Message> done) {
        assertEquals("NextReceivedBundle", method.getName());
        throw new RuntimeException(msg);
      }
    });
    try {
      rbu.run();
      fail("Unpacker did not rethrow an unexpected runtime exception");
    } catch (RuntimeException re) {
      assertEquals(msg, re.getMessage());
    }
    rpc.assertAllCallsMade();
  }

  public void testNextReceivedBundle_RuntimeError() throws Exception {
    final String msg = "test-a-message-win-a-prize";
    final ReceivedBundleUnpacker rbu = newRBU();
    rpc.add(new RpcChannel() {
      public void callMethod(final MethodDescriptor method,
          final RpcController controller, final Message request,
          final Message responsePrototype, final RpcCallback<Message> done) {
        assertEquals("NextReceivedBundle", method.getName());
        throw new OutOfMemoryError(msg);
      }
    });
    try {
      rbu.run();
      fail("Unpacker did not rethrow an unexpected OutOfMemoryError");
    } catch (OutOfMemoryError re) {
      assertEquals(msg, re.getMessage());
    }
    rpc.assertAllCallsMade();
  }

  private ReceivedBundleUnpacker newRBU() {
    return new ReceivedBundleUnpacker(server);
  }
}