PageRenderTime 867ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/apis/chef/src/test/java/org/jclouds/chef/binders/BindHexEncodedMD5sToJsonPayloadTest.java

http://github.com/jclouds/jclouds
Java | 70 lines | 42 code | 12 blank | 16 comment | 0 complexity | 45b28b473f3aac689911432e8395e1cb MD5 | raw file
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.jclouds.chef.binders;
  18. import static com.google.common.io.BaseEncoding.base16;
  19. import static com.google.common.primitives.Bytes.asList;
  20. import static org.testng.Assert.assertEquals;
  21. import java.io.File;
  22. import javax.ws.rs.HttpMethod;
  23. import org.jclouds.chef.ChefApiMetadata;
  24. import org.jclouds.chef.config.ChefParserModule;
  25. import org.jclouds.http.HttpRequest;
  26. import org.jclouds.json.config.GsonModule;
  27. import org.jclouds.rest.annotations.ApiVersion;
  28. import org.testng.annotations.Test;
  29. import com.google.common.collect.ImmutableSet;
  30. import com.google.inject.AbstractModule;
  31. import com.google.inject.Guice;
  32. import com.google.inject.Injector;
  33. @Test(groups = { "unit" })
  34. public class BindHexEncodedMD5sToJsonPayloadTest {
  35. Injector injector = Guice.createInjector(new AbstractModule() {
  36. @Override
  37. protected void configure() {
  38. bind(String.class).annotatedWith(ApiVersion.class).toInstance(ChefApiMetadata.DEFAULT_API_VERSION);
  39. }
  40. }, new ChefParserModule(), new GsonModule());
  41. BindChecksumsToJsonPayload binder = injector.getInstance(BindChecksumsToJsonPayload.class);
  42. @Test(expectedExceptions = IllegalArgumentException.class)
  43. public void testMustBeIterable() {
  44. HttpRequest request = HttpRequest.builder().method(HttpMethod.POST).endpoint("http://localhost").build();
  45. binder.bindToRequest(request, new File("foo"));
  46. }
  47. public void testCorrect() {
  48. HttpRequest request = HttpRequest.builder().method(HttpMethod.POST).endpoint("http://localhost").build();
  49. binder.bindToRequest(request,
  50. ImmutableSet.of(asList(base16().lowerCase().decode("abddef")), asList(base16().lowerCase().decode("1234"))));
  51. assertEquals(request.getPayload().getRawContent(), "{\"checksums\":{\"abddef\":null,\"1234\":null}}");
  52. }
  53. @Test(expectedExceptions = { NullPointerException.class, IllegalStateException.class })
  54. public void testNullIsBad() {
  55. HttpRequest request = HttpRequest.builder().method(HttpMethod.POST).endpoint("http://localhost").build();
  56. binder.bindToRequest(request, null);
  57. }
  58. }