Commit 9b05326f authored by Liang Ding's avatar Liang Ding

Option 相关单元测试

parent bf8cb4f8
...@@ -47,24 +47,36 @@ public final class OptionMgmtService { ...@@ -47,24 +47,36 @@ public final class OptionMgmtService {
private OptionRepository optionRepository = OptionRepositoryImpl.getInstance(); private OptionRepository optionRepository = OptionRepositoryImpl.getInstance();
/** /**
* Updates the specified option, if not found the old version of the specified option by id, creates it. * Adds or updates the specified option.
* *
* @param option the specified option * @param option the specified option
* @throws ServiceException service exception * @return option id
* @throws ServiceException
*/ */
public void updateOption(final JSONObject option) throws ServiceException { public String addOrUpdateOption(final JSONObject option) throws ServiceException {
final String id = option.optString(Keys.OBJECT_ID);
final Transaction transaction = optionRepository.beginTransaction(); final Transaction transaction = optionRepository.beginTransaction();
try { try {
if (null != optionRepository.get(id)) { String id = option.optString(Keys.OBJECT_ID);
optionRepository.update(id, option);
if (Strings.isEmptyOrNull(id)) {
id = optionRepository.add(option);
} else { } else {
optionRepository.add(option); final JSONObject old = optionRepository.get(id);
if (null == old) { // The id is specified by caller
id = optionRepository.add(option);
} else {
old.put(Option.OPTION_CATEGORY, option.optString(Option.OPTION_CATEGORY));
old.put(Option.OPTION_VALUE, option.optString(Option.OPTION_VALUE));
optionRepository.update(id, old);
}
} }
transaction.commit(); transaction.commit();
return id;
} catch (final Exception e) { } catch (final Exception e) {
if (transaction.isActive()) { if (transaction.isActive()) {
transaction.rollback(); transaction.rollback();
...@@ -73,34 +85,19 @@ public final class OptionMgmtService { ...@@ -73,34 +85,19 @@ public final class OptionMgmtService {
throw new ServiceException(e); throw new ServiceException(e);
} }
} }
/** /**
* Adds or updates the specified option. * Removes the option specified by the given option id.
* *
* @param option the specified option * @param optionId the given option id
* @throws ServiceException * @throws ServiceException service exception
*/ */
public void addOrUpdateOption(final JSONObject option) throws ServiceException { public void removeOption(final String optionId) throws ServiceException {
final Transaction transaction = optionRepository.beginTransaction(); final Transaction transaction = optionRepository.beginTransaction();
try { try {
final String id = option.optString(Keys.OBJECT_ID); optionRepository.remove(optionId);
if (Strings.isEmptyOrNull(id)) {
optionRepository.add(option);
} else {
final JSONObject old = optionRepository.get(id);
if (null == old) { // The id is specified by caller
optionRepository.add(option);
} else {
old.put(Option.OPTION_CATEGORY, option.optString(Option.OPTION_CATEGORY));
old.put(Option.OPTION_VALUE, option.optString(Option.OPTION_VALUE));
optionRepository.update(id, old);
}
}
transaction.commit(); transaction.commit();
} catch (final Exception e) { } catch (final Exception e) {
if (transaction.isActive()) { if (transaction.isActive()) {
......
/*
* Copyright (c) 2009, 2010, 2011, 2012, 2013, B3log Team
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.b3log.solo.repository.impl;
import org.b3log.latke.Keys;
import org.b3log.latke.repository.Transaction;
import org.b3log.solo.AbstractTestCase;
import org.b3log.solo.model.Option;
import org.b3log.solo.repository.OptionRepository;
import org.json.JSONObject;
import org.testng.Assert;
import org.testng.annotations.Test;
/**
* {@link OptionRepositoryImpl} test case.
*
* @author <a href="mailto:DL88250@gmail.com">Liang Ding</a>
* @version 1.0.0.0, Apr 19, 2013
* @since 0.6.0
*/
@Test(suiteName = "repository")
public final class OptionRepositoryImplTestCase extends AbstractTestCase {
/**
* Tests.
*
* @throws Exception exception
*/
@Test
public void test() throws Exception {
final OptionRepository optionRepository = getOptionRepository();
final JSONObject option = new JSONObject();
option.put(Keys.OBJECT_ID, Option.ID_C_BROADCAST_CHANCE_EXPIRATION_TIME);
option.put(Option.OPTION_CATEGORY, Option.CATEGORY_C_BROADCAST);
option.put(Option.OPTION_VALUE, 0L);
Transaction transaction = optionRepository.beginTransaction();
optionRepository.add(option);
transaction.commit();
Assert.assertEquals(optionRepository.count(), 1);
Assert.assertNotNull(optionRepository.get(Option.ID_C_BROADCAST_CHANCE_EXPIRATION_TIME));
}
}
/*
* Copyright (c) 2009, 2010, 2011, 2012, 2013, B3log Team
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.b3log.solo.service;
import org.b3log.latke.Keys;
import org.b3log.solo.AbstractTestCase;
import org.b3log.solo.model.Option;
import org.json.JSONObject;
import org.testng.Assert;
import org.testng.annotations.Test;
/**
* {@link OptionMgmtService} test case.
*
* @author <a href="mailto:DL88250@gmail.com">Liang Ding</a>
* @version 1.0.0.0, Apr 19, 2013
* @since 0.6.0
*/
@Test(suiteName = "service")
public class OptionMgmtServiceTestCase extends AbstractTestCase {
/**
* Add.
*
* @throws Exception exception
*/
@Test
public void add() throws Exception {
final OptionMgmtService optionMgmtService = getOptionMgmtService();
final JSONObject option = new JSONObject();
option.put(Keys.OBJECT_ID, Option.ID_C_BROADCAST_CHANCE_EXPIRATION_TIME);
option.put(Option.OPTION_CATEGORY, Option.CATEGORY_C_BROADCAST);
option.put(Option.OPTION_VALUE, 0L);
final String id = optionMgmtService.addOrUpdateOption(option);
System.out.println(id);
Assert.assertNotNull(id);
}
/**
* Update.
*
* @throws Exception exception
*/
@Test
public void update() throws Exception {
final OptionMgmtService optionMgmtService = getOptionMgmtService();
JSONObject option = new JSONObject();
option.put(Keys.OBJECT_ID, Option.ID_C_BROADCAST_CHANCE_EXPIRATION_TIME);
option.put(Option.OPTION_CATEGORY, Option.CATEGORY_C_BROADCAST);
option.put(Option.OPTION_VALUE, 0L);
final String id = optionMgmtService.addOrUpdateOption(option); // Add
System.out.println(id);
Assert.assertNotNull(id);
option = new JSONObject();
option.put(Keys.OBJECT_ID, Option.ID_C_BROADCAST_CHANCE_EXPIRATION_TIME);
option.put(Option.OPTION_CATEGORY, Option.CATEGORY_C_BROADCAST);
option.put(Option.OPTION_VALUE, 1L);
optionMgmtService.addOrUpdateOption(option); // Update
final JSONObject opt = getOptionQueryService().getOptionById(Option.ID_C_BROADCAST_CHANCE_EXPIRATION_TIME);
Assert.assertEquals(opt.getInt(Option.OPTION_VALUE), 1L);
}
/**
* Remove.
*
* @throws Exception exception
*/
@Test
public void remove() throws Exception {
final OptionMgmtService optionMgmtService = getOptionMgmtService();
final JSONObject option = new JSONObject();
option.put(Keys.OBJECT_ID, Option.ID_C_BROADCAST_CHANCE_EXPIRATION_TIME);
option.put(Option.OPTION_CATEGORY, Option.CATEGORY_C_BROADCAST);
option.put(Option.OPTION_VALUE, 0L);
final String id = optionMgmtService.addOrUpdateOption(option);
Assert.assertNotNull(id);
optionMgmtService.removeOption(id);
final JSONObject opt = getOptionQueryService().getOptionById(id);
Assert.assertNull(opt);
}
}
/*
* Copyright (c) 2009, 2010, 2011, 2012, 2013, B3log Team
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.b3log.solo.service;
import org.b3log.latke.Keys;
import org.b3log.latke.util.Requests;
import org.b3log.solo.AbstractTestCase;
import org.b3log.solo.model.Link;
import org.b3log.solo.model.Option;
import org.json.JSONObject;
import org.testng.Assert;
import org.testng.annotations.Test;
/**
* {@link OptionQueryService} test case.
*
* @author <a href="mailto:DL88250@gmail.com">Liang Ding</a>
* @version 1.0.0.0, Apr 19, 2013
* @since 0.6.0
*/
@Test(suiteName = "service")
public class OptionQueryServiceTestCase extends AbstractTestCase {
/**
* Gets.
*
* @throws Exception exception
*/
@Test
public void get() throws Exception {
// Check
final OptionQueryService optionQueryService = getOptionQueryService();
JSONObject options = optionQueryService.getOptions(Option.CATEGORY_C_BROADCAST);
Assert.assertNull(options);
// Add one
final OptionMgmtService optionMgmtService = getOptionMgmtService();
final JSONObject option = new JSONObject();
option.put(Keys.OBJECT_ID, Option.ID_C_BROADCAST_CHANCE_EXPIRATION_TIME);
option.put(Option.OPTION_CATEGORY, Option.CATEGORY_C_BROADCAST);
option.put(Option.OPTION_VALUE, 0L);
final String id = optionMgmtService.addOrUpdateOption(option);
Assert.assertNotNull(id);
// Check again
options = optionQueryService.getOptions(Option.CATEGORY_C_BROADCAST);
Assert.assertNotNull(options);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment