|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with 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, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | +package com.cloud.api.commands; |
| 18 | + |
| 19 | +import junit.framework.Assert; |
| 20 | +import junit.framework.TestCase; |
| 21 | + |
| 22 | +import org.junit.Before; |
| 23 | +import org.junit.Rule; |
| 24 | +import org.junit.Test; |
| 25 | +import org.junit.rules.ExpectedException; |
| 26 | +import org.mockito.Mockito; |
| 27 | + |
| 28 | +import com.cloud.exception.InvalidParameterValueException; |
| 29 | +import com.cloud.projects.Project; |
| 30 | +import com.cloud.projects.ProjectService; |
| 31 | +import com.cloud.user.Account; |
| 32 | + |
| 33 | +public class AddAccountToProjectCmdTest extends TestCase { |
| 34 | + |
| 35 | + |
| 36 | + private AddAccountToProjectCmd addAccountToProjectCmd; |
| 37 | + |
| 38 | + @Rule |
| 39 | + public ExpectedException expectedException = ExpectedException.none(); |
| 40 | + |
| 41 | + @Before |
| 42 | + public void setUp() { |
| 43 | + addAccountToProjectCmd = new AddAccountToProjectCmd(){ |
| 44 | + |
| 45 | + @Override |
| 46 | + public Long getProjectId() { |
| 47 | + return 2L; |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public String getAccountName() { |
| 52 | + |
| 53 | + // to run the test testGetEventDescriptionForAccount set the accountName |
| 54 | +// return "accountName"; |
| 55 | + // to run the test the testGetEventDescriptionForNullAccount return accountname as null |
| 56 | + return null; |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public String getEmail() { |
| 61 | +// return "customer@abc.com"; |
| 62 | + return null; |
| 63 | + } |
| 64 | + |
| 65 | + }; |
| 66 | + } |
| 67 | + |
| 68 | + |
| 69 | + /**** |
| 70 | + * Condition not handled in the code |
| 71 | + * |
| 72 | + *****/ |
| 73 | + |
| 74 | + /*@Test |
| 75 | + public void testGetEntityOwnerIdForNullProject() { |
| 76 | + |
| 77 | + ProjectService projectService = Mockito.mock(ProjectService.class); |
| 78 | + Mockito.when(projectService.getProject(Mockito.anyLong())).thenReturn(null); |
| 79 | + addAccountToProjectCmd._projectService = projectService; |
| 80 | + |
| 81 | + try { |
| 82 | + addAccountToProjectCmd.getEntityOwnerId(); |
| 83 | + } catch(InvalidParameterValueException exception) { |
| 84 | + Assert.assertEquals("Unable to find project by id 2", exception.getLocalizedMessage()); |
| 85 | + } |
| 86 | + |
| 87 | + }*/ |
| 88 | + |
| 89 | + |
| 90 | + @Test |
| 91 | + public void testGetEntityOwnerIdForProject() { |
| 92 | + |
| 93 | + Project project = Mockito.mock(Project.class); |
| 94 | + Mockito.when(project.getId()).thenReturn(2L); |
| 95 | + |
| 96 | + ProjectService projectService = Mockito.mock(ProjectService.class); |
| 97 | + Account account = Mockito.mock(Account.class); |
| 98 | + |
| 99 | + Mockito.when(account.getId()).thenReturn(2L); |
| 100 | + Mockito.when(projectService.getProject(Mockito.anyLong())).thenReturn( |
| 101 | + project); |
| 102 | + |
| 103 | + Mockito.when(projectService.getProjectOwner(Mockito.anyLong())) |
| 104 | + .thenReturn(account); |
| 105 | + addAccountToProjectCmd._projectService = projectService; |
| 106 | + |
| 107 | + Assert.assertEquals(2L, addAccountToProjectCmd.getEntityOwnerId()); |
| 108 | + |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * To run the test uncomment the return statement for getAccountName() in setup() and return null |
| 113 | + * |
| 114 | + * **/ |
| 115 | + |
| 116 | + |
| 117 | + /*@Test |
| 118 | + public void testGetEventDescriptionForNullAccount() { |
| 119 | + |
| 120 | + String result = addAccountToProjectCmd.getEventDescription(); |
| 121 | + String expected = "Sending invitation to email null to join project: 2"; |
| 122 | + Assert.assertEquals(expected, result); |
| 123 | + |
| 124 | + }*/ |
| 125 | + |
| 126 | + /*** |
| 127 | + * |
| 128 | + * |
| 129 | + * |
| 130 | + * ***/ |
| 131 | + |
| 132 | + |
| 133 | + |
| 134 | + /*@Test |
| 135 | + public void testGetEventDescriptionForAccount() { |
| 136 | + |
| 137 | + String result = addAccountToProjectCmd.getEventDescription(); |
| 138 | + String expected = "Adding account accountName to project: 2"; |
| 139 | + Assert.assertEquals(expected, result); |
| 140 | + |
| 141 | + }*/ |
| 142 | + |
| 143 | + @Test |
| 144 | + public void testExecuteForNullAccountNameEmail() { |
| 145 | + |
| 146 | + try { |
| 147 | + addAccountToProjectCmd.execute(); |
| 148 | + } catch(InvalidParameterValueException exception) { |
| 149 | + Assert.assertEquals("Either accountName or email is required", exception.getLocalizedMessage()); |
| 150 | + } |
| 151 | + |
| 152 | + } |
| 153 | + |
| 154 | + |
| 155 | + /*@Test |
| 156 | + public void testExecuteForAccountNameEmail() { |
| 157 | + |
| 158 | + try { |
| 159 | + |
| 160 | + ComponentLocator c = Mockito.mock(ComponentLocator.class); |
| 161 | + UserContext userContext = Mockito.mock(UserContext.class); |
| 162 | + |
| 163 | +// Mockito.when(userContext.current()).thenReturn(userContext); |
| 164 | + |
| 165 | + |
| 166 | + addAccountToProjectCmd.execute(); |
| 167 | + } catch(InvalidParameterValueException exception) { |
| 168 | + Assert.assertEquals("Either accountName or email is required", exception.getLocalizedMessage()); |
| 169 | + } |
| 170 | + |
| 171 | + }*/ |
| 172 | + |
| 173 | + |
| 174 | + |
| 175 | +} |
0 commit comments