Coverage Summary for Class: ResourcePasswordPersister (com.acciente.oacc.sql.internal.persister)

Class Class, % Method, % Line, %
ResourcePasswordPersister 100% (1/ 1) 83.3% (5/ 6) 78.2% (43/ 55)


1 /* 2  * Copyright 2009-2018, Acciente LLC 3  * 4  * Acciente LLC licenses this file to you under the 5  * Apache License, Version 2.0 (the "License"); you 6  * may not use this file except in compliance with the 7  * 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 12  * writing, software distributed under the License is 13  * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES 14  * OR CONDITIONS OF ANY KIND, either express or implied. 15  * See the License for the specific language governing 16  * permissions and limitations under the License. 17  */ 18 package com.acciente.oacc.sql.internal.persister; 19  20 import com.acciente.oacc.Resource; 21 import com.acciente.oacc.sql.internal.persister.id.Id; 22 import com.acciente.oacc.sql.internal.persister.id.ResourceId; 23  24 import java.io.Serializable; 25 import java.sql.SQLException; 26  27 public class ResourcePasswordPersister extends Persister implements Serializable { 28  private static final long serialVersionUID = 1L; 29  30  private final SQLPasswordStrings sqlPasswordStrings; 31  32  public ResourcePasswordPersister(SQLPasswordStrings sqlPasswordStrings) { 33  this.sqlPasswordStrings = sqlPasswordStrings; 34  } 35  36  public String getEncryptedBoundPasswordByResourceId(SQLConnection connection, 37  Resource resource) { 38  SQLStatement statement = null; 39  40  try { 41  SQLResult resultSet; 42  43  statement = connection.prepareStatement(sqlPasswordStrings.SQL_findInResourcePassword_Password_BY_ResourceID); 44  statement.setResourceId(1, resource); 45  resultSet = statement.executeQuery(); 46  47  // complain if we do not find the resource 48  if (!resultSet.next()) { 49  throw new IllegalArgumentException(resource + " not found!"); 50  } 51  52  // complain if the resource has no password set 53  final String encryptedBoundPassword = resultSet.getString("Password"); 54  if (encryptedBoundPassword == null) { 55  throw new IllegalStateException(resource + " has no password set!"); 56  } 57  58  // complain if we found more than one resource! 59  // (assuming the PK constraint is being enforced by the DB, currently do not see how this can happen) 60  if (resultSet.next()) { 61  throw new IllegalStateException(resource + " maps to more than one resource!"); 62  } 63  return encryptedBoundPassword; 64  } 65  catch (SQLException e) { 66  throw new RuntimeException(e); 67  } 68  finally { 69  closeStatement(statement); 70  } 71  } 72  73  public void setEncryptedBoundPasswordByResourceId(SQLConnection connection, 74  Resource resource, 75  String newEncryptedBoundPassword) { 76  __setEncryptedBoundPasswordByResourceId(connection, Id.<ResourceId>from(resource.getId()), newEncryptedBoundPassword); 77  } 78  79  public void setEncryptedBoundPasswordByResourceId(SQLConnection connection, 80  Id<ResourceId> resourceId, 81  String newEncryptedBoundPassword) { 82  __setEncryptedBoundPasswordByResourceId(connection, resourceId, newEncryptedBoundPassword); 83  } 84  85  private void __setEncryptedBoundPasswordByResourceId(SQLConnection connection, 86  Id<ResourceId> resourceId, 87  String newEncryptedBoundPassword) { 88  SQLStatement finderStatement = null; 89  SQLStatement insertStatement = null; 90  SQLStatement updateStatement = null; 91  92  try { 93  SQLResult resultSet; 94  95  finderStatement = connection.prepareStatement(sqlPasswordStrings.SQL_findInResourcePassword_Password_BY_ResourceID); 96  finderStatement.setResourceId(1, resourceId); 97  resultSet = finderStatement.executeQuery(); 98  99  if (!resultSet.next()) { 100  // insert new row 101  insertStatement = connection.prepareStatement(sqlPasswordStrings.SQL_createInResourcePassword_WITH_ResourceID_Password); 102  insertStatement.setResourceId(1, resourceId); 103  insertStatement.setString(2, newEncryptedBoundPassword); 104  105  assertOneRowInserted(insertStatement.executeUpdate()); 106  } 107  else { 108  // complain if we found more than one resource! 109  if (resultSet.next()) { 110  throw new IllegalStateException("ResourceId " + resourceId + " maps to more than one resource!"); 111  } 112  113  // update existing row 114  updateStatement = connection.prepareStatement(sqlPasswordStrings.SQL_updateInResourcePassword_Password_BY_ResourceID); 115  updateStatement.setString(1, newEncryptedBoundPassword); 116  updateStatement.setResourceId(2, resourceId); 117  118  assertOneRowUpdated(updateStatement.executeUpdate()); 119  } 120  } 121  catch (SQLException e) { 122  throw new RuntimeException(e); 123  } 124  finally { 125  closeStatement(finderStatement); 126  closeStatement(insertStatement); 127  closeStatement(updateStatement); 128  } 129  } 130  131  public void removeEncryptedBoundPasswordByResourceId(SQLConnection connection, Resource resource) { 132  SQLStatement statement = null; 133  134  try { 135  statement = connection.prepareStatement(sqlPasswordStrings.SQL_removeInResourcePassword_BY_ResourceID); 136  statement.setResourceId(1, resource); 137  138  assertOneRowUpdated(statement.executeUpdate()); 139  } 140  catch (SQLException e) { 141  throw new RuntimeException(e); 142  } 143  finally { 144  closeStatement(statement); 145  } 146  } 147  148 }