dhis2-devs team mailing list archive
-
dhis2-devs team
-
Mailing list archive
-
Message #40339
[Branch ~dhis2-devs-core/dhis2/trunk] Rev 20444: Added tests for KeyJsonValueStore, as well as refactoring code to follow code style and making th...
Merge authors:
Stian Sandvold (stian-sandvold)
------------------------------------------------------------
revno: 20444 [merge]
committer: Stian Sandvold <stian.sandvold@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2015-10-01 16:41:20 +0200
message:
Added tests for KeyJsonValueStore, as well as refactoring code to follow code style and making the code overall cleaner.
added:
dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/keyjsonvalue/
dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/keyjsonvalue/KeyJsonValueStoreTest.java
modified:
dhis-2/dhis-api/src/main/java/org/hisp/dhis/keyjsonvalue/KeyJsonValueStore.java
dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/keyjsonvalue/DefaultKeyJsonValueService.java
dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/keyjsonvalue/hibernate/HibernateKeyJsonValueStore.java
dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/KeyJsonValueController.java
--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk
Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/keyjsonvalue/KeyJsonValueStore.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/keyjsonvalue/KeyJsonValueStore.java 2015-10-01 09:03:27 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/keyjsonvalue/KeyJsonValueStore.java 2015-10-01 10:55:33 +0000
@@ -42,7 +42,7 @@
List<String> getKeysInNamespace( String namespace );
- void deleteKeysInNamespace( String namespace );
+ List<KeyJsonValue> getKeyJsonValueByNamespace( String namespace );
KeyJsonValue getKeyJsonValue( String namespace, String key );
}
=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/keyjsonvalue/DefaultKeyJsonValueService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/keyjsonvalue/DefaultKeyJsonValueService.java 2015-10-01 09:03:27 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/keyjsonvalue/DefaultKeyJsonValueService.java 2015-10-01 10:55:33 +0000
@@ -65,7 +65,7 @@
@Override
public void deleteNamespace( String namespace )
{
- keyJsonValueStore.deleteKeysInNamespace( namespace );
+ keyJsonValueStore.getKeyJsonValueByNamespace( namespace ).forEach( keyJsonValueStore::delete );
}
@Override
=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/keyjsonvalue/hibernate/HibernateKeyJsonValueStore.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/keyjsonvalue/hibernate/HibernateKeyJsonValueStore.java 2015-10-01 09:03:27 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/keyjsonvalue/hibernate/HibernateKeyJsonValueStore.java 2015-10-01 10:55:33 +0000
@@ -32,6 +32,7 @@
import org.hisp.dhis.common.hibernate.HibernateIdentifiableObjectStore;
import org.hisp.dhis.keyjsonvalue.KeyJsonValue;
import org.hisp.dhis.keyjsonvalue.KeyJsonValueStore;
+import org.hisp.dhis.query.Query;
import java.util.List;
@@ -43,33 +44,33 @@
implements KeyJsonValueStore
{
@Override
- @SuppressWarnings("unchecked")
+ @SuppressWarnings( "unchecked" )
public List<String> getNamespaces()
{
- return getQuery( "SELECT distinct namespace FROM org.hisp.dhis.keyjsonvalue.KeyJsonValue" ).list();
+ String hql = "SELECT distinct namespace FROM KeyJsonValue";
+ return getQuery( hql ).list();
}
@Override
- @SuppressWarnings("unchecked")
+ @SuppressWarnings( "unchecked" )
public List<String> getKeysInNamespace( String namespace )
{
- return getQuery(
- "SELECT distinct key FROM org.hisp.dhis.keyjsonvalue.KeyJsonValue WHERE namespace LIKE '" +
- namespace + "'" ).list();
+ String hql = "SELECT key FROM KeyJsonValue WHERE namespace = :namespace";
+ return getQuery( hql ).setString( "namespace", namespace ).list();
}
@Override
- @SuppressWarnings("unchecked")
- public void deleteKeysInNamespace( String namespace )
+ @SuppressWarnings( "unchecked" )
+ public List<KeyJsonValue> getKeyJsonValueByNamespace( String namespace )
{
- getCriteria( Restrictions.eq( "namespace", namespace ) ).list().forEach( o -> delete( (KeyJsonValue) o ) );
+ return getCriteria( Restrictions.eq( "namespace", namespace ) ).list();
}
@Override
public KeyJsonValue getKeyJsonValue( String namespace, String key )
{
- return (KeyJsonValue) getCriteria(
- Restrictions.eq( "namespace", namespace ),
+ return (KeyJsonValue) getCriteria(
+ Restrictions.eq( "namespace", namespace ),
Restrictions.eq( "key", key ) ).uniqueResult();
}
}
=== added directory 'dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/keyjsonvalue'
=== added file 'dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/keyjsonvalue/KeyJsonValueStoreTest.java'
--- dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/keyjsonvalue/KeyJsonValueStoreTest.java 1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/keyjsonvalue/KeyJsonValueStoreTest.java 2015-10-01 14:36:09 +0000
@@ -0,0 +1,107 @@
+package org.hisp.dhis.keyjsonvalue;
+
+import org.hisp.dhis.DhisSpringTest;
+import org.junit.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+
+import java.util.List;
+
+import static org.junit.Assert.*;
+
+/**
+ * @author Stian Sandvold.
+ */
+public class KeyJsonValueStoreTest extends DhisSpringTest
+{
+
+ @Autowired
+ private KeyJsonValueStore keyJsonValueStore;
+
+ @Test
+ public void testAddKeyJsonValue()
+ {
+ KeyJsonValue keyJsonValue = new KeyJsonValue();
+ keyJsonValue.setNamespace( "A" );
+ keyJsonValue.setKey( "1" );
+
+ int id = keyJsonValueStore.save( keyJsonValue );
+
+ assertNotNull( keyJsonValue );
+ assertEquals( keyJsonValue, keyJsonValueStore.get( id ) );
+ }
+
+ @Test
+ public void testAddKeyJsonValuesAndGetNamespaces()
+ {
+ KeyJsonValue keyJsonValueA = new KeyJsonValue();
+ keyJsonValueA.setNamespace( "A" );
+ keyJsonValueA.setKey( "1" );
+ keyJsonValueStore.save( keyJsonValueA );
+
+ KeyJsonValue keyJsonValueB = new KeyJsonValue();
+ keyJsonValueB.setNamespace( "B" );
+ keyJsonValueB.setKey( "1" );
+ keyJsonValueStore.save( keyJsonValueB );
+
+ List<String> list = keyJsonValueStore.getNamespaces();
+
+ assertTrue( list.contains( "A" ) );
+ assertTrue( list.contains( "B" ) );
+ }
+
+ @Test
+ public void testAddKeyJsonValuesAndGetKeysFromNamespace()
+ {
+ KeyJsonValue keyJsonValueA = new KeyJsonValue();
+ keyJsonValueA.setNamespace( "A" );
+ keyJsonValueA.setKey( "1" );
+ keyJsonValueStore.save( keyJsonValueA );
+
+ KeyJsonValue keyJsonValueB = new KeyJsonValue();
+ keyJsonValueB.setNamespace( "A" );
+ keyJsonValueB.setKey( "2" );
+ keyJsonValueStore.save( keyJsonValueB );
+
+ List<String> list = keyJsonValueStore.getKeysInNamespace( "A" );
+
+ assertTrue( list.contains( "1" ) );
+ assertTrue( list.contains( "2" ) );
+ }
+
+ @Test
+ public void testAddKeyJsonValueAndGetKeyJsonValue()
+ {
+ KeyJsonValue keyJsonValueA = new KeyJsonValue();
+ keyJsonValueA.setNamespace( "A" );
+ keyJsonValueA.setKey( "1" );
+ keyJsonValueStore.save( keyJsonValueA );
+
+ assertEquals( keyJsonValueStore.getKeyJsonValue( "A", "1" ), keyJsonValueA );
+ }
+
+ @Test
+ public void testGetKeyJsonValuesByNamespace()
+ {
+ KeyJsonValue keyJsonValueA = new KeyJsonValue();
+ keyJsonValueA.setNamespace( "A" );
+ keyJsonValueA.setKey( "1" );
+ keyJsonValueStore.save( keyJsonValueA );
+
+ KeyJsonValue keyJsonValueB = new KeyJsonValue();
+ keyJsonValueB.setNamespace( "A" );
+ keyJsonValueB.setKey( "2" );
+ keyJsonValueStore.save( keyJsonValueB );
+
+ KeyJsonValue keyJsonValueC = new KeyJsonValue();
+ keyJsonValueC.setNamespace( "A" );
+ keyJsonValueC.setKey( "3" );
+ keyJsonValueStore.save( keyJsonValueC );
+
+
+ List<KeyJsonValue> list = keyJsonValueStore.getKeyJsonValueByNamespace( "A" );
+
+ assertTrue(list.contains( keyJsonValueA ));
+ assertTrue(list.contains( keyJsonValueB ));
+ assertTrue(list.contains( keyJsonValueC ));
+ }
+}
=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/KeyJsonValueController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/KeyJsonValueController.java 2015-10-01 09:03:27 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/KeyJsonValueController.java 2015-10-01 10:02:47 +0000
@@ -29,21 +29,20 @@
*/
import java.io.IOException;
+import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.hisp.dhis.dxf2.render.RenderService;
+import org.hisp.dhis.dxf2.webmessage.WebMessage;
import org.hisp.dhis.dxf2.webmessage.WebMessageException;
import org.hisp.dhis.keyjsonvalue.KeyJsonValue;
import org.hisp.dhis.keyjsonvalue.KeyJsonValueService;
import org.hisp.dhis.webapi.utils.WebMessageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
-import org.springframework.web.bind.annotation.PathVariable;
-import org.springframework.web.bind.annotation.RequestBody;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RequestMethod;
+import org.springframework.web.bind.annotation.*;
/**
* @author Stian Sandvold
@@ -59,14 +58,18 @@
private RenderService renderService;
@RequestMapping( value = "", method = RequestMethod.GET, produces = "application/json" )
- public void getNamespaces( HttpServletResponse response )
+ public
+ @ResponseBody
+ List<String> getNamespaces( HttpServletResponse response )
throws IOException
{
- renderService.toJson( response.getOutputStream(), keyJsonValueService.getNamespaces() );
+ return keyJsonValueService.getNamespaces();
}
@RequestMapping( value = "/{namespace}", method = RequestMethod.GET, produces = "application/json" )
- public void getKeysInNamespace(
+ public
+ @ResponseBody
+ List<String> getKeysInNamespace(
@PathVariable String namespace,
HttpServletResponse response )
throws IOException, WebMessageException
@@ -77,13 +80,16 @@
WebMessageUtils.notFound( "The namespace '" + namespace + "' was not found." ) );
}
- renderService.toJson( response.getOutputStream(), keyJsonValueService.getKeysInNamespace( namespace ) );
+ return keyJsonValueService.getKeysInNamespace( namespace );
}
@RequestMapping( value = "/{namespace}", method = RequestMethod.DELETE )
- public void deleteNamespace(
+ public
+ @ResponseBody
+ WebMessage deleteNamespace(
@PathVariable String namespace,
- HttpServletResponse response ) throws WebMessageException
+ HttpServletResponse response )
+ throws WebMessageException
{
if ( !keyJsonValueService.getNamespaces().contains( namespace ) )
@@ -94,11 +100,13 @@
keyJsonValueService.deleteNamespace( namespace );
- throw new WebMessageException( WebMessageUtils.ok( "Namespace '" + namespace + "' deleted." ) );
+ return WebMessageUtils.ok( "Namespace '" + namespace + "' deleted." );
}
@RequestMapping( value = "/{namespace}/{key}", method = RequestMethod.GET, produces = "application/json" )
- public void getKeyJsonValue(
+ public
+ @ResponseBody
+ KeyJsonValue getKeyJsonValue(
@PathVariable String namespace,
@PathVariable String key,
HttpServletResponse response )
@@ -112,11 +120,13 @@
.notFound( "The key '" + key + "' was not found in the namespace '" + namespace + "'." ) );
}
- renderService.toJson( response.getOutputStream(), keyJsonValue );
+ return keyJsonValue;
}
@RequestMapping( value = "/{namespace}/{key}", method = RequestMethod.POST, produces = "application/json", consumes = "application/json" )
- public void addKey(
+ public
+ @ResponseBody
+ KeyJsonValue addKey(
@PathVariable String namespace,
@PathVariable String key,
@RequestBody String body,
@@ -142,12 +152,14 @@
keyJsonValueService.addKeyJsonValue( keyJsonValue );
- response.setStatus( 201 );
- renderService.toJson( response.getOutputStream(), keyJsonValue );
+ response.setStatus( HttpServletResponse.SC_CREATED );
+ return keyJsonValue;
}
@RequestMapping( value = "/{namespace}/{key}", method = RequestMethod.PUT, produces = "application/json", consumes = "application/json" )
- public void updateKeyJsonValue(
+ public
+ @ResponseBody
+ KeyJsonValue updateKeyJsonValue(
@PathVariable String namespace,
@PathVariable String key,
@RequestBody String body,
@@ -172,11 +184,13 @@
keyJsonValueService.updateKeyJsonValue( keyJsonValue );
- renderService.toJson( response.getOutputStream(), keyJsonValue );
+ return keyJsonValue;
}
@RequestMapping( value = "/{namespace}/{key}", method = RequestMethod.DELETE, produces = "application/json" )
- public void deleteKeyJsonValue(
+ public
+ @ResponseBody
+ WebMessage deleteKeyJsonValue(
@PathVariable String namespace,
@PathVariable String key,
HttpServletResponse response )
@@ -192,7 +206,6 @@
keyJsonValueService.deleteKeyJsonValue( keyJsonValue );
- throw new WebMessageException(
- WebMessageUtils.ok( "Key '" + key + "' deleted from namespace '" + namespace + "'." ) );
+ return WebMessageUtils.ok( "Key '" + key + "' deleted from namespace '" + namespace + "'." );
}
}