9cb027acf71063111056285de8cef28e2742d5c3
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * 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 writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * 
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.plugins.context.schema.avro;
22
23 import static org.junit.Assert.assertTrue;
24 import static org.junit.Assert.fail;
25
26 import org.junit.After;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.onap.policy.apex.context.impl.schema.SchemaHelperFactory;
30 import org.onap.policy.apex.context.parameters.ContextParameterConstants;
31 import org.onap.policy.apex.context.parameters.SchemaParameters;
32 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
33 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
34 import org.onap.policy.apex.model.basicmodel.service.ModelService;
35 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
36 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas;
37 import org.onap.policy.common.parameters.ParameterService;
38
39 /**
40  * @author Liam Fallon (liam.fallon@ericsson.com)
41  * @version
42  */
43 public class TestAvroSchemaHelperBadSchemas {
44     private final AxKey testKey = new AxArtifactKey("AvroTest", "0.0.1");
45     private AxContextSchemas schemas;
46
47     @Before
48     public void initTest() {
49         schemas = new AxContextSchemas(new AxArtifactKey("AvroSchemas", "0.0.1"));
50         ModelService.registerModel(AxContextSchemas.class, schemas);
51     }
52
53     @Before
54     public void initContext() {
55         SchemaParameters schemaParameters = new SchemaParameters();
56         schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME);
57         schemaParameters.getSchemaHelperParameterMap().put("AVRO", new AvroSchemaHelperParameters());
58         ParameterService.register(schemaParameters);
59         
60     }
61
62     @After
63     public void clearContext() {
64         ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME);
65     }
66
67     @Test
68     public void badSchemaTest() {
69         final AxContextSchema avroBadSchema0 = new AxContextSchema(new AxArtifactKey("AvroBad0", "0.0.1"), "AVRO", "}");
70         schemas.getSchemasMap().put(avroBadSchema0.getKey(), avroBadSchema0);
71
72         try {
73             new SchemaHelperFactory().createSchemaHelper(testKey, avroBadSchema0.getKey());
74             fail("This test should throw an exception");
75         } catch (final Exception e) {
76             assertTrue(e.getMessage()
77                     .startsWith("AvroTest:0.0.1: avro context schema \"AvroBad0:0.0.1\" schema is invalid"));
78         }
79
80         final AxContextSchema avroBadSchema1 = new AxContextSchema(new AxArtifactKey("AvroBad1", "0.0.1"), "AVRO", "");
81         schemas.getSchemasMap().put(avroBadSchema1.getKey(), avroBadSchema1);
82
83         try {
84             new SchemaHelperFactory().createSchemaHelper(testKey, avroBadSchema1.getKey());
85             fail("This test should throw an exception");
86         } catch (final Exception e) {
87             assertTrue(e.getMessage()
88                     .startsWith("AvroTest:0.0.1: avro context schema \"AvroBad1:0.0.1\" schema is invalid"));
89         }
90
91         final AxContextSchema avroBadSchema2 =
92                 new AxContextSchema(new AxArtifactKey("AvroBad2", "0.0.1"), "AVRO", "{}");
93         schemas.getSchemasMap().put(avroBadSchema2.getKey(), avroBadSchema2);
94
95         try {
96             new SchemaHelperFactory().createSchemaHelper(testKey, avroBadSchema2.getKey());
97             fail("This test should throw an exception");
98         } catch (final Exception e) {
99             assertTrue(e.getMessage()
100                     .startsWith("AvroTest:0.0.1: avro context schema \"AvroBad2:0.0.1\" schema is invalid"));
101         }
102
103         final AxContextSchema avroBadSchema3 =
104                 new AxContextSchema(new AxArtifactKey("AvroBad3", "0.0.1"), "AVRO", "{zooby}");
105         schemas.getSchemasMap().put(avroBadSchema3.getKey(), avroBadSchema3);
106
107         try {
108             new SchemaHelperFactory().createSchemaHelper(testKey, avroBadSchema3.getKey());
109             fail("This test should throw an exception");
110         } catch (final Exception e) {
111             assertTrue(e.getMessage()
112                     .startsWith("AvroTest:0.0.1: avro context schema \"AvroBad3:0.0.1\" schema is invalid"));
113         }
114
115         final AxContextSchema avroBadSchema4 =
116                 new AxContextSchema(new AxArtifactKey("AvroBad4", "0.0.1"), "AVRO", "{\"zooby\"}");
117         schemas.getSchemasMap().put(avroBadSchema4.getKey(), avroBadSchema4);
118
119         try {
120             new SchemaHelperFactory().createSchemaHelper(testKey, avroBadSchema4.getKey());
121             fail("This test should throw an exception");
122         } catch (final Exception e) {
123             assertTrue(e.getMessage()
124                     .startsWith("AvroTest:0.0.1: avro context schema \"AvroBad4:0.0.1\" schema is invalid"));
125         }
126
127         final AxContextSchema avroBadSchema5 =
128                 new AxContextSchema(new AxArtifactKey("AvroBad5", "0.0.1"), "AVRO", "{\"type\": \"zooby\"}");
129         schemas.getSchemasMap().put(avroBadSchema5.getKey(), avroBadSchema5);
130
131         try {
132             new SchemaHelperFactory().createSchemaHelper(testKey, avroBadSchema5.getKey());
133             fail("This test should throw an exception");
134         } catch (final Exception e) {
135             assertTrue(e.getMessage()
136                     .startsWith("AvroTest:0.0.1: avro context schema \"AvroBad5:0.0.1\" schema is invalid"));
137         }
138     }
139 }