Skip to content

Commit

Permalink
KAA-876: Extend the internal credentials entity with the application …
Browse files Browse the repository at this point in the history
…ID field
  • Loading branch information
Bohdan Khablenko committed Mar 28, 2016
1 parent 3a29584 commit 66d8fd8
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 82 deletions.
Expand Up @@ -22,40 +22,13 @@


import java.nio.ByteBuffer; import java.nio.ByteBuffer;


/**
* The interface Credentials dao
* @param <T> the type parameter
*/
public interface CredentialsDao<T extends Credentials> extends Dao<T, ByteBuffer> { public interface CredentialsDao<T extends Credentials> extends Dao<T, ByteBuffer> {


/** T save(String applicationId, CredentialsDto credentials);
* Saves given dto
* @param dto
* @return
*/
T save(CredentialsDto dto);


/**
* Find credential by id
* @param id the credential id
* @return credential object
*/
T findById(String id);



/** T find(String applicationId, String credentialsId);
* Updates credential's status by id
* @param id credential's id to be updated
* @param status status to update
* @return updated credential object
*/
T updateStatusById(String id, CredentialsStatus status);


T updateStatus(String applicationId, String credentialsId, CredentialsStatus status);


/** void remove(String applicationId, String credentialsId);
* Removes credential by id
* @param id credential id to be deleted
*/
void removeById(String id);
} }
Expand Up @@ -723,11 +723,11 @@ protected EndpointUserConfigurationDto generateEndpointUserConfigurationDto(Endp
return userConfigurationService.saveUserConfiguration(configurationDto); return userConfigurationService.saveUserConfiguration(configurationDto);
} }


protected CredentialsDto generateCredentials(byte[] credentialsBody, CredentialsStatus status) { protected CredentialsDto generateCredentials(String applicationId, byte[] credentialsBody, CredentialsStatus status) {
CredentialsDto credentialsDto = new CredentialsDto(); CredentialsDto credentialsDto = new CredentialsDto();
credentialsDto.setCredentialsBody(credentialsBody); credentialsDto.setCredentialsBody(credentialsBody);
credentialsDto.setStatus(status); credentialsDto.setStatus(status);
Credentials saved = this.credentialsDao.save(credentialsDto); Credentials saved = this.credentialsDao.save(applicationId, credentialsDto);


CredentialsDto generatedCredentials = saved.toDto(); CredentialsDto generatedCredentials = saved.toDto();
return generatedCredentials; return generatedCredentials;
Expand Down
Expand Up @@ -27,13 +27,22 @@
* *
* @since v0.9.0 * @since v0.9.0
*/ */
public final class CredentialsDto implements HasId, Serializable { public class CredentialsDto implements HasId, Serializable {


private static final long serialVersionUID = 1000L; private static final long serialVersionUID = 1000L;


private String id; private String id;
private byte[] credentialsBody; private byte[] credentialsBody;
private CredentialsStatus status; private CredentialsStatus status = CredentialsStatus.AVAILABLE;

public CredentialsDto() {
}

public CredentialsDto(String id, byte[] credentialsBody, CredentialsStatus status) {
this.id = id;
this.credentialsBody = Arrays.copyOf(credentialsBody, credentialsBody.length);
this.status = status;
}


@Override @Override
public String getId() { public String getId() {
Expand Down
Expand Up @@ -16,6 +16,8 @@


package org.kaaproject.kaa.server.common.nosql.mongo.dao; package org.kaaproject.kaa.server.common.nosql.mongo.dao;


import java.nio.ByteBuffer;

import org.kaaproject.kaa.common.dto.credentials.CredentialsDto; import org.kaaproject.kaa.common.dto.credentials.CredentialsDto;
import org.kaaproject.kaa.common.dto.credentials.CredentialsStatus; import org.kaaproject.kaa.common.dto.credentials.CredentialsStatus;
import org.kaaproject.kaa.server.common.dao.impl.CredentialsDao; import org.kaaproject.kaa.server.common.dao.impl.CredentialsDao;
Expand All @@ -28,11 +30,12 @@
import org.springframework.data.mongodb.core.query.Update; import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;


import java.nio.ByteBuffer; /**

* @author Artur Joshi
import static org.springframework.data.mongodb.core.query.Criteria.where; * @author Bohdan Khablenko
import static org.springframework.data.mongodb.core.query.Query.query; *

* @since v0.9.0
*/
@Repository @Repository
public class CredentialsMongoDao extends AbstractMongoDao<MongoCredentials, ByteBuffer> implements CredentialsDao<MongoCredentials> { public class CredentialsMongoDao extends AbstractMongoDao<MongoCredentials, ByteBuffer> implements CredentialsDao<MongoCredentials> {


Expand All @@ -49,31 +52,33 @@ protected Class<MongoCredentials> getDocumentClass() {
} }


@Override @Override
public MongoCredentials save(CredentialsDto credentialsDto) { public MongoCredentials save(String applicationId, CredentialsDto credentials) {
LOG.debug("Saving {}", credentialsDto.toString()); LOG.debug("Saving credentials [{}] for application [{}]", credentials.toString(), applicationId);
return this.save(new MongoCredentials(credentialsDto)); return this.save(new MongoCredentials(applicationId, credentials));
} }


@Override @Override
public MongoCredentials findById(String id) { public MongoCredentials find(String applicationId, String credentialsId) {
LOG.debug("Searching credential by ID[{}]", id); LOG.debug("Searching for credentials by application ID [{}] and credentials ID [{}]", applicationId, credentialsId);
Query query = Query.query(Criteria.where(MongoModelConstants.CREDENTIALS_ID).is(id)); Query query = Query.query(Criteria.where(MongoModelConstants.CREDENTIALS_ID).is(credentialsId)
.and(MongoModelConstants.APPLICATION_ID).is(applicationId));
return this.findOne(query); return this.findOne(query);
} }


@Override @Override
public MongoCredentials updateStatusById(String id, CredentialsStatus status) { public MongoCredentials updateStatus(String applicationId, String credentialsId, CredentialsStatus status) {
LOG.debug("Updating credentials status with ID[{}] to STATUS[{}]", id, status.toString()); LOG.debug("Settings status [{}] for credentials [{}] in application [{}]", status.toString(), credentialsId, applicationId);
updateFirst( updateFirst(
query(where(MongoModelConstants.CREDENTIALS_ID).is(id)), Query.query(Criteria.where(MongoModelConstants.CREDENTIALS_ID).is(credentialsId).and(MongoModelConstants.APPLICATION_ID).is(applicationId)),
Update.update(MongoModelConstants.CREDENTIAL_STATUS, status)); Update.update(MongoModelConstants.CREDENTIAL_STATUS, status));
return findById(id); return this.find(applicationId, credentialsId);
} }


@Override @Override
public void removeById(String id) { public void remove(String applicationId, String credentialsId) {
LOG.debug("Deleting credential by ID[{}]", id); LOG.debug("Removing credentials [{}] from application [{}]", credentialsId, applicationId);
Query query = Query.query(Criteria.where(MongoModelConstants.CREDENTIALS_ID).is(id)); Query query = Query.query(Criteria.where(MongoModelConstants.CREDENTIALS_ID).is(credentialsId)
.and(MongoModelConstants.APPLICATION_ID).is(applicationId));
this.remove(query); this.remove(query);
} }
} }
Expand Up @@ -35,17 +35,19 @@ public class MongoCredentials implements Credentials, Serializable {


@Id @Id
private String id; private String id;
@Field(CREDENTIALS_APPLICATION_ID)
private String applicationId;
@Field(CREDENTIALS_BODY) @Field(CREDENTIALS_BODY)
private byte[] credentialsBody; private byte[] credentialsBody;
@Field(CREDENTIAL_STATUS) @Field(CREDENTIAL_STATUS)
private CredentialsStatus status; private CredentialsStatus status;


public MongoCredentials() { public MongoCredentials() {
// An empty constructor for Spring
} }


public MongoCredentials(CredentialsDto dto) { public MongoCredentials(String applicationId, CredentialsDto dto) {
this.id = dto.getId(); this.id = dto.getId();
this.applicationId = applicationId;
this.credentialsBody = Arrays.copyOf(dto.getCredentialsBody(), dto.getCredentialsBody().length); this.credentialsBody = Arrays.copyOf(dto.getCredentialsBody(), dto.getCredentialsBody().length);
this.status = dto.getStatus(); this.status = dto.getStatus();
} }
Expand All @@ -62,6 +64,14 @@ public void setId(String id) {
this.id = id; this.id = id;
} }


public String getApplicationId() {
return this.applicationId;
}

public void setApplicationId(String applicationId) {
this.applicationId = applicationId;
}

public byte[] getCredentialsBody() { public byte[] getCredentialsBody() {
return credentialsBody; return credentialsBody;
} }
Expand All @@ -78,34 +88,57 @@ public void setStatus(CredentialsStatus status) {
this.status = status; this.status = status;
} }



@Override @Override
public boolean equals(Object o) { public int hashCode() {
if (this == o) return true; final int prime = 31;
if (o == null || getClass() != o.getClass()) return false; int result = 1;

result = prime * result + ((this.applicationId == null) ? 0 : this.applicationId.hashCode());
MongoCredentials that = (MongoCredentials) o; result = prime * result + Arrays.hashCode(this.credentialsBody);

result = prime * result + ((this.status == null) ? 0 : this.status.hashCode());
if (!Arrays.equals(credentialsBody, that.credentialsBody)) return false; return result;
if (status != that.status) return false;

return true;
} }


@Override @Override
public int hashCode() { public boolean equals(Object obj) {
int result = Arrays.hashCode(credentialsBody); if (this == obj) {
result = 31 * result + status.hashCode(); return true;
return result; }
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
MongoCredentials other = (MongoCredentials) obj;
if (this.applicationId == null) {
if (other.applicationId != null) {
return false;
}
} else if (!this.applicationId.equals(other.applicationId)) {
return false;
}
if (!Arrays.equals(this.credentialsBody, other.credentialsBody)) {
return false;
}
if (this.status != other.status) {
return false;
}
return true;
} }


@Override @Override
public String toString() { public String toString() {
return "MongoCredential{" + StringBuilder builder = new StringBuilder();
"id='" + id + '\'' + builder.append("MongoCredentials [id=");
", credentialsBody=" + Arrays.toString(credentialsBody) + builder.append(this.id);
", status=" + status + builder.append(", applicationId=");
'}'; builder.append(this.applicationId);
builder.append(", credentialsBody=");
builder.append(Arrays.toString(this.credentialsBody));
builder.append(", status=");
builder.append(this.status);
builder.append("]");
return builder.toString();
} }


@Override @Override
Expand Down
Expand Up @@ -139,6 +139,7 @@ private MongoModelConstants() {
* {@link org.kaaproject.kaa.server.common.nosql.mongo.dao.model.MongoCredentials} * {@link org.kaaproject.kaa.server.common.nosql.mongo.dao.model.MongoCredentials}
*/ */
public static final String CREDENTIALS = "credentials"; public static final String CREDENTIALS = "credentials";
public static final String CREDENTIALS_APPLICATION_ID = APPLICATION_ID;
public static final String CREDENTIALS_ID = ID; public static final String CREDENTIALS_ID = ID;
public static final String CREDENTIALS_BODY = "credentials_body"; public static final String CREDENTIALS_BODY = "credentials_body";
public static final String CREDENTIAL_STATUS = "credentials_status"; public static final String CREDENTIAL_STATUS = "credentials_status";
Expand Down
Expand Up @@ -22,6 +22,7 @@
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS) @DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
public class CredentialsMongoDaoTest extends AbstractMongoTest { public class CredentialsMongoDaoTest extends AbstractMongoTest {
private static final byte[] CREDENTIALS_BODY = "credentials_body".getBytes(); private static final byte[] CREDENTIALS_BODY = "credentials_body".getBytes();
private static final String APPLICATION_ID = "application_id";


@BeforeClass @BeforeClass
public static void init() throws Exception { public static void init() throws Exception {
Expand All @@ -40,34 +41,34 @@ public void afterTest() throws IOException {


@Test @Test
public void testFindCredentialsById() { public void testFindCredentialsById() {
CredentialsDto saved = this.generateCredentials(CREDENTIALS_BODY, AVAILABLE); CredentialsDto saved = this.generateCredentials(APPLICATION_ID, CREDENTIALS_BODY, AVAILABLE);
Assert.assertNotNull(saved); Assert.assertNotNull(saved);
Assert.assertNotNull(saved.getId()); Assert.assertNotNull(saved.getId());


Credentials found = this.credentialsDao.findById(saved.getId()); Credentials found = this.credentialsDao.find(APPLICATION_ID, saved.getId());
Assert.assertNotNull(found); Assert.assertNotNull(found);
Assert.assertEquals(saved, found.toDto()); Assert.assertEquals(saved, found.toDto());
} }


@Test @Test
public void testUpdateStatus() { public void testUpdateStatus() {
CredentialsDto credentials = this.generateCredentials(CREDENTIALS_BODY, AVAILABLE); CredentialsDto credentials = this.generateCredentials(APPLICATION_ID, CREDENTIALS_BODY, AVAILABLE);
Assert.assertNotNull(credentials); Assert.assertNotNull(credentials);
Assert.assertNotNull(credentials.getId()); Assert.assertNotNull(credentials.getId());


Credentials updated = this.credentialsDao.updateStatusById(credentials.getId(), REVOKED); Credentials updated = this.credentialsDao.updateStatus(APPLICATION_ID, credentials.getId(), REVOKED);
Assert.assertNotNull(updated); Assert.assertNotNull(updated);
Assert.assertEquals(REVOKED, updated.getStatus()); Assert.assertEquals(REVOKED, updated.getStatus());
} }


@Test @Test
public void testRemoveCredentials() { public void testRemoveCredentials() {
CredentialsDto credentials = this.generateCredentials(CREDENTIALS_BODY, AVAILABLE); CredentialsDto credentials = this.generateCredentials(APPLICATION_ID, CREDENTIALS_BODY, AVAILABLE);
Assert.assertNotNull(credentials); Assert.assertNotNull(credentials);
Assert.assertNotNull(credentials.getId()); Assert.assertNotNull(credentials.getId());


this.credentialsDao.removeById(credentials.getId()); this.credentialsDao.remove(APPLICATION_ID, credentials.getId());
Credentials removed = this.credentialsDao.findById(credentials.getId()); Credentials removed = this.credentialsDao.find(APPLICATION_ID, credentials.getId());
Assert.assertNull(removed); Assert.assertNull(removed);
} }
} }

0 comments on commit 66d8fd8

Please sign in to comment.