1 /** 2 * Copyright (c) 2013-2023, jcabi.com 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 1) Redistributions of source code must retain the above 8 * copyright notice, this list of conditions and the following 9 * disclaimer. 2) Redistributions in binary form must reproduce the above 10 * copyright notice, this list of conditions and the following 11 * disclaimer in the documentation and/or other materials provided 12 * with the distribution. 3) Neither the name of the jcabi.com nor 13 * the names of its contributors may be used to endorse or promote 14 * products derived from this software without specific prior written 15 * permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT 19 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 20 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 21 * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 22 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 28 * OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 package com.jcabi.github.mock; 31 32 import com.jcabi.github.DeployKey; 33 import com.jcabi.github.DeployKeys; 34 import org.hamcrest.MatcherAssert; 35 import org.hamcrest.Matchers; 36 import org.junit.Test; 37 38 /** 39 * Test case for {@link MkDeployKeys}. 40 * @author Andres Candal (andres.candal@rollasolution.com) 41 * @version $Id: 3b498b56281a67839dff8028e843033d380845ca $ 42 * @since 0.8 43 */ 44 public final class MkDeployKeysTest { 45 /** 46 * MkDeployKeys can fetch empty list of deploy keys. 47 * @throws Exception if some problem inside 48 */ 49 @Test 50 public void canFetchEmptyListOfDeployKeys() throws Exception { 51 final DeployKeys deployKeys = new MkGithub().randomRepo().keys(); 52 MatcherAssert.assertThat( 53 deployKeys.iterate(), 54 Matchers.emptyIterable() 55 ); 56 } 57 58 /** 59 * MkDeployKeys can fetch a single deploy key. 60 * @throws Exception If some problem inside 61 */ 62 @Test 63 public void canFetchSingleDeployKey() throws Exception { 64 final DeployKeys keys = new MkGithub().randomRepo().keys(); 65 final DeployKey key = keys.create("Title", "Key"); 66 MatcherAssert.assertThat(keys.get(key.number()), Matchers.equalTo(key)); 67 } 68 69 /** 70 * MkDeployKeys can create a deploy key. 71 * @throws Exception If some problem inside. 72 */ 73 @Test 74 public void canCreateDeployKey() throws Exception { 75 final DeployKeys keys = new MkGithub().randomRepo().keys(); 76 final DeployKey key = keys.create("Title1", "Key1"); 77 MatcherAssert.assertThat(key, Matchers.equalTo(keys.get(key.number()))); 78 } 79 80 /** 81 * MkDeployKeys can create distinct deploy keys. 82 * Reproduces bug described in issue #346. 83 * @throws Exception If some problem inside. 84 */ 85 @Test 86 public void canCreateDistinctDeployKeys() throws Exception { 87 final DeployKeys keys = new MkGithub().randomRepo().keys(); 88 final DeployKey first = keys.create("Title2", "Key2"); 89 final DeployKey second = keys.create("Title3", "Key3"); 90 MatcherAssert.assertThat( 91 first, 92 Matchers.not(Matchers.is(second)) 93 ); 94 MatcherAssert.assertThat( 95 first.number(), 96 Matchers.not(Matchers.is(second.number())) 97 ); 98 } 99 100 /** 101 * MkDeployKeys can be represented in JSON format. 102 * Reproduces bug described in issue #346. 103 * @throws Exception If some problem inside. 104 */ 105 @Test 106 public void canRepresentAsJson() throws Exception { 107 final DeployKeys keys = new MkGithub().randomRepo().keys(); 108 final DeployKey first = keys.create("Title4", "Key4"); 109 MatcherAssert.assertThat( 110 first.json().toString(), 111 Matchers.allOf( 112 Matchers.containsString("\"title\":\"Title4\""), 113 Matchers.containsString("\"key\":\"Key4\"") 114 ) 115 ); 116 } 117 }