// Path: src/main/java/net/unicon/cas/addons/serviceregistry/ReadWriteJsonServiceRegistryDao.java
// public final class ReadWriteJsonServiceRegistryDao extends JsonServiceRegistryDao {
// 
//     public ReadWriteJsonServiceRegistryDao(final Resource servicesConfigFile) {
//         super(servicesConfigFile);
//         this.objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
//     }
// 
//     @Override
//     protected RegisteredService saveInternal(final RegisteredService registeredService) {
//         logger.debug("Loading service definitions from resource [{}]", this.servicesConfigFile.getFilename());
//         final List<RegisteredService> resolvedServices = super.loadServices();
//         final List<RegisteredService> col = new ArrayList<RegisteredService>(resolvedServices);
// 
//         if (registeredService.getId() < 0) {
//             if (registeredService instanceof AbstractRegisteredService) {
//                 final Random random = new Random(registeredService.hashCode());
//                 final int serviceId = random.nextInt(Integer.MAX_VALUE);
//                 ((AbstractRegisteredService) registeredService).setId(serviceId);
//             }
//         }
//         boolean foundAndRemovedService = false;
//         final Iterator<RegisteredService> it = col.iterator();
//         while(!foundAndRemovedService && it.hasNext()) {
//             if (it.next().getId() == registeredService.getId()) {
//                 it.remove();
//                 foundAndRemovedService = true;
//             }
//         }
//         col.add(registeredService);
// 
//         saveListOfRegisteredServices(col);
//         return registeredService;
//     }
// 
//     @Override
//     protected boolean deleteInternal(final RegisteredService registeredService) {
//         logger.debug("Loading service definitions from resource [{}]", this.servicesConfigFile.getFilename());
//         final List<RegisteredService> resolvedServices = super.loadServices();
//         final RegisteredService regServiceToDelete = findServiceById(registeredService.getId());
// 
//         if (regServiceToDelete != null) {
//             logger.debug("Found service definition to remove: [{}]", regServiceToDelete);
//             final List<RegisteredService> col = new ArrayList<RegisteredService>(resolvedServices);
//             col.remove(regServiceToDelete);
// 
//             saveListOfRegisteredServices(col);
//             return true;
//         }
//         return false;
//     }
// 
//     private void saveListOfRegisteredServices(final List<RegisteredService> col) {
//         OutputStream out = null;
//         FileOutputStream fout = null;
// 
//         try {
//             fout = new FileOutputStream(this.servicesConfigFile.getFile());
//             out = new BufferedOutputStream(fout);
// 
//             final Map<String, Object> map = new LinkedHashMap<String, Object>(col.size());
//             map.put(SERVICES_KEY, col);
// 
//             logger.debug("Writing [{}] service definitions to resource [{}]", col.size(), this.servicesConfigFile.getFilename());
//             this.objectMapper.writerWithDefaultPrettyPrinter().writeValue(out, map);
// 
//             fout.flush();
//             out.flush();
// 
//         } catch (final Exception e) {
//             logger.error(e.getMessage(), e);
//         } finally {
//             IOUtils.closeQuietly(fout);
//             IOUtils.closeQuietly(out);
//         }
//     }
// }


import net.unicon.cas.addons.serviceregistry.ReadWriteJsonServiceRegistryDao;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.junit.Assert.assertTrue;
package net.unicon.cas.addons.config;



/**
 * @author Dmitriy Kopylenko
 * @author Unicon, inc.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class CasNamespaceReadWriteJsonServiceRegistryDaoParserTests {

    @Autowired
    ApplicationContext applicationContext;

    private static final String SERVICE_REGISTRY_DAO_BEAN_NAME = "serviceRegistryDao";

    @Test
    public void readWriteJsonServiceRegistryDaoBeanDefinitionCorrectlyParsed() {
        assertTrue(applicationContext.containsBean(SERVICE_REGISTRY_DAO_BEAN_NAME));
// Path: src/main/java/net/unicon/cas/addons/serviceregistry/ReadWriteJsonServiceRegistryDao.java
// public final class ReadWriteJsonServiceRegistryDao extends JsonServiceRegistryDao {
// 
//     public ReadWriteJsonServiceRegistryDao(final Resource servicesConfigFile) {
//         super(servicesConfigFile);
//         this.objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
//     }
// 
//     @Override
//     protected RegisteredService saveInternal(final RegisteredService registeredService) {
//         logger.debug("Loading service definitions from resource [{}]", this.servicesConfigFile.getFilename());
//         final List<RegisteredService> resolvedServices = super.loadServices();
//         final List<RegisteredService> col = new ArrayList<RegisteredService>(resolvedServices);
// 
//         if (registeredService.getId() < 0) {
//             if (registeredService instanceof AbstractRegisteredService) {
//                 final Random random = new Random(registeredService.hashCode());
//                 final int serviceId = random.nextInt(Integer.MAX_VALUE);
//                 ((AbstractRegisteredService) registeredService).setId(serviceId);
//             }
//         }
//         boolean foundAndRemovedService = false;
//         final Iterator<RegisteredService> it = col.iterator();
//         while(!foundAndRemovedService && it.hasNext()) {
//             if (it.next().getId() == registeredService.getId()) {
//                 it.remove();
//                 foundAndRemovedService = true;
//             }
//         }
//         col.add(registeredService);
// 
//         saveListOfRegisteredServices(col);
//         return registeredService;
//     }
// 
//     @Override
//     protected boolean deleteInternal(final RegisteredService registeredService) {
//         logger.debug("Loading service definitions from resource [{}]", this.servicesConfigFile.getFilename());
//         final List<RegisteredService> resolvedServices = super.loadServices();
//         final RegisteredService regServiceToDelete = findServiceById(registeredService.getId());
// 
//         if (regServiceToDelete != null) {
//             logger.debug("Found service definition to remove: [{}]", regServiceToDelete);
//             final List<RegisteredService> col = new ArrayList<RegisteredService>(resolvedServices);
//             col.remove(regServiceToDelete);
// 
//             saveListOfRegisteredServices(col);
//             return true;
//         }
//         return false;
//     }
// 
//     private void saveListOfRegisteredServices(final List<RegisteredService> col) {
//         OutputStream out = null;
//         FileOutputStream fout = null;
// 
//         try {
//             fout = new FileOutputStream(this.servicesConfigFile.getFile());
//             out = new BufferedOutputStream(fout);
// 
//             final Map<String, Object> map = new LinkedHashMap<String, Object>(col.size());
//             map.put(SERVICES_KEY, col);
// 
//             logger.debug("Writing [{}] service definitions to resource [{}]", col.size(), this.servicesConfigFile.getFilename());
//             this.objectMapper.writerWithDefaultPrettyPrinter().writeValue(out, map);
// 
//             fout.flush();
//             out.flush();
// 
//         } catch (final Exception e) {
//             logger.error(e.getMessage(), e);
//         } finally {
//             IOUtils.closeQuietly(fout);
//             IOUtils.closeQuietly(out);
//         }
//     }
// }


// Path: src/test/java/net/unicon/cas/addons/config/CasNamespaceReadWriteJsonServiceRegistryDaoParserTests.java
import net.unicon.cas.addons.serviceregistry.ReadWriteJsonServiceRegistryDao;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.junit.Assert.assertTrue;

package net.unicon.cas.addons.config;



/**
 * @author Dmitriy Kopylenko
 * @author Unicon, inc.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class CasNamespaceReadWriteJsonServiceRegistryDaoParserTests {

    @Autowired
    ApplicationContext applicationContext;

    private static final String SERVICE_REGISTRY_DAO_BEAN_NAME = "serviceRegistryDao";

    @Test
    public void readWriteJsonServiceRegistryDaoBeanDefinitionCorrectlyParsed() {
        assertTrue(applicationContext.containsBean(SERVICE_REGISTRY_DAO_BEAN_NAME));
        assertTrue(applicationContext.getBeansOfType(ReadWriteJsonServiceRegistryDao.class).size() == 1);